Use AppleScript to Get Ahead in Web-Based Games
Posted 08/15/2008 at 1:25am
| by Jason Whong
3. Control Safari

New text appears in purple. The Compile button will parse the code and turn the known classes and commands (that is, the nouns and verbs) blue. The parameters will turn black.
In this example, we’ve found a URL at a Mafia game (www.thegrpg.com) that lets us try to gain money and experience by pickpocketing a granny. Using AppleScript to visit that URL is pretty easy. To start writing scripts, launch Script Editor (usually in /Applications/Utilities/Script Editor), which opens a blank Script window. Ignore the Record button because it doesn’t notice the URLs you visit in Safari. Instead, we’ll have to do this the old-fashioned way: typing the script ourselves.
Type the following:
tell application “Safari”
set URL of tab 1 of window 1 to “http://www.thegrpg.com/crime.php?id=1”
end tell
If you’ve found a different URL, use it between the quotes. To make sure you typed it correctly, click the Compile button. This will fix the capitalization of the words for you automatically, indent the code so that it’s easier to read, change the color of some of the words, and complain if you made any errors.
Because AppleScript looks a lot like plain English, it’s pretty clear what this script does, as long as you know what a URL, a tab, and a window are. (If you end up writing more AppleScripts in the future, you’ll probably end up doing a lot with Tell and Set.)
Log in to the game. Click the Run button in Script Editor to collect your money and experience points, and an old lady’s wallet.
4. Repeat
There’s no real benefit to using the script we wrote in step 3. We could do the same thing by bookmarking the URL, or clicking the Reload button. The script also lets you pick someone’s pocket without going to the crime menu, which a human player wouldn’t do.
Suppose we figure that we can run the previous script five times before we reach the game’s limit for that action. The evil way to automate this would be to just add a repeat loop, but that would load the page five times really quickly, as if a robot were playing. AppleScript can pretend to be a real person. Modify your script so that it looks like this:
tell application “Safari”
set the URL of tab 1 of window 1 to “http://www
.thegrpg.com/crime.php”
delay (5 + (random number from 1 to 6))
repeat 5 times
set the URL of tab 1 of window 1 to “http://www.thegrpg.com/crime.php?id=1”
delay (5 + (random number from 1 to 6))
end repeat
end tell
It’s pretty easy to tell what we’ve added. First, we’re sending Safari to the game’s crime menu, which is where players find the list of crimes to attempt. The repeat loop tells AppleScript to attempt the pickpocketing five times. The delay statements tell AppleScript to wait a bit before moving on. Ordinarily, you could just type “delay 15” to have it wait 15 seconds, but that’s a little too precise to be humanlike. Instead, we’re having AppleScript wait anywhere from 6 to 11 seconds (or, 1d6+5 seconds for all you tabletop RPG geeks out there). Click the Run button to snatch five wallets in four fewer steps than other players would take to do the same thing.
5. Make the Script Smart

We committed a different crime so we could get sent to jail. Now we know what the error message looks like, and we can tell Safari to look for it.
Another drawback to our script is that in this game, certain “status changes” prevent our character from performing actions. For example, if we fail at committing a crime, our character goes to jail. We can’t pickpocket old ladies from there, but if our script keeps trying to, we might attract unwanted attention.
Fortunately, this game has only one error message that appears when a jailed player tries to perform an action, and it begins, “You are currently in jail.” We can use AppleScript to trap this message and stop sending unnecessary commands.
Add this line after the delay statement in the repeat loop. (Replace “currently in jail” with whatever words are in your error message.)
if text of tab 1 of window 1 contains “currently in jail” then exit repeat
It’s also easy to tell what AppleScript is looking for here. The text of the tab is the text we can see on the webpage, and if it contains the error message we’re looking for, AppleScript will know to exit from the repeat loop. Otherwise, it’ll just keep going. If there are other error messages you know you may run into, copy the “if text of tab 1…” statement you added above and paste it in again right after the first one, then change the part inside the quotes to match the contents of the other error messages.
Now the script quits after the first “in jail” error message, which is a bit more human than a script that blindly performs actions despite repeated error messages.
6. Repeat Again
A true evil genius will set up AppleScript to play the game while nobody is sitting at the Mac. This is easy to do by enclosing the repeat loop inside another repeat loop:
tell application “Safari”
set the URL of tab 1 of window 1 to “http://www.thegrpg.com/crime.php”
delay (5 + (random number from 1 to 6))
repeat 24 times
repeat 5 times
set the URL of tab 1 of window 1 to “http://www.thegrpg.com/crime
.php?id=1”
delay (5 + (random number from 1 to 6))
if text of tab 1 of window 1 contains “currently in jail” then exit repeat
end repeat
delay (1530 + (random number from 120 to 240))
end repeat
end tell
This script will repeat the inner repeat loop 24 times. At the end of the inner loop, it’ll wait about 25 to 30 minutes, then repeat the inner loop again. After it has repeated the inner loop 24 times, the script will stop asking Safari to do anything. This simulates a player who checks in frequently over a span of about 12 hours.
You could make the script repeat forever by removing the “24 times” from the outer repeat statement, but that simulates a player who doesn’t need to sleep (the kind who eventually draws enough criticism that scripting is banned).
One gotcha with unattended play is that some games won’t keep you logged in for a long time. If you’re not logged in, all of the actions will fail. We know that the longest possible delay in this script is within the currently allowed idle time, but if the game administrator shortens that time, he can break this script.