Last active
March 7, 2021 21:54
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(* | |
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 bygetScreenLayout()
, 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:The output is formatted like so:
However, if you particularly wanted to get rid of the braces and punctuation, then you can simply replace:
with:
and declare
NSRegularExpressionSearch
as a property whose value isa reference to 1024
. Then the output will look like this:with additional displays formatted identically below the previous.