Trigger Applescript on System Event

First off, even though I am on Mac OS since X came out, I am fairly new to applescript.

I wonder if it is possible to trigger applescript(s) on certain System Events, similar to how Folder Actions work.

My scenario is as follows: I use spaces and map different applications to different spaces. My Dock is usually at the bottom, but on one specific Space I want the dock to switch to the right.

I figured out the applescript to switch the dock, but how can I make it trigger when the space changes?

Thanks a lot for any input


AppleScript is not able to respond to space changes.
You could use a hotkey tool like FastScripts or Quicksilver to switch the space and the location of the dock

Thank you Stefan

this is the answer I was afraid of :wink:


Accepting one shortcut key to update the dock position and one to switch with when you consciously switch to a space and are not “moved” to a space :slight_smile: Is the realisitic option here.

And two shortcut keys are fare better than no solution to your problem.

If you really want to use your resources and system resources on this, you can write and applet which tracks changesof the space you are in at regular intervals and act acoordingly. All the code for doing this can be found at the site. Try to find the topic that contains this handler, I haven’t the time at the moment.

on get_my_space_number() – the extras menu is localized, so check for the names of the values in debugger.
set xxVar to 0
tell application “System Events”
tell process “SystemUIServer”
set xVar to value of attribute “AXChildren” of menu bar 1
set cVar to count of xVar
repeat with iVar from 1 to cVar
set zVar to value of attribute “AXDescription” of item iVar of xVar
if zVar = “spaces-menu-extra” then
set xxVar to iVar
exit repeat
end if
end try
end repeat
end tell
end tell
if xxVar = 0 then
display dialog “Spaces Menu Extra not installed”
tell application “System Events”
tell process “SystemUIServer”
set theCurrentSpace to value of menu bar item xxVar of menu bar 1
end tell
end tell
return theCurrentSpace as number
end if
end get_my_space_number

– the code over is what you could use in an idle handler to check if you are in a special space and act

The nicest thing would be if you could have tracked changes of some property file, but the only way
I know to get data out of your system regarding which space so far is to extract from the extras menu, and that could have been done. FYI the strings and values in the extras menu are localized.

I’ strongly believe that the easiest, most resource constraining solution for both you and your machine is to use two shortcuts.

There should have been some window events/actions together with the folder actions, and all running at higher priorities. But unfortunately this isn’t so.


you are right, the idea of busy polling does not appeal at all. OS X seems to be so nicely event driven that the lack of an applescript hook for this is surprising. Maybe this can be solved in a scripting addition.

There are only a few events AppleScript can respond to.

folder action events in System
receiving mails in
some events in