R – way to hook in to OSX sleep/wake events via Applescript

applescriptmacososx-snow-leopard

The problem I am trying to solve is quite simple.

When I open the lid of my MacBook I like to have the Dock on the left side of the screen, but when I get home and connect my MacBook to my Cinema display and set up dual monitors, I want the dock on the bottom of the Cinema, not on the left side of the MacBook.

I don’t want to have to go in to preferences every time I connect/disconnect my Cinema display.

I have a solution for 50% of the problem, namely, I have written the following Applescript code to switch the location of the Dock based upon my screen resolution, but I have to manually invoke it.

tell application "Finder" 
-- Determine Resolution
  set screenSize to bounds of window of desktop
  set screenWidth to item 3 of screenSize
  set screenHeight to item 4 of screenSize
end tell
if screenWidth is less than 1900 then    
    --MacBook Display    
    tell application "System Events"      
       tell dock preferences            
            set properties to {magnification:true, screen edge:left}
       end tell
    end tell
else
    --Cinema Display
    tell application "System Events"
        tell dock preferences
            set properties to {magnification:true, screen edge:bottom}
        end tell
    end tell
end if

My questions are:

  • Can I hook this code in to OSX sleep/wake events directly within Applescript? Is there some place where you can "register" scripts to be invoked on OS events that I am not aware of?

  • If not directly, what are some other options for hooking in to OSX events?

  • Do you have any recommendations for
    improving the above Applescript code?

Thanks.

Best Answer

Try executing your script via Sleepwatcher: http://www.bernhard-baehr.de/

Related Topic