-
Notifications
You must be signed in to change notification settings - Fork 80
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for any macOS terminal #100
Open
stepan-tikunov
wants to merge
2
commits into
marzocchi:master
Choose a base branch
from
stepan-tikunov:macos-terminals
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#!/usr/bin/osascript | ||
|
||
on run argv | ||
set targetPID to item 1 of argv | ||
|
||
tell application "System Events" | ||
return bundle identifier of first process whose unix id is targetPID | ||
end tell | ||
end run | ||
|
40 changes: 20 additions & 20 deletions
40
applescript/resources/is-apple-terminal-active.applescript
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,24 @@ | ||
#!/usr/bin/osascript -ss | ||
|
||
on run ttyName | ||
try | ||
set ttyName to first item of ttyName | ||
on error | ||
set ttyName to "" | ||
end try | ||
if ttyName is equal to "" then error "Usage: is-apple-terminal-active.applescript TTY" | ||
tell application id "com.apple.terminal" | ||
if frontmost is not true then error "Apple Terminal is not the frontmost application" | ||
-- fun stuff, with 2 tabs in one window AS reports 2 windows with one | ||
-- tab each, and all the tabs are frontmost! | ||
repeat with t in tabs of (windows whose frontmost is true) | ||
if t's tty is equal to ttyName then return | ||
end repeat | ||
error "Cannot find an active tab for '" & ttyName & "'" | ||
end tell | ||
try | ||
set ttyName to first item of ttyName | ||
on error | ||
set ttyName to "" | ||
end try | ||
if ttyName is equal to "" then error "Usage: is-apple-terminal-active.applescript TTY" | ||
tell application id "com.apple.terminal" | ||
if frontmost is not true then error "Apple Terminal is not the frontmost application" | ||
-- fun stuff, with 2 tabs in one window AS reports 2 windows with one | ||
-- tab each, and all the tabs are frontmost! | ||
repeat with t in tabs of (windows whose frontmost is true) | ||
if t's tty is equal to ttyName then return | ||
end repeat | ||
error "Cannot find an active tab for '" & ttyName & "'" | ||
end tell | ||
end run |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#!/usr/bin/osascript -ss | ||
|
||
on run argv | ||
set targetPID to item 1 of argv | ||
|
||
tell application "System Events" | ||
set appProcess to first process whose unix id is targetPID | ||
end tell | ||
|
||
tell application (name of appProcess) | ||
if frontmost is not true then error "Window with pid " & targetPID & " is not the frontmost application" | ||
end tell | ||
end run | ||
|
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#!/usr/bin/env zunit | ||
|
||
@setup { | ||
load ../notify.plugin.zsh | ||
|
||
if ! command -v osascript 1>/dev/null 2>/dev/null || [[ "$TERM_PROGRAM" == "Apple_Terminal" ]] \ | ||
|| [[ "$TERM_PROGRAM" == "iTerm.app" ]] || [[ -n "$ITERM_SESSION_ID" ]]; | ||
then | ||
skip 'this test must be run in macOS terminal that is not iTerm2 or Apple Terminal (e.g. Alacritty)' | ||
fi | ||
|
||
app_id=$(osascript -e 'tell application "System Events" to return bundle identifier of first process where it is frontmost') | ||
} | ||
|
||
@teardown { | ||
if ! command -v osascript 1>/dev/null 2>/dev/null || [[ "$TERM_PROGRAM" == "Apple_Terminal" ]] \ | ||
|| [[ "$TERM_PROGRAM" == "iTerm.app" ]] || [[ -n "$ITERM_SESSION_ID" ]]; | ||
then | ||
return | ||
fi | ||
|
||
osascript <<SCPT | ||
tell app id "$app_id" | ||
activate | ||
end | ||
SCPT | ||
} | ||
|
||
@test 'Any other macOS terminal: is-terminal-active - yes' { | ||
osascript <<SCPT | ||
tell app id "$app_id" | ||
activate | ||
end | ||
SCPT | ||
|
||
run is-terminal-active | ||
assert $state equals 0 | ||
} | ||
|
||
@test 'Any other macOS terminal: is-terminal-active - app in background' { | ||
osascript <<SCPT | ||
tell app "System Events" | ||
tell item 1 of (application processes whose bundle identifier is "$app_id") | ||
set visible to false | ||
end tell | ||
end tell | ||
SCPT | ||
|
||
run is-terminal-active | ||
assert $state equals 1 | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function will infinitely recurse if
ps
doesn’t take the expected options (maybe on other BSDs? I dunno), or if there is no process with PID 1 (maybe in a container?).I’m not especially worried about this, but it could be prevented by setting
setopt localoptions errexit
here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tried to google this option and barely found anything. I think I fixed it much simpler :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That works too!
setopt localoptions errexit
might be hard to google because zsh ignores underscores in the options. Just runningsetopt
gives a list of options, which are reported without underscores, but the manual lists them with underscores. So the above is equivalent tosetopt local_options err_exit
.local_options
causes the shell options to only be set for the duration of the local function.err_exit
causes the shell to exit when a command fails, likeset -e
. (It should probably have beenerr_return
which causes the function to return.)Docs:
setopt
in https://zsh.sourceforge.io/Doc/Release/Shell-Builtin-Commands.html forLOCAL_OPTIONS
andERR_EXIT
in https://zsh.sourceforge.io/Doc/Release/Options.html (butERR_RETURN
might have been better)