117 posts in this topic

czardas 1,116

The entries for this challenge appear in the following two posts below this one. The purpose here is to demonstrate AutoIt to a beginner. The code should be easy to follow if you are a beginner. Speculation about the authors is expected, but remember it is the code you are voting for, not the person who wrote it. Before casting a vote please read the 7 criteria listed below. You are entitled to cast one vote for one piece of code which you think would make a good example for the wikipedia article.

A Note to Participants

Although it may be tempting, I would like it if you refrain from dropping hints to the voters once voting begins.

Deadline for Voting

Voting will last approximately 3 days until midnight GMT on 23rd of February.

In the Event of a Tie
If there is no clear winner after the period for voting has ended, finalists will be selected and voting will go to sudden death, whereby whichever of the the remaining contestants has the most votes at a time determined without forewarning by the arbiter, will be declared the winner. If it is still neck and neck after this period has elapsed, the winner will be selected using a formula.

Now we've seen what the community can do, it's time for us MVPs to be judged by the community. What has been suggested is that a second example ought to be written to demonstrate a simple automation task to an absolute beginner. So let's show the community what we can do! You are allowed no more than 32 lines of code including comments.

As before, the new example should:

1. comply to standard coding practices

2. be easy to follow

3. do something useful

4. be of interest to a wide audience

5. comply with the rules of the forum

6. not be longer than exactly 32 lines of code

7. convey the ease of use of AutoIt to beginners

Names will be removed from submissions by an independant arbiter before being judged.

The community will vote for the code they like best.

Only current MVPs are allowed to enter, and no MVP is entitled to vote for the winner.

Team efforts can also be submitted.

The deadline for submissions will be 20th February 2015. The arbiter is jaberwacky once more. All submissions should be sent to the arbiter. The community will be given three days to vote for the winner, once jaberwacky has posted the examples we send to him. After voting has finished jaberwacky will reveal the name of the MVP whose code you like best. Good luck to everyone!

The winning examples from both challenges will be added to the Wikipedia page.

Share this post

Link to post

Share on other sites

jaberwacky 313

Opt('MustDeclareVars',1); require that variables are decalared#include <MsgBoxConstants.au3>; for MsgBox constants#include <Date.au3>; for date functionsLocal$sProgram="C:\PROGRAM FILES\CCLEANER\CCLEANER.EXE "; program to runLocal$sParm="/auto"; parm for programLocal$iRun_Interval=0; how often to run program; Get user input for how often to run this program, set default to 15 minutes and only accept values between 1 and 60While$iRun_Interval<1Or$iRun_Interval>60$iRun_Interval=InputBox('Auto Run Interval','Enter # of minutes between automatic runs of'&@CRLF&_; continue statement on next line$sProgram&@CRLF&@CRLF&' (Value must be between 1 and 60)'&@CRLF&@CRLF,15,"",300,160); if the cancel button was pushed inform the user and exit in 5 secondsIf@error=1ThenExitMsgBox($MB_ICONINFORMATION,'Application Canceled','Application canceled by user action',5); if anything else went wrong warn the user and wait for user inputIf@error>1ThenExitMsgBox($MB_ICONERROR,'InputBox Error','Error = '&@error)WEnd; Main Loop / wait for $iRun_Interval minutes then run program / Loop until user stops the program using the tray or kills itLocal$Last_Run=_NowCalc(); set time of last scan to current date/timeWhile1; loop foreverIf_DateDiff('n',$Last_Run,_NowCalc())>=$iRun_IntervalThen; if the number of minutes from the last run is GT or EQ to the scan interval then_RunProgram($sProgram,$sParm); 1 - call the function to run the program with the parm$Last_Run=_NowCalc(); 2 - reset the date/time of last run to nowEndIfSleep(1000); idle the processor for 1 secondWEnd; function to run program / requires program name and parm / the parm can be "" (blank)Func_RunProgram($pgm,$prm)ShellExecute($pgm,$prm,"","",@SW_HIDE); see Help file for parm definitionEndFunc;==>_RunProgram

Example 2

