Announce When a Command Line Task is Completed in Mac OS X
By appending the say command to the end of another command, Mac OS X will vocally announce when the initial task has finished running successfully. For example, to have OS X announce that a particular script has finished running the command could be:
python backup.py && say "jobs done"
The important part is the “&& say” portion, which can also be customized with other voices from Mac OS X’s text to speech options by using the -v flag followed by a voice name, like so:
dscacheutil -flushcache && say -v Alex your cache has been cleared sir
This is perfect for running scripts, making svn/git commits, compiling code, and other tasks that can take an indeterminate amount of time to complete and where it’s easy to become distracted by facebook^H^H^H^H^H^H^H^H other work.
This great tip was posted by @niels on Twitter, follow @osxdaily there too to get our latest posts and updates.
[…] This has a myriad of potentially valid uses, but one fantastic use-case is along the same veins of verbally announcing when a command has completed or sending a badge alert, but instead posting the notification to OS X Mountain Lion’s […]
[…] long ago we showed you how to announce when a command line task was finished by using the ‘say’ command, but because it speaks aloud it may be inappropriate to use […]
[…] the lines of vocally announcing task completion within the command line, you can also have Mac OS X speak the output of any executed […]
Haha… I read the tweet and clicked the link and was thinking “Oh… maybe it’s some fancy way of invoking growl or a UIAlertVeiw. Wait… wouldn’t be easier to just add && say -v Task Done to the end of you code” only to come over here and see that’s exactly what you did XD
the only drawback for using ‘&&’ is that you won’t get a notification if the command fails!
Why not simply use ‘;’ as command separator?
compare:
cd IDontExist && say -v Alex Oops
with:
cd IDontExist; say -v Alex Oops
Excellent! Alex even gets the intonation right. Put a question mark at the end and hear for yourself.
[…] [via OS X Daily] […]
Nope, not for the finder. For the terminal, it’d be
$cp foo bar && say “file transfer complete”
so…is it possible to make a notification when file transfer from finder is done? How to do it?
You can also pipe to say
I tried it with uptime as follows
uptime | say
The Mac told me the time, uptime, load average etc
Very cool little function
Enjoy!
You could also try “growlnotify”, e.g. like this
$ growlnotify -s -m “Task Done”
Agreed, growl is a superior solution for anyone in an office, and you can use -H to specify alternate hosts to receive the notification.
My function to say the task status
function sayTaskStatusUsingExitCode() {
if [ “$?” == “0” ]
then
say -v Alex “Task Ok”
else
say -v Alex “Task Error”
fi
}
You don’t need a function for that.
You can do:
sometask.sh && say -v Alex “Task Ok” || say -v Alex “Task Error”
And, by the way “&&” is wrong in the article itself – this is logical AND, which means the the latter part is executed *only* if the first part exited with a zero exit status.
It would make much more sense to do:
python backup.py; say “jobs done”
Then the second command is executed no matter what the exit code of the first.