AppleScript Programming/System Events

From Wikibooks, the open-content textbooks collection

Jump to: navigation, search

One of the many things that can be used with Applescript is System Events. It is always first executed by the "tell" command.

 tell application "System Events"

This will tell applescript to execute commands associated with the program System Events. For example, the command:

 keystroke "e"

Will literally type the letter "e".

Here's a simple program:

tell application "TextEdit"
	activate
	make new document
end tell
tell application "System Events"
	keystroke "Support Wikibooks!"
end tell

It will open the application TextEdit, start a new document, and type the phrase "Support Wikibooks!"

You can also use keystroke tab (note that there are no quotes) to type a tab, and keystroke return to enter a new line.

tell application "TextEdit"
	activate
	make new document
end tell
tell application "System Events"
	keystroke "Support Wikibooks!"
	keystroke return
	keystroke "by donating!"
end tell

Which will create a new document with Support Wikibooks! and by donating, on another line.