Skip to content

Instantly share code, notes, and snippets.

@paulfioravanti
Last active April 17, 2022 04:03
Show Gist options
  • Save paulfioravanti/1290fd2f6ee89f293214e9e3b3017e0c to your computer and use it in GitHub Desktop.
Save paulfioravanti/1290fd2f6ee89f293214e9e3b3017e0c to your computer and use it in GitHub Desktop.
Applescript Graveyard
# An attempted refactor of
# https://github.com/paulfioravanti/steno-dictionaries/blob/main/src/command/actions/find.applescript
# involving extracting common functions (`getActiveApp()` and `getiTermProcessName()`) into a util file,
# located in the src/command/ directory, and reading it back in.
# Not adopted since the nature of loading scripts in AppleScript is verbose and it was not possible to
# retrieve the file's relative path grandparent and store it in a property variable. Reading in the file
# dynamically every time meant that running the command felt slower, so I opted for just having duplicated
# handlers across multiple files.
global util
on run
tell application "Finder"
set pathToUtil to container of container of (path to me) as text & "util.scpt"
end tell
set util to (load script file pathtoUtil)
set activeApp to util's getActiveApp()
if activeApp is "iTerm2" then
performiTerm2Find()
else
performFind()
end if
end run
on performiTerm2Find()
set processName to util's getiTermProcessName()
if processName contains "vim" then
performVimFind()
else
performFind()
end if
end performiTerm2Find
on performVimFind()
tell application "System Events"
# 53 = Escape
key code 53
keystroke "/"
end tell
end performVimFind
on performFind()
tell application "System Events"
keystroke "f" using {command down}
end tell
end performFind
# This represents a single attempt to have the same pane navigation key shortcuts work
# across all of iTerm, tmux, and vim. This is only for selecting the pane above, but
# selecting the pane below, left, or right, would have been written in similar ways.
# This was not adopted due to this script just being too slow for an action that I would
# want a more quick and snappy response for.
on run
set activeApp to getActiveApp()
if activeApp is not equal to "iTerm2" then
displayError(activeApp)
end if
set processName to getiTermProcessName()
if processName contains "vim" or processName contains "tmux" then
performVimTmuxSelectPaneAbove()
else
performiTerm2SelectPaneAbove()
end
end run
on performVimTmuxSelectPaneAbove()
tell application "System Events"
keystroke "k" using {control down}
end tell
end performVimTmuxSelectPaneAbove
on performiTerm2SelectPaneAbove()
tell application "System Events"
# 126 = Up arrow
key code 126 using {option down, command down}
end tell
end performiTerm2SelectPaneAbove
on getActiveApp()
tell application "System Events"
return name ¬
of first application process ¬
whose frontmost ¬
is true
end tell
end getActiveApp
on getiTermProcessName()
tell application "iTerm2"
# REF: https://iterm2.com/documentation-scripting.html
return name ¬
of current session ¬
of current window
end tell
end getiTermProcessName
on displayError(activeApp)
set errorMessage to "Navigating splits not supported with " & activeApp & "."
display notification errorMessage with title "Error"
tell me to error errorMessage
end displayError
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment