Skip to content

Instantly share code, notes, and snippets.

@henryroe
Last active March 7, 2021 21:54
Show Gist options
  • Save henryroe/8810193 to your computer and use it in GitHub Desktop.
Save henryroe/8810193 to your computer and use it in GitHub Desktop.
Demonstration of how to use an AppleScript Bundle to enable access to NSScreen in AppleScriptObjC
(*
Copy and paste this script into a new document in AppleScript Editor.
When saving, select "Script Bundle" as File Type.
Save the file to: "~/Library/Script Libraries/Access NSScreen.scptd"
Note: you may need to create the directory: "~/Library/Script Libraries"
Note: while this gist has the file ending "scpt", by selecting the
"Script Bundle" file type, the file extension "scptd" should be added.
Now, in the document's toolbar in AppleScript Editor click "Bundle Contents"
and select "Applescript/Objective-C Library" checkbox
Click "Compile" and save again.
Now, you can try out the next file ("demo Access NSScreen.scpt") to get the size
and layout of your displays.
*)
use framework "AppKit"
on getScreenLayout()
set output to {}
repeat with curScreen in current application's NSScreen's screens()
set theFrame to curScreen's frame()
set thisDisplay to {width:width of |size| of theFrame, height:height of |size| of theFrame, originX:x of origin of theFrame, originY:y of origin of theFrame}
copy thisDisplay to the end of the output
end repeat
return output
end getScreenLayout
use theLib : script "Access NSScreen"
use scripting additions
set theResult to theLib's getScreenLayout()
set theString to ""
set dispnum to 0
repeat with curDisplay in theResult
set theString to theString & "Display " & (dispnum as string) & return
set theString to theString & tab & "Size (width, height):" & tab & width of curDisplay & tab & height of curDisplay & return
set theString to theString & tab & "Origin (x, y):" & tab & tab & tab & originX of curDisplay & tab & originY of curDisplay & return
set dispnum to dispnum + 1
end repeat
display dialog theString
@splicemaahs
Copy link

Thanks from my future self...

@ChristoferK
Copy link

There's no need to create a library to house one handler. It can just be declared on its own in a simple .applescript file, and then called within the same script to make use of it. demo Access NSScreen.scpt seems largely concerned with getting a string representation of the AppleScript list/record returned by getScreenLayout(), but it's possible to do away with most of that by utilising additional Objective-C properties. Here's a re-worked version of the two scripts, combined into one, which can be saved and run as either a plain text .applescript file or a compiled .scpt file:

use framework "AppKit"
use scripting additions

property this : a reference to current application
property NSScreen : a reference to NSScreen of this
property NSMutableDictionary : a reference to NSMutableDictionary of this

on getScreenLayout()
	set output to NSMutableDictionary's dictionary()
	repeat with screen in NSScreen's screens()
		(output's setObject:({width:item 1 of item 2 ¬
			, height:item 2 of item 2 ¬
			, X:item 1 of item 1 ¬
			, Y:item 2 of item 1} of screen's frame()) ¬
			forKey:("Display " & output's |count|()))
	end repeat
	return the output's |description|()
end getScreenLayout

display dialog getScreenLayout() as text

The output is formatted like so:

{
    "Display 0" =     {
        X = 0;
        Y = 0;
        height = 800;
        width = 1280;
    };
}

However, if you particularly wanted to get rid of the braces and punctuation, then you can simply replace:

return the output's |description|()

with:

tell the output's |description|() to return ¬
		(its stringByReplacingOccurrencesOfString:("[{};]") ¬
			withString:space options:NSRegularExpressionSearch ¬
			range:{0, its |length|()})

and declare NSRegularExpressionSearch as a property whose value is a reference to 1024. Then the output will look like this:

"Display 0" =      
        X = 0 
        Y = 0 
        height = 800 
        width = 1280

with additional displays formatted identically below the previous.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment