TSolo315 MturkCrowd
Six years of turking experience

Clipboard and StrReplace

Copy and alter text

1

Clipboard


As we have discussed in earlier tutorials, clipboard is a built in variable tied to the current contents of your clipboard (content you have copied). Here is a refresher on several methods to alter clipboard contents:


clipboard := "my text"   ; Give the clipboard entirely new contents.
clipboard := ""   ; Empty the clipboard.
clipboard := clipboard " Mturk AHK Script"   ; Append some text to the clipboard.
clipboard := StrReplace(Clipboard, "apple", "orange") ; Replaces all instances of apple in a string in your clipboard with orange.

These methods should make more sense to you after finishing the variables tutorial. In essence, you are simply editing the content of a variable. The clipboard variable is special because it has the additional effect of also editing the contents of your computer clipboard.

2

Clipwait


There is a short amount of time required for your computer to copy something to the clipboard. If you don't add a wait time, it's possible for the script to continue before the content is properly copied. Using the clipwait command improves script reliability by forcing the script to wait until the content is copied before continuing. It's good practice to use the following whenever copying content with a hotkey:


clipboard := "" ; Empty the clipboard. This allows clipwait to work, as it checks to see if text has been added.
sendinput ^c ; Copy highlighted text.
clipwait, 5 ; Wait for text to be copied to clipboard. You can add an options number (5 in this case) as a maximum waiting time. If the waiting time exceeds this AHK will change ErrorLevel status.

3

StrReplace


StrReplace replaces the specified substring with a new string. This can be used to alter the text content of any variable, including clipboard. The format is as follows: myNewVar := StrReplace(myOldVar, "text to replace", "text to replace it with"). myNewVar is the name of the variable the result will be stored in, and myOldVar is the name of the source variable. These can both be the same, meaning the edited result will be saved to the source variable. A few examples:


clipboard := StrReplace(clipboard, "apple", "orange") ; Replaces all instances of apple in a string in your clipboard with orange.
AlteredText := StrReplace(clipboard, "surveys", "batches") ; Takes the contents of your clipboard, replaces all instances of the word surveys with the word batches, and saves the result in a new variable called AlteredText.
AlteredText := StrReplace(AlteredText, "clicking", "scripting") ; Replaces all instances of the word clicking found in the AlteredText variable with the word scripting.

You can also use StrReplace to remove a substring by only providing the substring you would like removed.


clipboard := StrReplace(clipboard, "surveys") ; Removes all instances of the word surveys from your clipboard.

4

Special Characters


In AHK spaces and tabs have unique markup that can used when attempting to edit them. A_Space is a special variable containing a single space character, and A_Tab is a special variable containing a single tab character. If you wanted to replace all spaces in a string in your clipboard with dashes (-), you can use A_Space. clipboard := StrReplace(clipboard, A_Space, "-"). Because A_Space is a variable it is not surrounded by quotation marks like a string (text) would be. You can use variables in place of strings if the variable is given a string value. This can be useful if you have to use a certain string multiple times as it allows you to assign the string to a variable once and then simply use that variable in place of the string whenever necessary. The same effect could be achieved without the A_Space variable by using a string containing a single space: clipboard := StrReplace(clipboard, " ", "-").

5

Removing Line Breaks


When copying multiple lines of text it's often necessary to remove the line breaks to avoid AHK sending them as enter presses when using the sendinput command. This isn't necessary when only altering the clipboard but is important when using self-defined variables. You can use StrReplace to remove line breaks:


myVar := StrReplace(myVar, "`r`n")
myVar := StrReplace(myVar, "`r")
myVar := StrReplace(myVar, "`n")

6

Practical Example



^9::
click 2 ; Double click.
clipboard := "" ; Empty the clipboard.
sendinput ^c ; Copy highlighted text.
clipwait, 5 ; Wait for text to be copied.
clipboard := StrReplace(clipboard, " at ", "@")
clipboard := StrReplace(clipboard, "-at-", "@")
clipboard := StrReplace(clipboard, "_at_", "@")
clipboard := StrReplace(clipboard, "  at  ", "@")
clipboard := StrReplace(clipboard, "-@-", "@")
clipboard := StrReplace(clipboard, "_@_", "@")
return

If you were working on a batch that involved collecting email addresses you may find that many websites attempt to obscure these addresses (to avoid them getting picked up by bots). This script will replace several variations of an obscured @ character with the plain @ character.

Next Tutorial: Expressions