

Who says crime doesn’t pay? Our final script pickpockets old ladies when we’re not even at our Mac.
Recently, we’ve gotten into a few Web-based role-playing games that remind us of the ones we used to play on old-school dial-up BBSes. In these games, players choose actions from a menu, sometimes selecting other players as targets for the actions. Before the Web, we played by typing. Now we click links, but the style of play is still the same.
Some of these games have tens of thousands of users, many of whom will log in multiple times per day and grind away repetitively on some basic actions so they can level up faster than you. Fortunately, AppleScript was made for automating repetition, and Safari’s support for AppleScript means that with a modicum of evil genius scripting know-how, your Mac can gain those levels for you while you’re out enjoying real life.
What You Need: Mac OS 10.5, Safari 3, a user account on a Web game, and a copy of the rules for that Web game, if any exist
1. Think Ethics

Rule No. 1 on My Heroes Ability, a popular game on Facebook, expressly prohibits scripting. Looks like we’ll have to pick a different game for this example.
While scripting lets us play evil genius, we’re also going to play an ethical one, because the Web is made up mostly of other people’s servers. If you don’t want to get kicked off, it’s smart to know whether this sort of conduct is prohibited on the Web game before you use a script.
Go to your Web game’s site and look for the rules (they may be under “rules,” “terms of service,” or “acceptable use policy”). Or check out the game developer’s blog or the game’s forums if you don’t find anything on the main site. If the developer has expressed that any form of scripting is cheating, you know not to do it. Though unlikely, a litigious system administrator could ruin your fun if he catches you breaking the rules.
If you don’t see any prohibitions, proceed, but consider how your script will affect the server. Any use of the server, by humans or by scripts, slows it down somewhat. Following the instructions here should minimize the impact on the server. The more humanlike your script behaves, the less likely anyone will notice or care that a script is playing your character. If the rules are changed later to prohibit scripting, stop using AppleScript immediately.
2. Analyze the Game

When we’re logged into Generic RPG (www.thegrpg.com), every time we visit this URL our character attempts to snatch an old lady’s wallet.
Most Web games we play automatically with AppleScript have one thing in common: They’re really just a set of webpages that use server-side scripts to make changes to a database. When you visit a certain URL, the server runs the script and decides how to update the database, usually in less time than it would’ve taken you to roll a 20-sided die.
Consider what’s really going on inside the server as you play the game in Safari for a session or two. Look closely at the status bar at the top of the window: After a while, you may notice that some beneficial actions are tied to specific URLs on the server. Copy those URLs to your favorite text editor for safekeeping. Look also to see whether you must first visit one page before visiting another.
Many games limit your ability to perform actions. The limits may be spelled out in the game’s instructions, or you may have to experiment to learn them. The idea is not to circumvent the limit; rather, it’s to perform the actions as often as you can within the game rules. Try also to find out how long the game will let you stay logged in without doing anything.
Finally, don’t waste time analyzing the parts of the game that are form-based (meaning, the kind that require you to click a button on the page). Odds are, the game developer is checking whether you’re really submitting a form using the POST method, which AppleScript can’t handle.
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.