#include <Array.au3>#include <MsgBoxConstants.au3>Global$iInput1=12345,$fResult$fResult= ConvertDatatype($iInput1,"MiB","KiB")If@errorThenMsgBox($MB_ICONERROR,"ERROR","An error has occured. Error code: "&@error)ElseMsgBox(BitOR($MB_ICONINFORMATION,$MB_SYSTEMMODAL),"Base 1024",$iInput1&" MiBytes = "&$fResult)EndIfFunc ConvertDatatype($fNumber,$sSourceBase,$sDestBase,$iPrefix=1024,$bSuffix=True);binary prefix = 1024, decimal = 1000IfNotNumber($fNumber)ThenReturnSetError(1,0,0);check whether input of 1st parameter is a numberIf$iPrefix<>1000And$iPrefix<>1024ThenReturnSetError(2,0,0);check whether bases are within range of 1000 / 1024;create 2 arrays for the prefix calculations, one for decimal calculation and one of binary calculationLocal$aData_Dec[9][2]=[["B",$iPrefix^0],["KB",$iPrefix^1],["MB",$iPrefix^2],["GB",$iPrefix^3],["TB",$iPrefix^4],["PB",$iPrefix^5],["EiB",$iPrefix^6],["ZB",$iPrefix^7],["YB",$iPrefix^8]]Local$aData_Bin[9][2]=[["B",$iPrefix^0],["KiB",$iPrefix^1],["MiB",$iPrefix^2],["GiB",$iPrefix^3],["TiB",$iPrefix^4],["PiB",$iPrefix^5],["EiB",$iPrefix^6],["ZiB",$iPrefix^7],["YiB",$iPrefix^8]]Local$iPos1,$iPos2,$sSuffix=$bSuffix?$sDestBase&"ytes":"";declare some variables and suffix for the ouput if enabled in function parameter $bSuffixSwitch$iPrefix;depending on the $iPrefix variable do the calculationCase1000;decimal$iPos1=_ArraySearch($aData_Dec,$sSourceBase);search in the array for the source baseIf@errorThenReturnSetError(3,0,0);if not found exit function and set error flagLocal$iPos2=_ArraySearch($aData_Dec,$sDestBase);search in the array for the destination baseIf@errorThenReturnSetError(4,0,0);if not found exit function and set error flagReturn$fNumber*$aData_Dec[$iPos1][1]/$aData_Dec[$iPos2][1]&" "&$sSuffix;do the calculation and return the resultCase1024;binary$iPos1=_ArraySearch($aData_Bin,$sSourceBase);search in the array for the source baseIf@errorThenReturnSetError(5,0,0);if not found exit function and set error flagLocal$iPos2=_ArraySearch($aData_Bin,$sDestBase);search in the array for the destination baseIf@errorThenReturnSetError(6,0,0);if not found exit function and set error flagReturn$fNumber*$aData_Bin[$iPos1][1]/$aData_Bin[$iPos2][1]&" "&$sSuffix;do the calculation and return the resultEndSwitchEndFunc;==>ConvertDatatype

#Include <File.au3>; Include an AutoIt instruction file, that contains details for the _FileCreate function.Global$count=1,$examplefolder=@MyDocumentsDir&"\AutoIt Example Directory",$examplefile="Example File.txt"; Reserve memory & Assign variables in Global scope.Global$answer,$character,$length,$name,$position,$wait=5,$examplepath=$examplefolder&"\"&$examplefile; Reserve memory & Assign variables in Global scope.Global$exampletext="Hello Jon, welcome to the Beginner's Example for programming with AutoIt."; Reserve memory & Assign text to a variable.DirCreate($examplefolder); Create a folder (directory) in My Documents for the example file._FileCreate($examplepath); Create or overwrite the example file.$name=InputBox("Beginner's Example","Please enter your name.","Jon"," M",200,130); Get a mandatory name.If$name<>""Then; If $name variable is not blank, continue, else exit program.$exampletext=StringReplace($exampletext,"Jon",$name); Replace the name Jon in variable, with the name entered by user.SplashTextOn("","Please Wait!",200,120,-1,-1,33); Show user a splash screen so they know something is happening.ShellExecute($examplepath); Open the default program for text files.WinWaitActive($examplefile,"",$wait); Wait for up to 5 seconds for text file program window to open.SplashOff(); Turn splash screen off.IfWinActive($examplefile,"")Then; If text file program window is open, continue with example, else exit program.Send($exampletext); Send the text content of $exampletext variable to the text file program window.Send("{ENTER 2}"); Send ENTER twice$exampletext="You can also send each character individually."; Assign new text to existing variable.$length=StringLen($exampletext); Gets the total number of characters in $exampletextFor$position=1To$length; The value of $position increments every NEXT.$character=StringMid($exampletext,$position,$count); Get one character.Send($character); Send the text character to the text file program window.Sleep(50); Pause for 50 milliseconds.Next; Loop until the total number of characters is reached (has been processed).Send("{ENTER}"); Send ENTER once.Send("^s"); Send the SAVE hotkey.$answer=MsgBox(262177,"Cleanup Query","Remove the Example folder and file?"); Query example removal.If$answer=1Then; If response is OK, then proceed with cleanup, else exit program.WinClose($examplefile,""); Close the text file program window.DirRemove($examplefolder,1); Delete the Example folder (directory) and its contents.EndIf; Exit the response process.EndIf; Exit the active window process.EndIf; Exit the successful name process.Exit; Exit the program.

Example 6

; Make available a library of constant values.#include <MsgBoxConstants.au3>; Display a message box with a timeout of 6 seconds.MsgBox($MB_OK,"Msg","Avoid touching the keyboard or mouse during automation",6); Run the Windows Calculator.Run("calc.exe"); Wait for the calculator to become active.WinWaitActive("Calculator"); Automatically type the current year into the calculator.Send(@YEAR); Let's slow the script down a bit so we can see what's going on.Sleep(600); Automatically type in 'divide by 4', and then sleep 600 ms.Send("/4")Sleep(600); Hit the return key to display the result, and sleep 600 ms.Send("{ENTER}")Sleep(600); Copy the result to the clipboard using the Windows shortcut Ctrl+C.Send("^c"); Declare, and assign the contents of the clipboard to, a variable.Local$fResult=ClipGet(); Check to see if the variable contains a decimal point or not.IfStringInStr($fResult,".")Then; Display a message box with a timeout of 5 seconds.MsgBox($MB_OK,"Leap Year",@YEAR&" is not a leap year.",5)Else; This message will only display if the current year is a leap year.MsgBox($MB_OK,"Leap Year",@YEAR&" is a leap year.",5)EndIf; Close the Windows calculator - always tidy up afterwards.WinClose("Calculator")

Share this post

Link to post

Share on other sites

JLogan3o13 1,125

Czardas can correct me, but from the conversation he and I had on this subject it was really to be open to the community at large; a chance to see what the MVPs come up with. I would say just post submissions here, without names, and let discussion take place. Multiple threads, and who can see them is (in my 2cents) counter productive to the goal of the challenge.

Edit: If you want to maintain some semblance of order, reference the Post # of each submission in the OP on this thread.

Share this post

Link to post

Share on other sites

JLogan3o13 1,125

My understanding of the mechanism was the submissions were sent privately to jaberwacky as the arbiter. He then presented his #1 and #2 to MVPs, there was some discussion on cleaning up variable names with the writers' permission, and the choices were published. Unless something more happened via PM (doubtful), that is it in a nutshell from the MVP forum.

Share this post

Link to post

Share on other sites

czardas 1,116

You raise a good point JohnOne. There's nothing stopping anyone starting a group PM to discuss the code in private. If I had the ability to create a private voting area, I might have considered it. MVPs should not be afraid of have their code discussed in public, otherwise do not enter. Votes will be made public to ensure that all votes are legitimate and voting by proxy is not allowed.

It was only after some consideration that I made the decision to create a second challenge. The entries received in the first challenge, including yours, were considered a little too advanced for an absolute beginner. It was suggested that if the example were to be placed alongside a simpler example, then there would be no problem. What I thought was missing was an example of automation of 3rd party software, which is what makes AutoIt special. Things are a little bit improvised, and it might have been better to create a panel of judges, but that would have meant voting for every member of the panel and quite a delay. Maybe it's an idea for the future.

There are two more days remaining. If you are planning on participation, please be sure to meet the deadline.

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

What is the Secret Key?Life is like a Donut

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense to disagree with you. TheSaint's Toolbox(be advised many downloads are not working due to ISP screwup with my storage)

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

What is the Secret Key?Life is like a Donut

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense to disagree with you. TheSaint's Toolbox(be advised many downloads are not working due to ISP screwup with my storage)