FelixKratz / SketchyBar

A highly customizable macOS status bar replacement
https://felixkratz.github.io/SketchyBar/
GNU General Public License v3.0
5.25k stars 82 forks source link

FR: Event `front_app_switched` should also send deactivated app #463

Closed chrisgrieser closed 6 months ago

chrisgrieser commented 6 months ago

Right now, the event front_app_switched does populate INFO with the name of the app that has been activated (become frontmost).

For various scripting purposes, it is useful to get the name of the app that was deactivated, for instance things like "update the counter of this app when closing/deactivating it"

FelixKratz commented 6 months ago

The way I would keep track of the previously focussed app in sketchybar would not be different from a scripting approach: when the front app changes, write the current front app to some cache and use it on the next event to retrieve the previously focused window.

For example use this handle script for the front_app_switched event:

#!/bin/bash

if [ "$SENDER" = "front_app_switched" ]; then
  PREVIOUS_FRONT_APP=$(</tmp/front_app)
  echo "$INFO" > /tmp/front_app

  sketchybar --set $NAME label="$INFO" icon.background.image="app.$INFO"
fi
chrisgrieser commented 6 months ago

Ah, yeah, that works as well. For reference if anyone finds this issue:

# only trigger on deactivation of $TASK_APP
if [[ "$SENDER" = "front_app_switched" ]]; then
    data="/tmp/sketchybar_front_app"
    [[ -f "$data" ]] && deactivated_app=$(<"$data")
    echo -n "$INFO" >"$data"
    [[ "$deactivated_app" != "$TASK_APP" ]] && return 0
fi
do …