2013-05-02

Shutdown Mac after VLC Playlist Completes

So, I like to listen to music to help me fall asleep, specifically I listen to Hemi-sync & Brainsync brain entrainment music. These are very effective at what they do, and require the use of stereo headphones.
I'm cheap and unwilling to buy a smart phone or an mp3 player. I'm a minimalist computer geek, what can I say. I like to use my Macbook (now macbook pro) to listen to music in bed and nod off to sleep. I use Insomniax to stop my macbook from falling asleep when I close the lid so that we can listen to the music. Update: With newer macs Insomniax will no longer work as it is no longer being maintained. I now use NoSleep.
On my macbook/macbookpro, I just turn down the brightness all the way with the keyboard function keys to the point that it literally turns off the display (it's not just black, I think this is a very cool feature) and close the lid, no fuss or muss about it. When I want to do something on there, I open the lid, and turn up the brightness.
Now on to the meat of this article. VLC will not shutdown the computer when it is done playing your music, but we can still get this behavior using the command line and by changing a setting in VLC. Yes, you can control VLC using command line, did you know that? It's a very powerful program. But first, We need to change a setting in the Mac OS Aqua GUI, then make a script or alias with some code. So lets get started. Open up VLC and change the following setting.
VLC>Preferences > Show All > Playlist > Play & Exit
To get the behavior we want, I have discovered that it works using the ncurses interface via Bash shell. Command line to the rescue! Using the command line via the Terminal application in "Applications/Utilities/Terminal.app" (I recomend you add this to your dock if you have not already) we can tell tell Terminal to open VLC adding an option to launch VLC using a different interface - nCurses in this case - instead of the default Mac OS Aqua GUI like so
/Applications/VLC.app/Contents/MacOS/VLC -I ncurses
Since we're launching this program from the shell, we can add further instructions. The following will launch VLC with the nCurses interface and wait, when the first command completes (VLC closes or crashes), it will move on to the next command, which is to shut down the computer. This is achieved using two ampersands, the && bit.
/Applications/VLC.app/Contents/MacOS/VLC -I ncurses && osascript -e 'tell application "System Events" to shut down'
To learn more about the && command read this tutorial.
Personally I add a bit to tell the VLC ncurses interface which directory to begin in when I open the file browser in the ncurses interface so that I don't have to browse very far to find the files I want to play. You do this by using the --browse-dir option.
So to put it all together, make an alias in your bash profile or make a bash script.
I opted to do a bash script as I want as little as possible in the bash profile, it can get messy in there once you really start to customize things to make working with command line software easier. More importantly some programs you install will modify or wipe out that .profile by overwriting it which can be a pain. In a new plain text file paste the following code and edit the paths to fit your situation and username.
#! /bin/bash
/Applications/VLC.app/Contents/MacOS/VLC -I ncurses --browse-dir PathToMusicFolder && osascript -e 'tell application "System Events" to shut down'

Once you have your bash script you will need to make it executable
chmod 755 PathToYourScript
The name of your script is what you use to execute it, so make it easy to remember and type in the Terminal. I call mine vlcshutdown, notice there is no extension.
Make sure your shell can find your script, you can do this by adding the directory where you keep your own personal scripts to the search path for your shell by editing the shell's profile. In this case the .profile text file in your home folder. If you don't have a .profile you can create one and follow this example.
export PATH=/Users/Username/PersonalScriptDirectory/:$PATH
If making a bash script and changing your path is too complicated you can just opt to put everything in your .profile. You only need the following text in your .profile, none of the other stuff I've written above.
alias vlcshutdown=/Applications/VLC.app/Contents/MacOS/VLC -I && osascript -e 'tell application "System Events" to shut down'
With this alias, you would type vlcshutdown in the Terminal to launch VLC with nCurses. Once this is open you can add things to your playlist (shift B) by browsing (using arrow keys) pressing enter to select. Then select your playlist and hit enter, your music will play, when the playlist ends and providing you don't have repeat on, VLC will close and then your computer will shut down. You can pause and resume by pressing the space bar, and fast forward or backwards by pressing right arrow or left arrow. press h for the help screen to see all the shortcuts.
One cool thing about using the nCurses interface is you can play audio from movies too, but nCurses will only play the audio from those files, not the video itself. This interface opens up the possibility of listening to your music over an SSH connection although I've not bothered to do this.
I wrote this in a way where someone who knows nothing at all about command line would probably struggle, if this applies to you, leave a comment and I'll try and help.

Update Sept 19th, 2015: The standard GUI VLC client now quits after playlist is completed when the option is selected via VLC > Preferences > Show All > Interface > Main Interface > Interface Module > Default or Mac OS X Interface (same thing either way), but strangely it seems to require a nudge on the mouse at times to quit, which kinda defeats the point. This can be worked around if one uses a virtual mouse jiggler. In Jiggler Preferences I set Time Between Jiggles to 30 seconds and left all the other settings at default.

I find it depends on what applications are running, so I have included a safeguard in my script to forcibly shutdown the computer 2 minutes after vlc quits in case the polite shutdown command does not work. I am also redirecting error messages to null because I was getting trivial errors that clutter the terminal.

One more con to the mac interface run from the terminal is that the --browse-dir is an option that seems to only work for the ncurses interface, when using the macosx interface it will start browsing at whichever directory you browsed to last time with VLC. Not a big deal.








With either interface I can't seem to find a way to shutdown the computer from a script where the computer boots up like a clean slate At the very least the terminal will always along with whatever else applications you had when quitting.

I now have two scripts, one for the mac osx interface and one for the ncurses interface both updated and shown below. Make sure you edit the --browse-dir in the ncurses example to reflect where you want vlc to start looking at media files, or remove that bit entirely.

filename:  vlcshutdownm (trailing m is for macosx)
#! /bin/bash
/Applications/VLC.app/Contents/MacOS/VLC &>/dev/null && sudo shutdown -h +2; osascript -e 'tell application "System Events" to shut down'


filename: vlcshutdownn (trailing n is for ncurses)

#! /bin/bash

/Applications/VLC.app/Contents/MacOS/VLC -I ncurses --browse-dir /Users/YourUsernameHere/DirectoryWithMusicEtc && osascript -e 'tell application "System Events" to shut down'

No comments: