julienXX / terminal-notifier

Send User Notifications on macOS from the command-line.
Other
6.37k stars 330 forks source link

Open folder/file when notification is clicked #206

Closed 8bitgentleman closed 7 years ago

8bitgentleman commented 7 years ago

Is it possible to open a certain folder or file when the notification is clicked?

jonvandermeulen commented 7 years ago

So, it takes a bit of experimentation to get it to do precisely what you want, depending on context.

terminal-notifier -title "Title" -message "Message" -actions "Close" -execute "open ~/"

In this example, the -execute switch will run the command when the message is clicked. The command open ~/SomeDir will open the directory in Finder (you can also do something like open /Applications/TextEdit.app ~/SomeDir/SomeFile.foo - you get the idea)

As of this writing, this only works if you also include the -actions switch. If you want to do anything more interesting than "Close" you have to capture the stdin from it, eg:

#!/bin/bash
function notifier_action {
    read action
    if [ "$action" = "View Log" ]; then
        open /Applications/Utilities/Console.app /var/log/system.log
    fi
}

terminal-notifier -title "Title" -message "Message" -actions "View Log" -execute "open ~/" | notifier_action
julienXX commented 7 years ago

I added an example actions usage in the README https://github.com/julienXX/terminal-notifier#exemple-actions-usage-with-a-shell-script-and-jq