Skip to content

Instantly share code, notes, and snippets.

@wojteklu
Last active July 7, 2024 14:20
Show Gist options
  • Save wojteklu/5f27f1cdcb35ba71e53d02a5aebd1d0c to your computer and use it in GitHub Desktop.
Save wojteklu/5f27f1cdcb35ba71e53d02a5aebd1d0c to your computer and use it in GitHub Desktop.
Show macOS notification when long running command finishes and your terminal is not in focus.

Add the following to your ~/.zshrc:

function notifyme {
  LAST_EXIT_CODE=$?
  CMD=$(fc -ln -1)
  osascript -e 'on run argv
  tell application "System Events"
    set frontApp to name of first application process whose frontmost is true
    if frontApp is not "Terminal" then
      set notifTitle to item 1 of argv
      if notifTitle starts with "open" is false
        set notifBody to "succeded"
        set errorCode to item 2 of argv
        if errorCode is not "0"
          set notifBody to "failed with error code " & errorCode
        end if
        display notification notifBody with title notifTitle
      end if
    end if
  end tell
end run' $CMD $LAST_EXIT_CODE
}

export PS1='$(notifyme)'$PS1
@sumitjainn
Copy link

Works with BASH too, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment