1

I am on macOS El Captain (upgrading to High Sierra soon) and I'm looking for a tool which can trigger an AppleScript or shell script every time when the number of monitors/displays attached to the system changes.

I have tried ControlPlane, but could not get the trigger on attached-display-change to work. Two questions:

1) Does anybody know that display triggering in ControlPlane definitely works on macOS El Captain and higher? (Indicating that I am doing something wrong.)

Or:

2) Is there another tool which can act on a change of numbers of displays every time a monitor/display is (dis-)connected?


PS: I am aware there are other questions in the StackExchange network relating to this topic (like How can I run a script whenever I plug in an external monitor? or Reset Mac OS X Windows Position after de-attaching external monitor), but the answers do not seem to apply to El Captain/High Sierra .

1

There may be a more efficient way but if you save this following AppleScript code in Script Editor, as an application, after customizing it to your needs… running this application, as currently configured, will continue to run and check every five seconds, until an additional monitor is detected.

property displayCount : 1

repeat until displayCount is greater than 1
    tell application "Image Events"
        set theDisplays to count of displays
    end tell
    set displayCount to theDisplays
    delay 5 -- How Often To Check How Many Connected Monitors.. In Seconds
end repeat

-- The Following Line Will Execute When An Additional Display Is Connected
-- Replace The Following Code With Whatever Actions You Choose

activate
display dialog "New Display Connected" buttons {"Cancel", "OK"} default button "OK"

-- OR use the "run script" command as in the sample below

--set theScript to (path to desktop as text) & "whatever.scpt"
--set runScript to run script alias theScript

return

This next option will detect whether a monitor is connected or disconnected and will continue running

property displayCount : missing value
property tempDisplayCount : missing value

countDisplays()

repeat
    repeat until displayCount is greater than 1
        countDisplays()
    end repeat
    displayConnected()
    countDisplays()
    copy displayCount to tempDisplayCount
    repeat until tempDisplayCount is not equal to displayCount
        countDisplays()
    end repeat
    copy displayCount to tempDisplayCount
    if tempDisplayCount is greater than displayCount then
        displayConnected()
    else if tempDisplayCount is equal to displayCount then
        displayDisconnected()
    end if
end repeat

on displayConnected()
    -- The Following Lines Will Execute When An Additional Display Is Connected
    -- Replace The Following Code With Whatever Actions You Choose
    -- OR use the "run script" command as in the sample below
    -- set theScript to (path to desktop as text) & "whatever.scpt"
    -- set runScript to run script alias theScript
    activate
    set newDisplayConnected to button returned of (display dialog "New Display Connected" buttons {"Stop Monitoring", "Continue Monitoring"} default button "Continue Monitoring")
    if newDisplayConnected is "Stop Monitoring" then
        quit me
    end if
end displayConnected

on displayDisconnected()
    -- The Following Lines Will Execute When A Display Is Disconnected
    -- Replace The Following Code With Whatever Actions You Choose
    -- OR use the "run script" command as in the sample below
    -- set theScript to (path to desktop as text) & "whatever.scpt"
    -- set runScript to run script alias theScript
    activate
    set newDisplayDisconnected to button returned of (display dialog "A Display Was Disconnected" buttons {"Stop Monitoring", "Continue Monitoring"} default button "Continue Monitoring")
    if newDisplayDisconnected is "Stop Monitoring" then
        quit me
    end if
end displayDisconnected

on countDisplays()
    tell application "Image Events"
        set theDisplays to count of displays
    end tell
    set displayCount to theDisplays
    delay 5 -- How Often To Check How Many Connected Monitors.. In Seconds
end countDisplays

enter image description here enter image description here

  • 1
    Looks pretty good. Though I am a bit worried about a never-ending AppleScript, but this one should be not too taxing on the system. – halloleo Aug 15 '18 at 3:14
  • I need the script to continue running in order to detect the next change. - Will update the question accordingly. – halloleo Aug 15 '18 at 21:35
  • @halloleo . Did you notice I updated the code for you?Is this more along the lines of what you are looking for? – wch1zpink Aug 17 '18 at 15:08
  • Thanks @wch1zpink. The second script is exactly applicable to the (updated) question. :-) – halloleo Aug 18 '18 at 0:19
  • 1
    Done, @wch1zpink. – halloleo Aug 19 '18 at 15:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .