Skip to content

Instantly share code, notes, and snippets.

@mlliarm
Forked from wojteklu/terminal_notification.md
Last active December 6, 2021 10:19
Show Gist options
  • Save mlliarm/575000763d3a11f5475f664b4f6b3cd5 to your computer and use it in GitHub Desktop.
Save mlliarm/575000763d3a11f5475f664b4f6b3cd5 to your computer and use it in GitHub Desktop.
Show macOS notification when long running command finishes and your terminal is not in focus.

ATTENTION READER

BEWARE/ACHTUNG: The Bash script below doesn't work as-is on a GNU/Linux system, as GNU/Linux doesn't have the osascript OSX binary, as this is a native binary in OSX (see: OS X man page of osascript, how to use osascript binary within an OS X system). Please monitor the ticket at the repo of the python osascript binary. This is our best shot.

What and how

What

  • "Show macOS notification when long running command finishes and your terminal is not in focus." (from the title).
  • So it seems that the suggestion script is meant to run on OSX only (hence the osascript call).
  • I was able to find this (osascript) python package that seems to do that same thing. I'll test this.
  • Well, the previously found interesting looking PyPy solution, osascript is failing to work with my old Python version. I've opened an issue on that repository to examine if it's a matter of backwards incompatibillity or if it should be working with python3.6 as well, but it doesn't and thus: bug. Well, now let's wait.

How

Add the following to your ~/.zshrc or ~/.bashrc (it should work with both, according to a commentor on the parent gist): Renamed the file from *.md to *.sh to see if there's any change in the syntax highlighting, but it seems that there's not.

In order to understand the Bash commands that are being fed to osascript with the osascript -e, I'll have to get some proper coloring. So I think I'll revert some of the changes I did, and I'll convert the file back to a Markdown file.

The code

This is the juicy part, which now we can read and understand what it does as syntax highlighting is helping more than before:

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

This is a function wrapper generalizing the above juicy part and modularizing the actions that we want to be taken:

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

Some philosophical remarks on using this or other code you find online

  • Well, iff (if and only if) you've understood what the above code does, and you agree with its actions, then feel free to add it in your ~/.*shrc file. If you don't understand much because your Bash knowledge isn't strong enough, read this beginner's guide, and if you need moar read this advanced bash user manual too.
  • Then, and only then, do revisit the code you found somewhere in Gibson's modern day cyberspace.
  • NEVER add code on your machine -on prem or on cloud- if you don't understand what the code does.
  • Trust None but your own common logic and understanding of the code you're reading.
  • This doesn't mean that people are willingly putting source codes online that could do malicious activities on other peoples' computers. I mean, if this were true, at least one person that can read the code would report this or make it widely known. But, this doesn't mean that we live in an world full of 'Angels'. As the Americans used to say, "Hope for the best but prepare for the worst". And perhaps Mayas' Angelou quote is much more general and covers all the edge cases too: “Hoping for the best, prepared for the worst, and unsurprised by anything in between.”.
  • So, besides the argument that we don't live in a society yet full of people that don't want to cause harm to their neighbor, I believe that reading with a critical eye every snippet of code one finds online (at Github, Gists, StackOverflow, etc) is a very good practive of critical thinking. If you are not critical at anything you read, don't expect to develop your critical thinking.
  • And what is a software engineer without critical thinking? They are simple 'soulless' automatons hitting aimlessly the keys on a machine equally without any kind of 'soul' as well. It's logical reasoning that makes us human. It's logic that makes us something more than mere animals driven by their instict for survival.
  • A software engineer without critical thinking can only cause harm, first to himself and then to others.
  • And where the world will go if we're full of software engineers that cause harm to others or/and themselves?
  • The world will go to shit. That's where it'll go.
  • Maybe we're already there. Who knows. QED.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment