AppleScript Programming/Advanced Code List/Hide Application

From Wikibooks, open books for an open world
Jump to: navigation, search

Sometimes, your script will need to do something that doesn't have an AppleEvent defined to accomplish the task. In this case, you may be able to script the task anyway by going through the graphical user-interface (GUI), using a technique called GUI Scripting.

For example, to hide frontmost application, you can do the follwing:

tell application "System Events" to keystroke "h" using command down

Then, to hide all background programs:

tell application "System Events" to keystroke "h" using {command down, option down}

But when you want to hide a specific application that is at background, it gets more tricky:

tell application "System Events" to tell process "Safari" to set visible to false

Safari is just an example here.

I use iKey to run this script once Adium is deactivated:

tell application "Adium"
	set vCounter to count every window
	if vCounter < 2 then tell application "System Events" to tell process "Adium" to set visible to false
end tell

Shortly explained, it hides Adium if there is only one window (like, buddy-list) open.