External monitor and wake from sleep triggers

I have several scripts that I use to arrange my desktop and dock, but how can I intercept the above events to create an (apple-)script trigger…?

Hi Eelco,

SleepWatcher 2.0.4 can trigger UNIX commands, but also AppleScripts via the osascript command.
If it’s sufficient, to detect the external monitor at startup, use Image Events

tell application "Image Events"
	set {DispNames, Cnt} to {name of display profile of displays, count displays}
end tell

I found SleepWatcher a bit of a pain to use, so I’ve adapted MacChampion’s Scenario, a preference pane that lets you run scripts kept in it’s folders at log-in & out, Sleep, and Idle. You can also assign hot keys to them. The scripts are just “ordinary” AppleScript.scpt files. Very useful, and so far, completely reliable.

Stefan and Adam,

Thanks, that helps.
I will check out Scenario.

When it comes to Stefan’s Image Events script, it does its work, but I was actually looking for a trigger/handler that intercepts the plugging in (/-out) of an extra monitor and then runs a script like that.

I know, there are two options:

¢ a stay open script with an idle handler, which checks periodically the number of displays
¢ a launchd agent, which does the same, but invisible in the background

AFAIK there is no way, to get a trigger, when the status of the displays has changed