Sample Macros

 

 

 

The following is a collection of Hotkeys macro programs with descriptions of their usage. These programs are included with the Hotkeys installation and can be accessed with the Hotkeys Editor. Click the Tutorial button for a description of all macro code syntax.

 

Webpage Links
Web Source
Find String In Files
Send Email
Compare 2 Selected Text
Replace Hotstrings

Video Frame Splitter
Newest Version
Run Excel Macro

 

Webpage Links

The following Hotkeys macro "Webpage Links" extracts all the links from a webpage and places them into file c:\weblinks.html then loads the file into a new instance of IE.

The macro accesses the webpage's source code by sending IE keystrokes that select the View menu option, then Source. This action opens Notepad with the webpage source code containing all the page links. The source code is then saved to file c:\websource.txt, which the macro opens and parses out the "href=" links. The links are then written to file c:\weblinks.html which is loaded into a new instance of IE.

This macro extracts href links that contain the string "http://www". Change this string or use additional tests to nominate links that meet your specific criteria. When sending keystrokes to IE, it is not always a given that Notepad will be the topmost application after it opens. For this reason it is recommended to force the focus onto Notepad using the SETFOCUS command. This will ensure that Notepad will be the active foreground application that receives the keystrokes.

Use IE to visit a webpage with the links to extract. Activate the macro with its hotkey (e.g. the hotkey for the sample macro below was set to ^9 (Ctrl-9))

Notes

  • Lines of source code from a webpage can be very long and manipulating large strings can slow down the performance of the macro. Try to keep variable manipulations to a minimum for large string values.
  • Modify the HTML code in the WRITE statements to change the output format of the weblinks.html file. For this example, the links are centered and single spaced. Long links are word wrapped. The macro extracts links indiscriminately, which means not all links may be navigable.
  • The WAIT statement is used to create a delay so that some menu operations can complete before proceeding (e.g. Open Notepad, Save File As.., Confirm Close)
  • The macro does not check the links for file types. Add an additional test to extract a specific file type, e.g. "jpg", "htm", etc.

Macro Code

#Macro 'Webpage Links' extracts links from a web page into IE
[EXT="http://www"]
[LEXT=LEN(EXT)]
#Open Notepad with the webpage source text
[SENDKEYS "{F10}{RIGHT 2}{DOWN}c"]
[WAIT 1]
[PATHEXISTS "c:\websource.txt" ANS]
[IF ANS="YES"]
    [KILLFILE "c:\websource.txt"]
[ENDIF]
#Place the focus on Notepad and save the source text to a file
[SETFOCUS "Notepad" SUCCESS]
[IF SUCCESS="NO"]
    [YESNOCANCEL "Fatal Error SETFOCUS failed." ANS]
    [CANCEL IF SUCCESS="NO"]
[ENDIF]
[SENDKEYS "{F10}{DOWN}"]
[WAIT 1]
[SENDKEYS "a"]
[FSRC="c:\websource.txt"]
[SENDKEYS FSRC]
[SENDKEYS "~"]
[WAIT 1]
#Close Notepad
[SENDKEYS "{F10}{DOWN}"]
[WAIT 1]
[SENDKEYS "x"]
[WAIT 1]
[SENDKEYS "n"]
[ASCII="34"]
#Open the output html file and write the header info
[HTML="c:\weblinks.html"]
[OPEN 2 HTML]
[WRITE 2 "<html>"]
[WRITE 2 ""]
[WRITE 2 "<head>"]
[REC="<meta http-equiv="&ASCII&"Content-Type"&ASCII&" content="&ASCII&"text/html; charset=windows-1252"&ASCII&">"]
[WRITE 2 REC]
[WRITE 2 "<title>Webpage Links</title>"]
[WRITE 2 "</head>"]
[WRITE 2 "<body>"]
[TITLE="Webpage Links"]
[REC="<p align="&ASCII&"center"&ASCII&">"&TITLE&"</p>"]
[WRITE 2 REC]
[REC="<p align="&ASCII&"center" &ASCII&">"]
[WRITE 2 REC]
#Open source file, search for links & write links to file
[USTR="href="]
[OPEN 3 FSRC]
[CNT=0]
[LOOP Y=1-10000]
    [READ 3 HREC]
    [V=LEN(HREC)]
    [IF V>0]
        [SEARCH=1]
        [LOOP Z=1-1000]
            [FINDSTRING HREC USTR SEARCH]
            [IF SEARCH=0]
                [QUITLOOP]
            [ELSE]
                [SSEARCH=SEARCH+6]
                [START=SSEARCH]
                [FINDSTRING HREC ASCII SSEARCH]
                [IF SSEARCH>0]
                    [END=SSEARCH-1]
                    [URL=MID(HREC START END)]
                    [FSEARCH=1]
                    [FINDSTRING URL EXT FSEARCH]
                    [IF FSEARCH>0]
                        [REC="<a href="&ASCII&URL&ASCII&">"]
                        [WRITE 2 REC]
                        [REC=URL&"</a><br>"]
                        [WRITE 2 REC]
                        [CNT=CNT+1]
                    [ENDIF]
                [ENDIF]
                [SEARCH=SSEARCH]
            [ENDIF]
        [ENDLOOP]
    [ENDIF]
