Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

plasticparadox

macrumors 6502
Original poster
May 24, 2003
484
1
I want to make a script that will
a) open System Preferences and the Exposé pane
b) disable the Exposé and Dashboard shortcut keys
c) close System Preferences
d) launch a game
e) upon the game quitting, relaunch System Preferences
f) re-enable shortcut keys
g) close System Preferences

If possible, I want to make the System Preferences part of the script hidden. I've put together a bit of a script that covers opening System Preferences, and I'm sure I can figure out how to close programs as well.
Are there any AppleScript gurus who can give me examples of how to interact with drop-down menus and also how to activate the next part of the script upon the game's closure? Game is Deus Ex under Classic, if it matters.
 

HexMonkey

Administrator emeritus
Feb 5, 2004
2,240
504
New Zealand
Here's some code that can be used as a base, which should detect when the game is quit (by checking every 10 seconds). To use it, you will have to save the script as an application, with the "Stay Open" option checked. You will need to modify the disableExposeAndLaunchApp() and enableExpose() functions.

Code:
property appName : "Deus Ex"
property isLoading : true

on disableExposeAndLaunchApp()
	--Put code to disable Exposé here, then launch the game
end disableExposeAndLaunchApp

on enableExpose()
	--Put code to re-enable Exposé here
end enableExpose

on run
	set isLoading to true
	disableExposeAndLaunchApp()
	return 10
end run

on idle
	tell application "System Events" to set isOpen to exists (first process whose name is appName)
	if not isOpen and not isLoading then
		--appName has just quit
		enableExpose()
		quit
	else if isOpen then
		--appName is open
		set isLoading to false
	end if
	return 10
end idle

To handle disabling/enabling Exposé and Dashboard, you will need to use GUI scripting, which is essentially giving commands that replicate a user's actions. Unfortunately, this method is slow, entirely visible to the user, and not backwards or future compatible, as it will have to be changed each time the interface changes. Also, you must have the "Enable access for assistive devices" option checked in Universal Access for it to work.

Below is an example that turns off the Exposé and Dashboard shortcuts in Tiger. It should be relatively simple to write the code to re-enable them from it. If you find that some actions are being skipped, you will have to increase the delays.

Code:
tell application "System Preferences" to activate
tell application "System Events"
	tell process "System Preferences"
		try
			--First open the "Dashboard & Exposé" pane
			click menu item "Dashboard & Exposé" of menu "View" of menu bar 1
			delay 2
			tell group 2 of window "Dashboard & Exposé"
				--Disable each shortcut individually
				click pop up button 1
				click last menu item of menu of pop up button 1
				
				delay 0.5
				click pop up button 2
				click last menu item of menu of pop up button 2
				
				delay 0.5
				click pop up button 3
				click last menu item of menu of pop up button 3
				
				delay 0.5
				click pop up button 4
				click last menu item of menu of pop up button 4
			end tell
		on error theError
			--An error occured
			display dialog ("Sorry, an error occured while altering Exposé settings:" & return & theError) buttons "OK" default button "OK"
		end try
	end tell
end tell
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.