Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to bring terminal to front when command is finished?

Is there a way that I can force terminal to become the active window when a command is finished running?

It would be very useful when running lengthy commands or scripts to be notified somehow when they complete.

Alternatively, could I send a notification through macOS every time a command finishes?

like image 719
tehp Avatar asked Oct 24 '25 14:10

tehp


1 Answers

On macOS 10.8+, AppleScript supports the display notification command for displaying notifications (optionally with a sound).

Here's an example, using sleep 3 to represent a long-running shell command:

sleep 3; osascript -e 'display notification "Done!" with title "Long-running shell command" sound name "Hero"'

You could wrap this functionality in a shell function and add it to your ~/.zshrc file:

notifyWhenDone() {
  local msg=${1:-'Done!'} title=${2:-'Long-running shell command'} sound=${3:-'Hero'}
  osascript -e 'display notification "'"$msg"'" with title "'"$title"'" sound name "'"$sound"'"'
}

Note: To make this fully robust, you'd have to make sure that the content of optional arguments $1, $2, and $3 doesn't break the AppleScript command.

With this function in place:

sleep 3; notifyWhenDone

If you also want to activate Terminal.app on completion, append the following option to the arguments passed to osascript:
-e 'activate application "Terminal"'.

like image 68
mklement0 Avatar answered Oct 27 '25 04:10

mklement0



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!