Question

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 .

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with apple.stackexchange
scroll top