[ENDLOOP]
[CLOSE 3]
[IF CNT=0]
    [URL="No links were found"]
    [REC="<a>"&URL&"</a>"]
    [WRITE 2 REC]
[ENDIF]
#Write footer then close file
[WRITE 2 "</p>"]
[WRITE 2 "</body>"]
[WRITE 2 "</html>"]
[CLOSE 2]
#Launch IE and open the links file
[PATHEXISTS "c:\weblinks.html" ANS]
[IF ANS="NO"]
    [YESNOCANCEL "No links were found" ANS]
[ELSE]
    [S="c:\Program Files\Internet Explorer\iexplore.exe c:\weblinks.html"]
    [COMMAND S]
[ENDIF]

 

Web Source

The Hotkeys macro "Web Source" performs the same function as the "Webpage Links" macro except it does not require IE and Notepad to extract source code from webpages. The macro command [WEBSOURCE URL Src] retrieves the source code from a webpage and writes it to a file or variable, no browser required.

This macro retrieves an URL's source code and saves it to file c:\tmp\src.htm, which the macro opens and parses out the "href=" links. The links are then written to file c:\weblinks.html if they meet the search criteria. When the search is completed, the file is loaded into a new instance of IE.

This macro extracts href links that contain the string "http://www". Change this string or use additional tests to nominate links that meet your specific criteria.

* Note: This macro can be modified into a "WebCrawler" by adding a recursive loop to cycle through all the links posted into file c:\weblinks.html and searching for a string in the source code. Write the "hits" to a separate output file and display the listing upon completion.

Macro Code

#prompt for URL from user
[USERINPUT "Enter URL" URL]
[CANCEL IF URL=""]
[FSRC="e:\tmp\src.htm"]
[WEBSOURCE URL FSRC]
[IF FSRC="Timed Out" OR FSRC="File Error"]
    [YESNOCANCEL FSRC ANS]
    [CANCEL IF ANS=ANS]
[ENDIF]
[EXT="http://www"]
[LEXT=LEN(EXT)]
[ASCII="34"]
#Open the output html file and write the header info
[HTML="c:\weblinks.html"]
[OPEN 2 HTML]
[WRITE 2 "<html>"]
[WRITE 2 ""]
[WRITE 2 "<head>"]
[REC="<meta http-equiv="&ASCII&"Content-Type"&ASCII&" content="&ASCII&"text/html; charset=windows-1252"&ASCII&">"]
[WRITE 2 REC]
[WRITE 2 "<title>Webpage Links</title>"]
[WRITE 2 "</head>"]
[WRITE 2 "<body>"]
[TITLE="Webpage Links"]
[REC="<p align="&ASCII&"center"&ASCII&">"&TITLE&"</p>"]
[WRITE 2 REC]
[REC="<p align="&ASCII&"center" &ASCII&">"]
[WRITE 2 REC]
#Open source file, search for links & write links to file
[USTR="href="]
[OPEN 3 FSRC]
[CNT=0]
[LOOP Y=1-10000]
    [READ 3 HREC]
    [V=LEN(HREC)]
    [IF V>0]
        [SEARCH=1]
        [LOOP Z=1-1000]
            [FINDSTRING HREC USTR SEARCH]
            [IF SEARCH=0]
                [QUITLOOP]
            [ELSE]
                [SSEARCH=SEARCH+6]
                [START=SSEARCH]
                [FINDSTRING HREC ASCII SSEARCH]
                [IF SSEARCH>0]
                    [END=SSEARCH-1]
                    [URL=MID(HREC START END)]
                    [FSEARCH=1]
                    [FINDSTRING URL EXT FSEARCH]
                    [IF FSEARCH>0]
                        [REC="<a href="&ASCII&URL&ASCII&">"]
                        [WRITE 2 REC]
                        [REC=URL&"</a><br>"]
                        [WRITE 2 REC]
                        [CNT=CNT+1]
                    [ENDIF]
                [ENDIF]
                [SEARCH=SSEARCH]
            [ENDIF]
        [ENDLOOP]
    [ENDIF]
[ENDLOOP]
[CLOSE 3]
[IF CNT=0]
    [URL="No links were found"]
    [REC="<a>"&URL&"</a>"]
    [WRITE 2 REC]
[ENDIF]
#Write footer then close file
[WRITE 2 "</p>"]
[WRITE 2 "</body>"]
[WRITE 2 "</html>"]
[CLOSE 2]
#Launch IE and open the links file
[PATHEXISTS "c:\weblinks.html" ANS]
[IF ANS="NO"]
    [YESNOCANCEL "No links were found" ANS]
[ELSE]
    [S="c:\Program Files\Internet Explorer\iexplore.exe c:\weblinks.html"]
    [COMMAND S]
[ENDIF]
 


Find String In Files

The following Hotkeys macro "Find String In Files" prompts for a folder, a file extension and a string to search then proceeds to search in all the files of the specified type for the search string. All the search files must be ASCII based and a list of filenames containing the search string is posted in Notepad.

There are no setup conditions required to run this macro.

#List files containing a search string
[USERINPUT "dialgs+Find String Folder+Folder+++" FOLDER]
[CANCEL IF FOLDER=""]
[USERINPUT "Enter 3 letter extension (file type), e.g. TXT" FEXT]
[CANCEL IF FEXT=""]
[USERINPUT "Enter the string to search" SSTR]
[CANCEL IF SSTR=""]
[USTR=UCASE(SSTR)]
[DFILE="C:\FILENAMES.TXT"]
[F=FOLDER&"\*."&FEXT]
#Use DIR to write a list of the folder files
[CMD="COMSPEC DIR/B "&F&" >"&DFILE]
[COMMAND CMD]
[WAIT 1]
#Open input/output files
[OPEN 1 DFILE]
[OUTFILE="C:\FILESFOUND.TXT"]
[OPEN 2 OUTFILE]
[ASCII="34"]
[N=0]
#Read up to 1000 files
[LOOP X=1-1000]
    [READ 1 REC]
    [FPATH=FOLDER&"\"&REC]
    #Macro sets FSTR=Yes if true
    [HOTKEY "Is string in file"]
    [IF FSTR<>""]
        [RECOUT="String "&ASCII&SSTR&ASCII&" found in file: "&FPATH&"~"&" "&HREC]
        [WRITE 2 RECOUT]
        [N=N+1]
    [ENDIF]
[ENDLOOP]
[CLOSE 1]
[CLOSE 2]
#Display the results in Notepad (if any)
[IF N>0]
    [CMD="notepad.exe "&OUTFILE]
    [COMMAND CMD]
[ELSE]
    [MSG="String "&ASCII&SSTR&ASCII&" not found."]
    [YESNOCANCEL MSG ANS]
[ENDIF]

Macro [Hotkey "Is String In File"] statement

# Assume variables FSTR="YES" or "", USTR=search string, FPATH=file to search
[OPEN 3 FPATH]
[FSTR=""]
[LOOP Y=1-10000]
    [READ 3 HREC]
    [V=LEN(HREC)]
    [IF V>0]
        [SEARCH=1]
        [HR=UCASE(HREC)]
        [FINDSTRING HR USTR SEARCH]
        [IF SEARCH>0]
            [FSTR="YES"]
            [QUITLOOP]
        [ENDIF]
    [ENDIF]
[ENDLOOP]
[CLOSE 3]


Send Email

Press a hotkey to send an email message. This macro uses the default email application (e.g. Outlook Express) to set up an email. The user is prompted for the email address and message which is loaded into the email program, ready for sending. The mailto: keyword is key to launching the email application.

Note that a new line must be denoted by %0D%0A in the email message.

There are no setup conditions required to run this macro.

[USERINPUT "Enter an email address" EMAIL]
[CANCEL IF EMAIL=""]
[USERINPUT "Messge to send:" TEXTBOX]
[CANCEL IF TEXTBOX=""]
[T=TEXTBOX&"%0D%0A"&"This is the last line."]
[M="mailto:"&Email&"?subject=Re:Help"&"&body= "&T]
[COMMAND M]

 

Compare 2 Selected Text

The following macro compares 2 selected sections of text from any 2 documents. The macro samples the Windows clipboard contents until it detects 2 selected sections of text, then compares them. The results of the comparison are then loaded into Notepad.

#clear the clipboard, then regularly check it for new text
[SETCLIP ""]
[TOG=0]
[LOOP X=1-1000]
    [GETCLIP S]
    [IF S<>""]
        #option to send "quit" to the clipboard to stop the macro
        [CANCEL IF S="quit"]
        #when a section of text is detected in the clipboard, save it to a file
        [IF TOG=0]
            [TOG=1]
            [FILEA="C:\FILEA.TXT"]
            [SAVECLIP FILEA]
         [ELSE]
            [FILEB="C:\FILEB.TXT"]
            [SAVECLIP FILEB]
            #when 2 sections of text are detected, quit the loop and compare the files
            [QUITLOOP]
        [ENDIF]
        [SETCLIP ""]
    [ENDIF]
    [WAIT .5]
[ENDLOOP]
#To run a system command such as FileCompare (FC), prefix the command line with COMSPEC
[CMD="COMSPEC FC "&FILEA&" "&FILEB&" >c:\compare.txt"]
[COMMAND CMD]
[WAIT 1]
[COMMAND "notepad.exe c:\compare.txt"]

 

Replace Hotstrings

"Hotstrings" are words that trigger an event when typed in a document. This involves monitoring and recording keystrokes to test for hotstrings. When a hotstring is detected, macro code is activated to perform an action, such as replacing the hotstring with a word or phrase. Word replacement hotstrings allow you to create shorthand words that get replaced just after you type them into a document. Note that a hotstring can activate any programmed Hotkeys code.

The following macro program monitors your keystrokes and when a hotstring is detected, it is replaced with another word or phrase. This allows you to use shorthand words to input entire phrases into a document. The macro maintains a hotstring file that contains lines of hotstring/replacment pairs delimited by the first space on each line (no spaces allowed in the hotstrings). Each line must end with RETURN and the EOF is denoted by the "$" symbol on the last line. This is to ensure that no extra characters on the last line get mistaken for a hotstring. Here is a sample listing for file c:\tmp\HOTSTRINGS.LIS. Ensure that the c:\tmp folder already exists.

btw by the way
lol laughing out loud
fyi for your information
mya Joe Blow{~}1234 Lofty Hts.{~}Mytown, CA{~}94272-4931{~}USA
$

The "Replace Hotstrings" macro will monitor for keystrokes for approximately 3 hours before quitting. Quit the macro by entering the hotstring "quiths" or by clicking on the taskbar icon , or by pressing the ESC key. To add, delete or edit the hotstrings, enter the hotstring "ediths". This will load all the hotstrings into a textbox control and any changes will be saved to the hotstrings file. Note that editing the hotstrings is handled by another macro, i.e. "Edit Hotstrings" which is called by the Replace Hotstrings macro.

Macro Code

#Macro Replace Hotstrings
[HSFILE="C:\tmp\HOTSTRINGS.LIS"]
[OPEN 1 HSFILE]
[NRECS=0]
#Count hotstrings in file
[LOOP X=1-10000]
    [READ 1 REC]
    [C=MID(REC 1 1)]
    [IF REC="" OR C="$"]
        [QUITLOOP]
    [ENDIF]
    [NRECS=NRECS+1]
[ENDLOOP]
[CLOSE 1]
[IF NRECS=0]
    [Q="There were no hotstrings in file "&HSFILE]
    [YESNOCANCEL Q ANS]
    [CANCEL IF NRECS=0]
[ENDIF]
#Place the hotstring list into the ReadClip Reader
[READCLIPTEXT=""]
[LOADFILE HSFILE]
[CANCEL IF READCLIPTEXT=""]
[ASCII="13"]
[RC=READCLIPTEXT&ASCII]
[KLSAV=""]
#Reset the keylogger and monitor for hotstrings
[KEYLOG RESET]
[LOOP X=1-10000]
    [KEYLOG KL]
    [IF KL<>KLSAV]
        [KLSAV=KL]
        [N=1]
        [FPTR=1]
        #Test if keystrokes match a hotstring
        [LOOP N<=NRECS]
            [SEARCH=FPTR]
            [FINDSTRING RC ASCII SEARCH]
            [IF SEARCH=0]
                [TPTR=LEN(RC)]
            [ELSE]
                [TPTR=SEARCH-1]
            [ENDIF]
            [HSTRING=MID(RC FPTR TPTR)]
            [SEARCH=1]
            [FINDSTRING HSTRING " " SEARCH]
            [IF SEARCH=0]
                [QUITLOOP]
            [ENDIF]
            [SMO=SEARCH-1]
            [HS=MID(HSTRING 1 SMO)]
            [SEARCH=1]
            [FINDSTRING KL HS SEARCH]
            #If a hotstring is detected, replace it and reset the keylogger
            [IF SEARCH>0]
                [LHS=LEN(HS)]
                [SPO=SMO+2]
                [HSREP=MID(HSTRING SPO TPTR)]
                [SOUT="{BACKSPACE "&STR(LHS)&"}"&HSREP]
                [SENDKEYS SOUT]
                [KEYLOG RESET]
            [ENDIF]
            #Test for quit
            [SEARCH=1]
            [FINDSTRING KL "quiths" SEARCH]
            [IF SEARCH>0]
                [SENDKEYS "{BACKSPACE 6}"]
                [CANCEL IF SEARCH>0]
            [ENDIF]
            #Test for edit & launch Edit Hotstrings macro
            [SEARCH=1]
            [FINDSTRING KL "ediths" SEARCH]
            [IF SEARCH>0]
                [KEYLOG RESET]
                [HOTKEY "Edit Hotstrings"]
            [ENDIF]
            [FPTR=TPTR+3]
            [N=N+1]
        [ENDLOOP]
    [ENDIF]
    [WAIT 1]
[ENDLOOP]

Macro Code for [Hotkey "Edit Hotstrings"] statement

[READCLIPTEXT=""]
[HSFILE="C:\tmp\HOTSTRINGS.LIS"]
[PATHEXISTS HSFILE ANS]
#if the file doesn't exist, create it
[IF ANS="NO"]
    [OPEN 1 HSFILE]
    [WRITE 1 ""]
    [CLOSE 1]
[ELSE]
    #else load it into the ReadClip textbox
    [LOADFILE HSFILE]
[ENDIF]
#open a textbox with all the hotstrings for edit
[USERINPUT READCLIPTEXT TEXTBOX]
[CANCEL IF TEXTBOX=""]
[READCLIPTEXT=TEXTBOX]
#save the changes
[SAVEFILE HSFILE]

 

Newest Version

This Hotkeys macro determines the newest versions of the files from 2 folders. If you have multiple copies of files, such as a software project, it is important to establish which files have been modified most recently. These files represent the newest versions based on their "last modified" file dates. The macro prompts for two folders containing same named files, then compares their date differences and loads the results into Notepad. A negative date difference indicates that the file in folder 1 is a newer version than in folder 2. A positive value indicates that the file in folder 2 is a newer version than in folder 1.

There are no setup conditions required to run this macro.

#select the folder to compare
[USERINPUT "dialog+Compare Folder+Folder+++" FOLDERB]
[CANCEL IF FOLDERB=""]
#select the files to compare in another folder
[FSEL=""]
[FINDFILES "" FSEL]
[CANCEL IF FSEL=""]
#determine folder names
[ASCII="13"]
[CR=ASCII]
[ASCII="10"]
[CRLF=CR&ASCII]
[L=LEN(FSEL)]
[LL=L-1]
[RC=MID(FSEL LL L)]
[IF RC<>CRLF]
    [FSEL=FSEL&CRLF]
[ENDIF]
[NA=0]
[CRLFSAV=-1]
[L=LEN(FSEL)]
[I=0]
[CPOS=1]
[FINDSTRING FSEL CR CPOS]
[IF CPOS=0]
    [YESNOCANCEL "Error setting folder - Aborting" ANS]
    [CANCEL IF CPOS=0]
[ENDIF]
[CPOS=CPOS-1]
[FOLDERA=MID(FSEL 1 CPOS)]
[LA=LEN(FOLDERA)]
[CPOS=-1*LA]
[FINDSTRING FOLDERA "\" CPOS]
[IF CPOS=0]
    [YESNOCANCEL "Error setting folder - Aborting" ANS]
    [CANCEL IF CPOS=0]
[ENDIF]
[CPOS=CPOS-1]
[FOLDERA=MID(FOLDERA 1 CPOS)]
#write the folder names/order to an output file
[OPEN 2 "c:\FC.TXT"]
[WRITE 2 FOLDERA]
[WRITE 2 FOLDERB]
#parse out the filenames and compare their file dates
[LOOP I<L]
    [I=I+1]
    [M=MID(FSEL I I)]
    [IF M=CR]
        [CL=CRLFSAV+2]
        [CLL=I-1]
        [FILEA=MID(FSEL CL CLL)]
        [LA=LEN(FILEA)]
        [CPOS=-1*LA]
        [FINDSTRING FILEA "\" CPOS]
        [IF CPOS>0]
            [C=CPOS+1]
            [CC=LEN(FILEA)]
            [FB=MID(FILEA C CC)]
            [FILEB=FOLDERB&"\"&FB]
            [PATHEXISTS FILEB ANS]
            [IF ANS="NO"]
                [FF="Missing "&FB]
                [WRITE 2 FF]
            [ELSE]
                [DATEDIFF FILEA FILEB F]
                [FF=STR(F)&" "&FB]
                [WRITE 2 FF]
            [ENDIF]
        [ELSE]
            [FF="Missing "&FILEA]
            [WRITE 2 FF]
        [ENDIF]
        [CRLFSAV=I]
    [ENDIF]
[ENDLOOP]
[CLOSE 2]
[CMD="notepad.exe c:\FC.TXT"]
[COMMAND CMD]

 

Run Excel Macro

The following Hotkeys macro reads from a list of files, loads each file into Excel, runs an Excel macro that modifies, saves, then closes each file. An XLS file containing the (single) macro must be preloaded.

Note that keystrokes sent to the active window need not be in a SENDKEYS statement, but they must not include quotes and they must not be preceded by spaces (which would also be sent to the active window). Non-printable characters must be enclosed by parentheses, e.g. {TAB}. They must be placed on a separate line which does not contain [brackets].

#file to contain the XLS filenames
[OUTFILES="E:\FDI\DIRLIS.TXT"]
#place search results into DIRLIS.TXT
[FINDFILES "E:\FDI\" OUTFILES]
[OPEN 1 OUTFILES]
#set loop to large number (EOF quits the loop)
[LOOP X=1-1000]
    [READ 1 REC]
#open dialog and enter the filename (REC)
^o
    [SENDKEYS REC]
{ENTER}
#activate the macro
^m
    [WAIT 1]
 #save the changes
^s
    [WAIT 1]
 #close the file
{F10}{DOWN}{DOWN}{DOWN}{ENTER}
    [WAIT 1]
[ENDLOOP]
[CLOSE 1]

 

Back