Last active
September 9, 2021 22:40
-
-
Save c4710n/c528881dbd0b8a691c6de9d98b35c02d to your computer and use it in GitHub Desktop.
macOS: focus on emacs frame
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
#!/usr/bin/env osascript | |
# | |
# Focus on frame by the title of frame. | |
# | |
# Usage: | |
# $ focus-emacs-frame.applescript editor | |
# $ focus-emacs-frame.applescript terminal | |
on run argv | |
if argv is {} then | |
focusApp() | |
end if | |
-- the first argument passed from CLI | |
set frameName to item 1 of argv | |
set frame to findFrame(frameName) | |
if frame is not false then | |
focusFrame(frame) | |
else | |
beep | |
focusApp() | |
end if | |
end run | |
on focusApp() | |
tell application "System Events" | |
set frontmost of process "emacs" to true | |
end tell | |
end focusApp | |
on focusFrame(frame) | |
tell application "System Events" to tell process "emacs" | |
set frontmost to true | |
perform action "AXRaise" of frame | |
end tell | |
end focusFrame | |
on findFrame(frameName) | |
local selectedFrameName | |
local selectedFrame | |
tell application "System Events" to tell process "emacs" | |
-- The trick here is that you can't rely on "window 1" to be the one you're after, | |
-- So let's filter the UI elements list by subroles. | |
-- Emacs return a a total bogus AXTextField:AXStandardWindow, but we can stil work with it | |
set frames to (every UI element whose subrole is "AXStandardWindow") | |
if frames is {} then set frames to (every UI element whose subrole is "AXDialog") | |
if frames is {} then | |
beep | |
return | |
end if | |
repeat with frame in frames | |
if name of frame starts with frameName then | |
return frame | |
end if | |
end repeat | |
end tell | |
return false | |
end findFrame |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment