-
Notifications
You must be signed in to change notification settings - Fork 79
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
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
|
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 |
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 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,31 @@ function current-tty { | |
fi | ||
} | ||
|
||
# Find the top level parent PID of current shell (not including root process), also accounting for TMUX | ||
function top-level-ppid { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function will infinitely recurse if I’m not especially worried about this, but it could be prevented by setting There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. That works too!
Docs:
|
||
# Find parent PID of process by its PID | ||
function ppid-of { | ||
pid=$1 | ||
ps -p $pid -o ppid= | ||
} | ||
|
||
pid=$$ | ||
|
||
if is-inside-tmux; then | ||
pid=$(tmux display-message -p '#{client_pid}') | ||
fi | ||
|
||
ppid=$(ppid-of $pid) | ||
|
||
# We're assuming the root process PID is equal to 1 | ||
while [[ $ppid -ne 1 ]]; do | ||
pid=$ppid | ||
ppid=$(ppid-of $pid) | ||
done | ||
|
||
echo $pid | ||
} | ||
|
||
# Exit with 0 if given TMUX pane is the active one. | ||
function is-current-tmux-pane-active { | ||
is-inside-tmux || return 1 | ||
|
@@ -40,11 +65,11 @@ function notification-title { | |
zstyle -s ':notify:' "$type"-title title | ||
|
||
while [[ $# -gt 0 ]]; do | ||
k="$1" | ||
v="$2" | ||
title=$(echo $title | sed "s/#{$k}/$v/") | ||
shift | ||
shift | ||
k="$1" | ||
v="$2" | ||
title=$(echo $title | sed "s/#{$k}/$v/") | ||
shift | ||
shift | ||
done | ||
|
||
echo $title | ||
|
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 | ||
} |
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.
I think this is less understandable then it was before — this clause is part of the outer
if
, so it should either be the way it was before, or on the same level (4 spaces less indent) as the outerif
.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.
Yes, my bad. Fixed it