obb 2022-01-11

Does anyone know how to get the size of your display with JXA? I'm trying to make an example by adapting this script: https://lukas-domagala.de/blog/cljs-to-jxa-for-automation.html (/cc @domagala.lukas) to move Emacs to the left of the screen and my Google Chrome to the right of the screen. It would make a pretty nice example of how to lay out certain apps by creating a little script for it.

I’ve been looking at that problem for the post. It’s not that hard to get your whole setup size:

tell application "Finder"
	get bounds of window of desktop
end tell
=> {0, 0, 6272, 1440}
But as you can see, you don’t really get each monitor. This thread https://forum.latenightsw.com/t/get-sizes-of-monitor-s-via-applescript/1351/3 has solutions for multiple monitors, but I didn’t have the patience to translate them into jxa.
use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use framework "AppKit"
use scripting additions

set allFrames to (current application's NSScreen's screens()'s valueForKey:"frame") as list
=> {{{0.0, 0.0}, {2560.0, 1440.0}}, {{4480.0, 193.0}, {1792.0, 1120.0}}, {{2560.0, 240.0}, {1920.0, 1200.0}}}

I could find lots of applescript examples, but I don't know how to translate it into JXA

For example this one:

tell application "Finder"
	get bounds of window of desktop
end tell

Got it!

$ obb -e '(def finder (js/Application "Finder")) (def bounds (.bounds (.window (.-desktop finder)))) (prn bounds)'
#js {:x 0, :y 0, :width 1680, :height 1050}

🙌 1

yeah translating the applescript examples has been the most frustrating thing, especially once they get more complicated. applescript syntax is truly any interesting experience.

I find it’s easiest to just work your way through it iteratively. That’s part of why I want to get REPL support up and running soon.

yeah, but I didn't even know where to start.

as I had trouble finding this API. If I know the API, I can iterate pretty quickly by just executing snippets

Got it! Makes sense.

Searching for applescript was usually a lot better than jxa, just more annoying to translate

I think I might be able to replace phoenix eventually 😉