Www Auto Hot Key Com (1)

5
Home | Download | Documentation | Changelog | Support | Forum | Wiki  Search Tutorial and Overview This brief introduction will help you sta rt scri pting your own macros a nd hotkeys right awa y. Tutorial Contents Cre ating a scri pt Launching a program or document Sending keystrokes and mouse cli cks Activating and manipulating windows Getting i nput from the use r with MsgBox, InputBox , etc. Usi ng va ri ables a nd the cl i pboa rd Repe ati ng a seri es of acti ons over and over Manipulating files and folders Overvi ew of other features Creating a script Each s cri pt is a p lain text fi le containing commands to be e xecuted b y the program (AutoHotkey. exe). A scri pt may also con tain hotkeys an d hotstrings , or even consist e ntirely of them. Howe ver , i n the a bse nce of hotkeys a nd hots trings, a scri pt will perform its commands se quentially from top to bo ttom the moment it i s launched . T o cre ate a new scri pt: 1. Download and ins tall AutoHotkey. 2. Right-click an empty spot on your desktop or in a folder of your choice. 3. In the menu that appears, select New -> AutoHotkey Script . (Alternatively, select New -> Text Doc umen t.) 4. T ype a na me for the file, ensu ring that i t ends in .ahk. For example: T es t.ahk 5. Right-click the file and choos e Edit Scri pt. 6. On a new blank li ne, type the follow ing: #space::Run w ww .googl e.com In the line above, the first character "#" stands for the Windows key; so #spa ce means holdi ng down the Windows key then pressing the spacebar to activate the hotkey. T he :: means that the subseque nt command should be executed whe never this hotkey i s presse d, i n this case to go to the Google web site. To try out this script, continue as follows: 1. Save and cl ose the fil e. 2. Double-click the file to launch it. A new icon appe ars in the taskba r notif ication area. 3. Hold down the Windows key and press the spacebar . A web page opens in the de faul t browser. 4. To exit or edit the script, right- cli ck the gree n "H" icon in the ta skbar notification area. Notes: Multi ple scripts can be r unning simultaneously, each w ith it s own icon in the taskbar notification area. Each s cri pt can h ave multi ple hotkeys an d hotstrings . T o ha ve a s cri pt launch automaticall y whe n you s tart your computer , create a shortcut in the Start Menu's Startup folder . Launching a program or document The Run command is us ed to launch a program, document, URL, or shortcut. Here are s ome common examples: Run Notepad Run C:\My Documents\Address List.doc Run C:\My Documents\My Shortcut.lnk Run www.yahoo.com Run mailto:[email protected] A hotkey can be a ssigned to any of the a bove examples by incl uding a hotkey label. In the first example below, the assigned hotkey is Win+N, whil e in the second it is Control+Alt+C: #n::Run Notepad ^!c::Run calc.exe The above exampl es are known as singl e-l ine hotkeys because each consists of only one command. T o have more than one command executed by a hotke y, put the fir st line bene ath the hotkey de finition and make the last line a return. For example: #n:: Run http://www.google.com Run Notepad.exe return If the program or document to be run is not integrated w ith the syste m, spe cif y its ful l path to get it to launch: Run %A_ProgramFiles%\Winamp\Winamp.exe In the ab ove e xample, %A_ProgramFiles% is a built-in variable . By using it rather tha n so mething li ke C:\Program Files , the script is made more portable, meaning that it would be more likely to run on other computers. Note: The names of commands and variables are not case sensitive. For example, "Run" is the same as "run", and "A_ProgramFiles" is the same as "a_programfiles". To have the s cript wa it f or the program or document to close before continuing, use RunWait instead of Run. In the foll ow ing example, the MsgBox command will not execute until after the user closes Notepad: RunWai t Notepa d converted by Web2PDFConvert.com

Transcript of Www Auto Hot Key Com (1)

Page 1: Www Auto Hot Key Com (1)

8/8/2019 Www Auto Hot Key Com (1)

http://slidepdf.com/reader/full/www-auto-hot-key-com-1 1/4

Home | Download | Documentation | Changelog | Support | Forum | Wiki

  Search

Tutorial and Overview

This brief introduction will help you sta rt scripting your own macros a nd hotkeys right away.

Tutorial Contents

Creating a script

Launching a program or document

Sending keystrokes and mouse clicks

Activating and manipulating windows

Getting input from the user with MsgBox, InputBox, etc.

Using variables and the clipboard

Repeating a series of actions over and over

Manipulating files and folders

Overview of other features

Creating a script

Each script is a p lain text file containing commands to be e xecuted by the program (AutoHotkey.exe). A script may also con tain hotkeys and

hotstrings, or even consist e ntirely of them. However, in the absence of hotkeys a nd hots trings, a script will perform its commands sequentiallyfrom top to bo ttom the moment it is launched .

To create a new script:

1. Download and ins tall AutoHotkey.

2. Right-click an empty spot on you r desktop o r in a folder of your choice.

3. In the menu that appears, select New -> AutoHotkey Script . (Alternatively, select New -> Text Document.)

4. Type a name for the file, ensu ring that it ends in .ahk. For example: Tes t.ahk

5. Right-click the file and choose Edit Script.

6. On a new blank line, type the follow ing:#space::Run www .google.com

In the line above, the first character "#" stands for the Windows key; so #spa ce means holding down the Windows key then pressing thespacebar to activate the hotkey. The :: means that the subsequent command should be executed whenever this hotkey is presse d, in this caseto go to the Google web site. To try out this script, continue as follows:

1. Save and close the file.

2. Double-click the file to launch it. A new icon appe ars in the taskba r notification area.

3. Hold down the Windows key and press the spacebar. A web page opens in the de fault browser.4. To exit or edit the script, right-click the green "H" icon in the ta skbar no tification area.

Notes:

Multiple scripts can be running simultaneously, each w ith its own icon in the taskbar notification area.

Each s cript can have multiple hotkeys and hotstrings.

To have a s cript launch automatically whe n you s tart your computer, create a shortcut in the Start Menu's Startup folder.

Launching a program or document

The Run command is us ed to launch a program, document, URL, or shortcut. Here are some common examples:

Run NotepadRun C:\My Documents\Address List.docRun C:\My Documents\My Shortcut.lnkRun www .yahoo.comRun mailto:[email protected]

A hotkey can be a ssigned to any of the a bove examples by including a hotkey label. In the first example below, the assigned hotkey is Win+N,wh ile in the second it is Control+Alt+C:

#n::Run Notepad^!c::Run calc.exe

The above examples are known as single-line hotkeys because each consists of only one command. To have more than one command executedby a hotkey, put the first line beneath the hotkey de finition and make the last line a return. For example:

#n::Run http://www.google.comRun Notepad.exereturn

If the program or document to be run is not integ rated w ith the system, spe cify its full path to get it to launch:

Run %A_ProgramFiles%\Winamp\Winamp.exe

In the above example, %A_ProgramFiles% is a built-in variable. By using it rather than something like C:\Program Files, the script is made moreportable, meaning that it would be more likely to run on other computers. Note: The names of commands and variables are not case sensitive.For example, "Run" is the same as "run", and "A_ProgramFiles" is the same as "a_programfiles".

To have the script wa it for the program or document to close before continuing, use RunWait instead of Run. In the follow ing example, theMsgBox command will not execute until after the user closes Notepad:

RunWait Notepad

converted by Web2PDFConvert.com

Page 2: Www Auto Hot Key Com (1)

8/8/2019 Www Auto Hot Key Com (1)

http://slidepdf.com/reader/full/www-auto-hot-key-com-1 2/4

MsgBox The user has finished (Notepad has been closed).

To learn more about launching programs -- such as pass ing parameters , specifying the working directory, and discovering a program's exit code -- click here.

Sending keystrokes and mouse clicks

Keystrokes a re sent to the a ctive (foremost) window by using the Send command. In the follow ing example, Control+Alt+S becomes a hotkey totype a s ignature (ensure that a w indow such a s an editor or draft e-mail message is active before pressing the hotkey):

^!s::Send Sincerely,{Enter}John Smithreturn

In the above example, all characters are sent literally except {Enter}, which simulates a press of the Enter key. The next example illustratessome of the other commonly used special characters:

Send ̂ c!{tab}pasted:^v

The line above s ends a Co ntrol+C followed by an Alt+Tab followed by the string "pasted:" followed by a Control+V. See the Send command for acomplete list of special characters and keys.

Finally, keystrokes can also be sent in response to abbreviations you type, which are known as hotstrings. For example, whenever you type Btwfollowed by a s pace o r comma, the follow ing line w ill replace it w ith "By the w ay":

::btw::by the way

Mouse Clicks: To send a mouse click to a w indow it is first necessary to dete rmine the X and Y coordinate s where the click should occur. This canbe done with either AutoScriptWriter or Window Spy, which are included with AutoHotkey. The following steps apply to the Window Spy method:

1. Launch W indow Spy from a script's tray-icon menu or the Start Menu.

