AppleScript help

Discussion in 'Mac Programming' started by plasticparadox, Jun 28, 2005.

  1. plasticparadox macrumors 6502

    May 24, 2003
    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.
  2. HexMonkey Administrator


    Staff Member

    Feb 5, 2004
    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.

    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
    	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
    	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.

    tell application "System Preferences" to activate
    tell application "System Events"
    	tell process "System Preferences"
    			--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

Share This Page