ff09151d6b82fef7af4ced774a416a36e7835b8a

Author
Aurora <AuroraWright@users.noreply.github.com>
Committer
GitHub <noreply@github.com>
Date

Message

fix(bgnotify): avoid permission prompts by checking frontmost app ID (#10318)

Co-authored-by: Marc Cornellà <hello@mcornella.com>

Diff

 1diff --git a/plugins/bgnotify/bgnotify.plugin.zsh b/plugins/bgnotify/bgnotify.plugin.zsh
 2index aecde20ea6ff4598fffb5a0270c10d789bb8b3f3..479796dbec57202492a7f5c7a8e8cf1c161f2f6a 100644
 3--- a/plugins/bgnotify/bgnotify.plugin.zsh
 4+++ b/plugins/bgnotify/bgnotify.plugin.zsh
 5@@ -20,6 +20,12 @@ if ! (type bgnotify_formatted | grep -q 'function'); then ## allow custom functi
 6   }
 7 fi
 8 
 9+currentAppId () {
10+  if (( $+commands[osascript] )); then
11+    osascript -e 'tell application (path to frontmost application as text) to id' 2>/dev/null
12+  fi
13+}
14+
15 currentWindowId () {
16   if hash osascript 2>/dev/null; then #osx
17     osascript -e 'tell application (path to frontmost application as text) to id of front window' 2&> /dev/null || echo "0"
18@@ -32,11 +38,20 @@ currentWindowId () {
19 
20 bgnotify () { ## args: (title, subtitle)
21   if hash terminal-notifier 2>/dev/null; then #osx
22-    [[ "$TERM_PROGRAM" == 'iTerm.app' ]] && term_id='com.googlecode.iterm2';
23-    [[ "$TERM_PROGRAM" == 'Apple_Terminal' ]] && term_id='com.apple.terminal';
24+    local term_id="$bgnotify_appid"
25+    if [[ -z "$term_id" ]]; then
26+      case "$TERM_PROGRAM" in
27+      iTerm.app) term_id='com.googlecode.iterm2' ;;
28+      Apple_Terminal) term_id='com.apple.terminal' ;;
29+      esac
30+    fi
31+
32     ## now call terminal-notifier, (hopefully with $term_id!)
33-    [ -z "$term_id" ] && terminal-notifier -message "$2" -title "$1" >/dev/null ||
34-    terminal-notifier -message "$2" -title "$1" -activate "$term_id" -sender "$term_id" >/dev/null
35+    if [[ -z "$term_id" ]]; then
36+      terminal-notifier -message "$2" -title "$1" >/dev/null
37+    else
38+      terminal-notifier -message "$2" -title "$1" -activate "$term_id" -sender "$term_id" >/dev/null
39+    fi
40   elif hash growlnotify 2>/dev/null; then #osx growl
41     growlnotify -m "$1" "$2"
42   elif hash notify-send 2>/dev/null; then #ubuntu gnome!
43@@ -54,6 +69,7 @@ bgnotify () { ## args: (title, subtitle)
44 bgnotify_begin() {
45   bgnotify_timestamp=$EPOCHSECONDS
46   bgnotify_lastcmd="${1:-$2}"
47+  bgnotify_appid="$(currentAppId)"
48   bgnotify_windowid=$(currentWindowId)
49 }
50 
51@@ -62,7 +78,7 @@ bgnotify_end() {
52   elapsed=$(( EPOCHSECONDS - bgnotify_timestamp ))
53   past_threshold=$(( elapsed >= bgnotify_threshold ))
54   if (( bgnotify_timestamp > 0 )) && (( past_threshold )); then
55-    if [ $(currentWindowId) != "$bgnotify_windowid" ]; then
56+    if [[ $(currentAppId) != "$bgnotify_appid" || $(currentWindowId) != "$bgnotify_windowid" ]]; then
57       print -n "\a"
58       bgnotify_formatted "$didexit" "$bgnotify_lastcmd" "$elapsed"
59     fi