2. Activate the w indow o f interes t either by clicking its title bar, alt-tabbing, or other means (W indow Spy will stay "always on top" by des ign).3. Move the mouse cursor to the desired position in the target w indow and w rite dow n the mouse coordinates displayed by Window Spy (or

on W indows XP and ea rlier, press Shift-Alt-Tab to activate W indow Spy so tha t the "frozen" coo rdinates can be copied and pa sted ).

4. Apply the coordinates discovered above to the Click command. The following e xample clicks the left mouse button:Click 112, 223

To move the mouse w ithout clicking, use MouseMove. To drag the mouse, use MouseClickDrag.

Activating and manipulating windows

To activate a window (make it foremost), use WinActivate. To detect w hether a w indow exists, use IfWinExist or WinWait. The following exampleillustrates these commands:

IfWinExist Untitled - Notepad{

WinActivate}else

{ Run NotepadWinWait Untitled - NotepadWinActivate

}

The above example first se arches for any existing w indow w hose title starts w ith "Untitled - Notepad" (case sensitive). If such a w indow isfound, it is activated. Otherwise , Notepad is launched a nd the script waits for the Untitled window to appe ar, at which time it is activated. Theabove example also utilizes the last found window to avoid the need to specify the window's title to the right of each WinActivate.

Some o f the othe r commonly used window commands are:

IfWinActive: Checks if the specified window is currently active.

WinWaitActive: Waits for the specified window to become active (typically used after sending a window-activating keystroke such aspress ing Control-F for "Find").

WinClose: Closes the s pecified window.

WinMove: Moves and/or resizes the s pecified window.

WinMinimize, WinMaximize, WinRestore: Minimizes , maximizes, or restores the s pecified w indow, respectively.

Getting input from the user with MsgBox, InputBox, etc.

The following example displays a dialog with two buttons (YES and NO):

MsgBox, 4, , Would you like to continue?IfMsgBox, No

return; Otherwise, the user picked yes.MsgBox You pressed YES.

Use the InputBox command to p rompt the use r to type a string. Use FileSelectFile or FileSelectFolder to have the us er se lect a file or folder. Formore advanced ta sks, use the Gui command to create custom data entry forms and user interfaces.

Tip: You may have noticed from the other examples that the first comma of any command may be o mitted (except w hen the first paramete r isblank or starts w ith := or =, or the command is a lone at the top of a continuation section). For example:

MsgBox This is ok.MsgBox, This is ok too (it has an e xplicit comma).

Using variables and the clipboard

A variable is an a rea o f memory in which the s cript stores text or numbers. A variable con taining only digits (with an optiona l decimal point) isautomatically interpreted as a number when a math operation or comparison requires it.

With the e xception of local variables in functions, all variables a re global; that is, their conten ts may be read or altered by any part of the script.In addition, variables are no t declared; they come into existence s imply by using them (and e ach variable sta rts off empty/blank).

converted by Web2PDFConvert.com

Page 3: Www Auto Hot Key Com (1)

8/8/2019 Www Auto Hot Key Com (1)

http://slidepdf.com/reader/full/www-auto-hot-key-com-1 3/4

To assign a string to a variable, follow these examples:

MyVar1 = 123MyVar2 = my string

To compare the contents of a variable to a number or string, follow these examples:

if MyVar2 = my string{

MsgBox MyVar2 conta ins the s tring "my string".}if MyVar1 >= 100{

MsgBox MyVar1 contains %MyVar1%, wh ich is a number greater than or equa l to 100.

}

In the MsgBox line above, notice that the second o ccurrence of MyVar1 is enclosed in percent signs. This displays the contents of MyVar1 at thatposition. The same technique can be use d to copy the contents o f one variable to another. For example:

MyVarConcatena ted = %MyVar1% %MyVar2%

The line above stores the string "123 my string" (without the quotes) in the variable MyVarConcatenated.

To compare the contents of a variable w ith that of another, consider this example:

if (ItemCount > ItemLimit){

MsgBox The value in ItemCount, which is %ItemCoun t%, is grea ter than %ItemLimit%.}

Notice that the first line of the example a bove contains pa rentheses. The parentheses signify that the if-statement contains an expression.Without them, that line would be considered a "non-expression if-statement", and thus it would need percent signs around ItemLimit (such if-

statements are limited to a single comparison operator; that is, they cannot contain math operators or conjunctions such as "AND" and "OR").

Math: To perform a math opera tion, use the colon-equal ope rator (:=) to as sign the result of an expression to a variable as in the examplebelow:

NetPrice := Price * (1 - Discount/100)

See expressions for a complete list of math ope rators.

Clipboard: The variable named Clipboard is special because it contains the current text on the W indows clipboard. Even so, it can be us ed a sthough it were a normal variable. For example, the following line would display the current contents o f the clipboa rd:

MsgBox %clipboard%

To alter the clipboard, consider the following example, which replaces the current contents of the clipboard with new text:

clipboa rd = A line of text.`r`nA second line o f text.`r`n

In the above, `r and `n (accent followed by the letter "r" or "n") are used to indicate two special characters: carriage return and linefeed. Thesetwo characters start a new line of text as though the use r had pressed Enter.

To append te xt to the clipboard (or any other variable), follow this example:

clipboa rd = %clipboard% And here is the te xt to append.

See the clipboard and variables sections for more details.

Repeating a series of actions over and over 

To pe rform something more tha n once conse cutively, use a loop. The following loop shows a MsgBox three times:

Loop 3{

MsgBox This window will be displayed three times.}

You could also specify a variable after the word Loop, which is useful in situations where the number of iterations is determined somewhereinside the s cript:

Loop %RunCount%{

Run C:\Check Server Status.exeSleep 60000 ; Wait 60 seconds.

}

In the above, the loop is performed the specified number of times unless RunCount contains 0, in which case the loop is skipped entirely.

A loop may also terminate itself when one or more conditions change. The following example clicks the left mouse button repeatedly while theuser is holding down the F1 key:

$F1:: ; Make the F1 key into a hotkey (the $ symbol facilitates the "P" mode o f GetKeyState below ).Loop ; Since no number is specified with it, this is an infinite loop unless " break" or "return" is e ncountered inside.{

if not GetKeyState ("F1", "P") ; If this state ment is true, the use r has phys ically released the F1 key.

break ; Break out o f the loop.  ; Otherwise (since the above didn't "break"), keep clicking the mouse.

Click ; Click the left mouse button at the curso r's current pos ition.}return

In the example above, when the user release s the F1 key, the loop detects this and s tops itself via the break command. Break causes executionto jump to the line after the loop's closing brace.

converted by Web2PDFConvert.com

Page 4: Www Auto Hot Key Com (1)

8/8/2019 Www Auto Hot Key Com (1)

http://slidepdf.com/reader/full/www-auto-hot-key-com-1 4/4

An alternate w ay to achieve the same effect is with a "while" loop:

$F1::while GetKeyState("F1", "P") ; While the F1 key is being he ld down physically.{

Click}return

The examples show n above are gene ral-purpose loops. For more specialized needs, consider one o f the following loops:

File-reading/writing loop: Retrieves the lines in a text file, one at a time. This can be used to trans form a file into a different format on a line-by-line ba sis. It can also be used to se arch for lines matching your criteria.

Files and folders loop: Retrieves the spe cified files o r folders, one at a time. This allows an ope ration to be pe rformed upo n each file or folder that

meets your criteria.

Parsing loop: Retrieves substrings from a string, one at a time. This allows a string such as "Red,Green,Blue" to be easily broken down into itsthree component fields.

Registry loop: Retrieves the contents of the spe cified registry subkey, one item at a time.

Manipulating files and folders

To add text to the e nd of a file (or crea te a new file), use FileAppend as shown in the following example. Note that it uses `n (linefeed) to start anew line of text afterward:

FileAppend, A line of te xt to a ppend.`n, C:\My Documents\My Text File.txt

To ove rwrite an e xisting file, use FileDelete prior to FileAppend. For example:

FileDelete, C:\My Documents\My Text File.txt

Some of the othe r commonly used file and folder commands are:FileRead: Read the entire contents o f a file into a variable.

File-reading Loop: Retrieve the lines in a text file, one by one.

IfExist: Dete rmine w hethe r a file or folder exists.

FileSelectFile and FileSelectFolder: Display a dialog for the use r to pick a file or folder.

FileDelete /FileRecycle: Delete/recycle one or more files. Use FileRemoveDir to delete an e ntire folder.

FileCopy /FileMove: Copy/move one o r more files. Use FileCopyDir /FileMoveDir to copy/move an entire folder.

Files-and-folders Loop: Retrieve the files and folders conta ined in a folder, one a t a time.

FileSetAttrib and FileSetTime: Change the attributes or timestamp of one or more files.

IniRead, IniWrite , and IniDelete: Create, acces s, and maintain standard-format INI files.

RegRead, RegWrite, RegDelete , and Registry Loop: Work with the Windows registry.

Overview of other features

See the command list for an overview o f every command.

 

Home | Download | Documentation | Changelog | Support | Forum | Wiki

converted by Web2PDFConvert.com