Skip to content

Instantly share code, notes, and snippets.

@ruseel
Created September 13, 2025 03:00
Show Gist options
  • Select an option

  • Save ruseel/0d3e3eada9707a1b6a8d66e21a5544cd to your computer and use it in GitHub Desktop.

Select an option

Save ruseel/0d3e3eada9707a1b6a8d66e21a5544cd to your computer and use it in GitHub Desktop.
macOS Monitor Identifier - Uses NSScreen API and CGDisplaySerialNumber for consistent monitor identification
#!/usr/bin/osascript
(*
Monitor Identifier for macOS
Uses NSScreen API and CGDisplaySerialNumber for consistent monitor identification
Use NSNumber instead of NSString format because CGDisplaySerialNumber returns
a uint32_t that gets converted to floating-point in AppleScript,
NSNumber's stringValue properly converts the floating-point back
to integer string.
*)
use framework "Foundation"
use framework "AppKit"
use framework "CoreGraphics"
use scripting additions
property NSScreen : a reference to current application's NSScreen
property NSString : a reference to current application's NSString
property NSNumber : a reference to current application's NSNumber
on run argv
return getMonitorsJSON()
end run
on getMonitorsJSON()
set monitors to getMonitorData()
set jsonArray to "["
set isFirst to true
repeat with monitor in monitors
if not isFirst then set jsonArray to jsonArray & ","
set isFirst to false
set jsonArray to jsonArray & "{"
set jsonArray to jsonArray & "\"displayID\":" & (displayID of monitor) & ","
set jsonArray to jsonArray & "\"width\":" & (width of monitor) & ","
set jsonArray to jsonArray & "\"height\":" & (height of monitor) & ","
set jsonArray to jsonArray & "\"x\":" & (x of monitor) & ","
set jsonArray to jsonArray & "\"y\":" & (y of monitor) & ","
set jsonArray to jsonArray & "\"serialNumber\":" & (serialNumber of monitor)
set jsonArray to jsonArray & "}"
end repeat
set jsonArray to jsonArray & "]"
return jsonArray
end getMonitorsJSON
on getMonitorData()
set monitorList to {}
set allScreens to NSScreen's screens()
repeat with aScreen in allScreens
-- Extract display ID from NSScreen's device description
set deviceDesc to aScreen's deviceDescription()
set displayID to (deviceDesc's objectForKey:"NSScreenNumber") as integer
-- Get screen geometry: frame returns {{x, y}, {width, height}}
set screenFrame to aScreen's frame()
set frameOrigin to item 1 of screenFrame
set frameSize to item 2 of screenFrame
set xPos to (item 1 of frameOrigin) as integer
set yPos to (item 2 of frameOrigin) as integer
set screenWidth to (item 1 of frameSize) as integer
set screenHeight to (item 2 of frameSize) as integer
set serialNumRaw to current application's CGDisplaySerialNumber(displayID)
set nsNum to NSNumber's numberWithDouble_(serialNumRaw)
set serialNum to nsNum's stringValue() as string
set monitorInfo to {displayID:displayID, width:screenWidth, height:screenHeight, x:xPos, y:yPos, serialNumber:serialNum}
set end of monitorList to monitorInfo
end repeat
return monitorList
end getMonitorData
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment