Keolo Blog

AppleScript GUI Scripting

AppleScript is sometimes useful for automating repetitive tasks on macOS.

But some applications are not scriptable or provide just a few scriptable commands.

In these cases you can use GUI scripting. GUI scripting works with any application. It simulates mouse clicks and key presses.

For example, open AppleScript Editor (in /Applications/Utilities) and type:

tell application "System Events"
	tell process "TextEdit"
		set frontmost to true

		keystroke "Hello"

		click menu item "Print…" of menu 1 of menu bar item "File" of menu bar 1
		repeat until sheet 1 of front window exists
		end repeat
		keystroke "5"
	end tell
end tell

If it doesn’t work, make sure GUI scripting isn’t disabled: quit AppleScript Editor, open System Preferences, click Security & Privacy, click Privacy, click Accessibility, make sure the AppleScript Editor checkbox is selected, quit System Preferences, open AppleScript Editor again.

Useful commands include:

← Read other blog posts