Có thể có một cách hiệu quả hơn nhưng nếu bạn lưu mã AppleScript này theo Script Editor, dưới dạng một ứng dụng, sau khi tùy chỉnh nó theo nhu cầu của bạn, chạy ứng dụng này, như được cấu hình hiện tại, sẽ tiếp tục chạy và kiểm tra cứ sau 5 giây, cho đến khi màn hình bổ sung được phát hiện.
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
Tùy chọn tiếp theo này sẽ phát hiện xem màn hình được kết nối hay ngắt kết nối và sẽ tiếp tục chạy
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