osascript

Latest

  • Terminal Tip: "Now Playing" info from the command line

    by 
    Erica Sadun
    Erica Sadun
    04.02.2007

    Have you ever wondered whether there was a command line way to check iTunes and determine which song is currently playing. The command-line osascript utility makes this an easy problem to solve. Osascript executes AppleScript commands from Terminal's command line and can run many of the same kinds of simple scripts you'd normally run in Script Editor. Here's a quick how-to to build a Now Playing command line utility. Launch Terminal. In Terminal, enter: touch nowplaying In Terminal, enter: open -e nowplaying In TextEdit, paste the following into nowplaying. The second line wraps here but should copy and paste correctly--at least it did when I tried it out.#! /bin/bashosascript -e 'tell application "iTunes" to if player state is playing then "Now Playing: " & name of current track & " - " & artist of current track' Save the file in TextEdit and close it. In Terminal, enter: chmod 755 nowplaying Open iTunes and start playing a song. In Terminal, enter: ./nowplaying The script, when run, outputs the track name and artist. % ./nowplayingNow Playing: Everything I've Got - Iain Archer%Thanks Dave M %Gallery-2397% Update: For those of you who don't want to keep iTunes open:#! /bin/bashosascript -e 'tell application "System Events" to if ((name of processes) contains "iTunes") then do shell script ("osascript -e " & quoted form of ("tell application \"iTunes\" to if player state is playing then \"Now Playing: \" & name of current track & \" - \" & artist of current track" & ""))'