General AppleScript question

I am looking for a way to interface directly with a running application and send keystrokes, clicks, etc into the application. I am putting together a kind of how-to type thing where the script will open the program, type in some things, select a menu, show how to highlight, etc etc. Is this possible with AS or should I look elsewhere?

Generally speaking, yes. It’s called GUI scripting and the dictionary for “System Events” contains much of what can be done.

Would you know a good place to start reading up on how to begin? AS is pretty new to me, so I need a crash course :slight_smile:

BTW, Thanks for the quick response

There are lots of books and references listed here but for a quick start, I recommend Danny Goodman’s eBook: AppleScript Handbook, Mac OS X Edition

If you search these forums (the AppleScript | OS X, anyway) for GUI scripting, you’ll find lots of examples. You don’t say what your app is, so I can’t say how easy it will be to script. GUI scripting only works if you have “Enable access for assistive devices” checked in the Univeral Access pane of System Preferences, or run the script below (requires admin password) to turn it on:

tell application "System Events" to set (UI elements enabled) to true

Beyond that, we need to know a lot more about what you want to accomplish in order to help.

Well the idea of the app is really for a game (World of Warcraft). There are a lot of Windows tools that can open the game, process inventories, and character data files which can then be uploaded to web sites for characters to share. The problem is there is nothing for the Mac and the game has a pretty good following.

So in getting there, I basically want to:

  • open the application
  • send in my login information
  • send in various keystrokes to open my bags, click things, etc

For me, this is my how-to lesson… after I get that going I will start trying to go a bit deeper, but I figured if I start with this it will give me an understanding of how to attach to the running program and issue commands and see if I can tell what the results are.

Does that make more sense?

I think games are specially difficult to script with System Events, but it’s worth a try. This is a sample using Safari:

launch application "Safari"

tell application "System Events"
	repeat while "Safari" is not in name of processes
		delay 1
	end repeat
	tell process "Safari"
		set frontmost to true
		delay 1
		keystroke "n" using {command down} --> new window
		delay 1
		keystroke "l" using {command down} --> focus location, just in case
		delay 1
		repeat with x in ""
			delay 0.1
			keystroke x
		end repeat
		delay 1
		keystroke return
		delay 3
		keystroke "w" using {command down}
	end tell
end tell

As you see, the basic code is pretty easy and self-explaining. You can emulate clicks using the command “click at”, followed by the coordinates. Ie: click at {124, 548}

But you may need something more sophisticated to make work the clicks some times. I use then XTool (look for it at

I have actually already gotten so far just from this site, so thanks so much. This was just the icing on the cake.

All the best.