Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Controlling iTunes from the Terminal Apps
[Editor's addition: I received two different 'iTunes command line' hint submissions at nearly the same time. In the interest of ease of use in the future, I'm combining them in this one posting.

The first 'iTunes terminal script' tip, submitted by Stu G., reads as follows:

"I come from a more unix background and have always controlled xmms (a mp3 player) from the command line, usually remotely. To my knowledge, iTunes alone does not do this. So using the new scriptability of version 2.01 and the 'osascript' command, I tossed together some quick aliases I use with my tcsh:
alias next      osascript -e 'tell application "iTunes"'[space]
-e "next track" -e "end tell"
alias prev osascript -e 'tell application "iTunes"'[space]
-e "previous track" -e "end tell"
alias pause osascript -e 'tell application "iTunes"'[space]
-e "pause" -e "end tell"
[NOTE: Enter as one line each, replace [space] with the actual space character]. Stop and play, etc are just as easy to create. What I think would be nice, is if some one takes the scripting of iTunes and smashes these controls into say the Services menu, then there would be semi-global keyboard control of iTunes."

The second tip, submitted by the author of this article, David S., is more of a full command line interface for iTunes. I've installed it on my machine, and it's very nicely done. The rest of this tip is his text... -rob.]



Now that iTunes has built-in AppleScript support, everyone is making AppleScripts to control it. I felt left out since I haven't found a script to control it from the command line. This little shell script I made should fill that void. I have put some of iTunes features I use most into it.

If you're interested in controlling iTunes from the Terminal, read the rest of the article...

Create the following script using either a Terminal text editor such as Pico or vi, or use TextEdit/BBEdit and make sure you save the fle as pure text.
#!/bin/sh
#
####################################
# iTunes Command Line Control v1.0
# written by David Schlosnagle
# created 2001.11.08
####################################

showHelp () {
echo "-----------------------------";
echo "iTunes Command Line Interface";
echo "-----------------------------";
echo "Usage: `basename $0` <option>";
echo;
echo "Options:";
echo " status = Shows iTunes' status, current artist and track.";
echo " play = Start playing iTunes.";
echo " pause = Pause iTunes.";
echo " next = Go to the next track.";
echo " prev = Go to the previous track.";
echo " mute = Mute iTunes' volume.";
echo " unmute = Unmute iTunes' volume.";
echo " vol up = Increase iTunes' volume by 10%";
echo " vol down = Increase iTunes' volume by 10%";
echo " vol # = Set iTunes' volume to # [0-100]";
echo " stop = Stop iTunes.";
echo " quit = Quit iTunes.";
}

if [ $# = 0 ]; then
showHelp;
fi

while [ $# -gt 0 ]; do
arg=$1;
case $arg in
"status" ) state=`osascript -e 'tell application "iTunes" to player state as string'`;
echo "iTunes is currently $state.";
if [ $state = "playing" ]; then
artist=`osascript -e 'tell application "iTunes" to artist of current track as string'`;
track=`osascript -e 'tell application "iTunes" to name of current track as string'`;
echo "Current track $artist: $track";
fi
break ;;

"play" ) echo "Playing iTunes.";
osascript -e 'tell application "iTunes" to play';
break ;;

"pause" ) echo "Pausing iTunes.";
osascript -e 'tell application "iTunes" to pause';
break ;;

"next" ) echo "Going to next track." ;
osascript -e 'tell application "iTunes" to next track';
break ;;

"prev" ) echo "Going to previous track.";
osascript -e 'tell application "iTunes" to previous track';
break ;;

"mute" ) echo "Muting iTunes volume level.";
osascript -e 'tell application "iTunes" to set mute to true';
break ;;

"unmute" ) echo "Unmuting iTunes volume level.";
osascript -e 'tell application "iTunes" to set mute to false';
break ;;

"vol" ) echo "Changing iTunes volume level.";
vol=`osascript -e 'tell application "iTunes" to sound volume as integer'`;
if [ $2 = "up" ]; then
newvol=$(( vol+10 ));
fi

if [ $2 = "down" ]; then
newvol=$(( vol-10 ));
fi

if [ $2 -gt 0 ]; then
newvol=$2;
fi
osascript -e "tell application \"iTunes\" to set sound volume to $newvol";
break ;;

"stop" ) echo "Stopping iTunes.";
osascript -e 'tell application "iTunes" to stop';
break ;;

"quit" ) echo "Quitting iTunes.";
osascript -e 'tell application "iTunes" to quit';
exit 1 ;;

"help" | * ) echo "help:";
showHelp;
break ;;
esac
done
To use the file, save it somewhere in your $path. Then 'chmod 755 filename' to make it executable. Type 'rehash'. Then you can start using it. Personally mine is named 'itunes', so to start playing I use the command:
itunes play
If you just type "itunes" it will list the options. Currently the options are:
status   = Shows iTunes' status, current artist and track.
play = Start playing iTunes.
pause = Pause iTunes.
next = Go to the next track.
prev = Go to the previous track.
mute = Mute iTunes' volume.
unmute = Unmute iTunes' volume.
vol up = Increase iTunes' volume by 10%
vol down = Increase iTunes' volume by 10%
vol # = Set iTunes' volume to # [0-100]
stop = Stop iTunes.
quit = Quit iTunes.
If anyone has any suggestions, I'd love to hear them. Especially if there is a way to speed these AppleScript commands up.

-David Schlosnagle
http://www.david-s.net
    •    
  • Currently 3.83 / 5
  You rated: 1 / 5 (6 votes cast)
 
[111,944 views]  

Controlling iTunes from the Terminal | 35 comments | Create New Account
Click here to return to the 'Controlling iTunes from the Terminal' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Two suggestions:
Authored by: mithras on Nov 09, '01 10:27:23AM
First, to allow this script to be run via SSH from another machine: (as I note in the MacNN forum where David also posted this) Change the Applescripts slightly to use the Remote AppleEvent syntax:
tell application "iTunes" of machine "eppc://127.0.0.1" to play the current track
For the above to work, you'll need: (1) To click the checkbox "Allow remote Apple Events" in the Sharing control panel (2) To be physically present at the machine to enter authorization name & password. If you save the authorization to your Keychain, you won't have to enter it again. This loops back the AppleEvents to the same machine that you are SSH'd into, which presumably is the same machine where iTunes is running. I imagine you could also change the IP address from the loopback 127.0.0.1 to the IP of whatever machine is running iTunes; this would let you run the scripts on a local Terminal, to control a remote iTunes.
Second, to speed up the scripts: Applescript is compiling those scripts each time. It would be faster to precompile them, using the Script Editor or the osacompile tool, and save them to separate files. Then change the shell script to 'osascript /Library/Scripts/iTunes/play.scpt' or whatever location you settle on. It's still not exactly speedy, but it helps.

[ Reply to This | # ]
Two suggestions:
Authored by: eskilling on May 30, '02 04:24:40PM

I've made the changes but the problem I seem to be running into is this error:

can't get the applications event dictionary (-2709)

I got this before I made the changes to David's original script. It runs great locally but if I ssh from another machine I get that.



[ Reply to This | # ]
Two suggestions:
Authored by: David Kennedy on Mar 17, '03 05:31:49PM

Okay, I have compiled the various small functions ('play', 'pause', etc) as AppleScripts, but am puzzled as how to manage the 'vol' one? Any suggestions as to how to handle this one? I'm new AppleScript and dunno how to pass params etc.



[ Reply to This | # ]
Run as ssh
Authored by: logo on Mar 18, '03 04:42:41AM

You will probably not need the addition of the host inside the applescript.

Assuming you have installed the script on the target machine you can run it remotely using ssh.

Once the ssh login to the machine running itunes has succeeded the applescript is executed locally right there...

you can even run the script in one line with:
ssh <login here>@<remotemachine here> /<pathtoscript>/itunes play
this will prompt for the password and then your itunes will come to life!!!



[ Reply to This | # ]
command not found
Authored by: shack on Nov 10, '01 12:27:26AM
Okay, I'm baffled. Saved this into my ~/bin directory. chmod 755 itunes. rehashed. Command not found. Closed all Terminal instances and tried again. Command not found.
[open-131-161-241-7:~] shacker% rehash
[open-131-161-241-7:~] shacker% which itunes
/Users/shacker/bin/itunes
[open-131-161-241-7:~] shacker% itunes
itunes: Command not found.
[open-131-161-241-7:~] shacker% echo $PATH
/Users/shacker/bin/powerpc-apple-darwin:/Users/shacker/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin
[open-131-161-241-7:~] shacker%
This makes no sense. What am I missing? There are other things in my ~/bin that work fine. What the heck?

[ Reply to This | # ]
Very strange
Authored by: robg on Nov 10, '01 01:54:28AM

I had mine in /usr/local/bin, but just moved it to check. Works fine, and my path matches yours (more or less; it includes the ~/bin, which is what counts).

It's very odd that 'which' will give you the right answer but it still won't run. I'll email you my saved script privately, just in case there's something odd there.

-rob.



[ Reply to This | # ]
command not found
Authored by: Taran137 on Nov 10, '01 09:28:37AM

I am having the same problem as robg.



[ Reply to This | # ]
command not found
Authored by: jimhill on Nov 10, '01 09:08:48PM

The command which is not found is probably not the itunes script, since "which" found it with no problems. Look at system executables that this scripts calls: /bin/sh, and osascript. Try "which"ing each of them and see if the machine has trouble finding either. Dollars to donuts it'll be osascript that's the offender, since I'd be pretty baffled if you managed to get an OS X installation without /bin/sh.



[ Reply to This | # ]
command not found
Authored by: Taran137 on Nov 11, '01 01:12:16PM

[localhost:~] taran% which osascript
/usr/bin/osascript

[localhost:~] taran% which sh
/bin/sh

[localhost:~] taran% which itunes
/Users/taran/bin/itunes

That is not it.



[ Reply to This | # ]
command not found
Authored by: _merlin on Nov 11, '01 06:32:43PM

Check your script for typos



[ Reply to This | # ]
solved! - command not found
Authored by: tcurtin on Jan 17, '03 05:12:00PM

I'm guessing you had the same problem as me - it has to do with the line breaks in the file you copied over. I selected the text on the macosxhints page, and dragged it over into BBEdit. "command not found"

Open terminal, cd into the directory containing the script, and type
more itunes (or whatever you called the script)

You'll only see a few lines, all garbled. The fix (for me) was to change from Macintosh to Unix newlines using BBEdit. You can download a trial version that will let you do this - you can also do it using sed or perl, but I don't have the time now to create the script, and it would probably just cloud the issue anyway. :)

Good luck!



[ Reply to This | # ]
"option" not found
Authored by: dlgraves on Mar 17, '03 12:05:08PM

hi

I have a similar problem: the 'itunes' script runs but breaks when it gets to this line:

echo \"Usage: `basename $0` <option>\";

At that point the script says:

/Users/dlg/bin/itunes: option: No such file or directory

Does anyone understand what's going on? If this is an error within the script I don't see how it could work for other people.
Thanks a lot.

Lucas



[ Reply to This | # ]
"option" not found
Authored by: logo on Mar 18, '03 04:16:47AM

>> echo \"Usage: `basename $0` <option>\";

Remove the backslashes!

Currently you echoing " and then issuing the command with read input (<) from option...
and not an echo of the string containing "...<option>"

HTH



[ Reply to This | # ]
"option" not found
Authored by: mervTormel on Mar 18, '03 12:37:44PM

it's a little more complicated than that.

basename can take an argument, and if $0 is -bash (interactive bash shell), then basename gaks on the -switch.

so, pass basename the -- switch so that it stops processing arguments...

echo "Usage: `basename -- $0` <option>";

this works on the command line; you may need to quote it up some for your shell/script



[ Reply to This | # ]
a couple small additions
Authored by: reverie on Nov 11, '01 05:33:16AM

I have a huge mp3 library stored over several different locations (different folders, as well as different networked machines!), so it's kind of a pain to keep everything in the itunes music library. I have a simple alias to open up a folder of music in itunes:

alias itunes 'open -a iTunes'

so that "itunes /mp3s/Coil*" will load up and start playing all of my Coil mp3s.

Also, the initial aliases in this thread were a little more wordy than necessary. Try this out to save yourself a few keystrokes (and make the code more maintainable):

alias next 'osascript -e '"'"'tell application "iTunes" to next track'"'"
alias prev 'osascript -e '"'"'tell application "iTunes" to previous track'"'"

The ugly quoting up there is to make use of nested quoting--another problem I had with the initial posting was that I couldn't get the aliases to work! I think all aliases with spaces in them need to be surrounded by quotes, at least when you have them in your .cshrc (in fact, I highly recommend using quotes in all of your unix scripting--I'm sure we all know by now what nightmares await those who skimp on quotes).



[ Reply to This | # ]
one more thing...
Authored by: reverie on Nov 11, '01 05:46:12AM

The original article mentioned wanting global keyboard control of itunes, and that one way to do this would be through the Services menu. It doesn't seem like a great idea to do that, first because Services is designed more for dealing with data in the current app ("Cut and Paste on Steroids"), and second because there's already a way to control itunes with about as many keystrokes as it would take to access an item in the Services menu. Assuming that you have Full Keyboard Access turned on (which I assume you do if you're talking about getting to the Services menu from the keyboard), you can just activate the dock, arrow over to the itunes icon, then either up- or down- arrow to get the popup menu which has more control and info than is possible with applescript.



[ Reply to This | # ]
Controlling iTunes from the Terminal
Authored by: andrewgwhite on Jun 08, '03 04:49:16PM

I found that issuing 'itunes vol up' or 'vol down' would cause the script to barf when it hit 'if [ $2 -gt 0 ]; then', since that expression is testing for an integer. My fix, nasty as all get out probably, was to rewrite the case for vol thusly:

"vol" ) echo "Changing iTunes volume level.";
vol=`osascript -e 'tell application "iTunes" to sound volume as integer'`;
if [ $2 = "up" ]; then
newvol=$(( vol+10 ));
osascript -e "tell application \"iTunes\" to set sound volume to $newvol";
break ;
fi

if [ $2 = "down" ]; then
newvol=$(( vol-10 ));
osascript -e "tell application \"iTunes\" to set sound volume to $newvol";
break ;
fi

if [ $2 -gt 0 ]; then
newvol=$2;
osascript -e "tell application \"iTunes\" to set sound volume to $newvol";
break ;
fi
break ;;

No one else has reported this issue, but if they stumble on it, I hope this helps. Also, if anyone has any ideas on how to do this a little more cleanly, please give me a shout.

Cheers,
a.



[ Reply to This | # ]
Controlling iTunes from the Terminal
Authored by: linky on Apr 24, '04 05:22:38PM
All you need to do is make this change to the offending line (line 80, the way I saved it:)
if [ $(($2)) -gt 0 ]; then
The bash manpage will tell you the $((...)) operator forces arithmetic expansion, which eliminates the comparison between a string and an integer.

[ Reply to This | # ]
Controlling iTunes from the Terminal
Authored by: teahaile on Aug 30, '06 06:26:08AM
the following makes more sense to me, logically:
            "vol"    ) echo "Changing iTunes volume level."
;
            if [ $($2) ]; then
                vol=`osascript -e 'tell application "iTunes
" to sound volume as integer'`;
                if [ $2 = "up" ]; then
                    newvol=$(( vol+10 ));

                elif [ $2 = "down" ]; then
                    newvol=$(( vol-10 ));

                elif [ $2 -gt 0 ]; then
                    newvol=$2;
                fi

                osascript -e "tell application "iTunes" t
o set sound volume to $newvol";
            fi


[ Reply to This | # ]
Controlling iTunes from the Terminal (last post was ERROR)
Authored by: teahaile on Aug 30, '06 06:47:22AM
last entry was an error on my part....i meant to suggest the following:
            "vol"    ) echo "Changing iTunes volume level."
;
            if [ -z $2 ]; then
                break;
            fi
                vol=`osascript -e 'tell application "iTunes
" to sound volume as integer'`;
                if [ $2 = "up" ]; then
                    newvol=$(( vol+10 ));

                elif [ $2 = "down" ]; then
                    newvol=$(( vol-10 ));

                elif [ $2 -gt 0 ]; then
                    newvol=$2;
                fi

                osascript -e "tell application "iTunes" t
o set sound volume to $newvol";

            break ;;


[ Reply to This | # ]
Add playlist control to the script
Authored by: kahless on Dec 18, '03 02:43:57PM
One more suggestion. Add the ability to choose a playlist. I added an itunes playlist "name of playlist" command. Here is the code I added to the original script. Add
echo " playlist "@" = Play iTunes' playlist named @";
to the showHelp and add
"playlist" ) echo "Changing iTunes playlist."; osascript -e 'tell application "iTunes"' -e "set the new_playlist to \"$2\" as string" -e "play playlist new_playlist" -e "end tell"; break ;;
in the main body of the script. I put it after the vol command.

[ Reply to This | # ]
Add playlist control to the script (correction)
Authored by: kahless on Mar 09, '04 01:24:22PM
The quotes around $2 need to be escaped as \"$2\".

[ Reply to This | # ]
Add playlist control to the script
Authored by: the.hudi on Jun 18, '08 05:10:54AM
another suggestion: For the help:

echo " playlist = Show playlists saved in iTunes.";
And the code for showing available playlists, if there is no parameter given.

## addition playlist of choice
"playlist" )
if [ -n "$2" ]; then
echo "Changing iTunes playlists to '$2'.";
osascript -e 'tell application "iTunes"' -e "set new_playlist to \"$2\" as string" -e "play playlist new_playlist" -e "end tell"; 
break ;
else
# Show available iTunes playlists.
echo "Playlists:";
osascript -e 'tell application "iTunes"' -e "set allPlaylists to (get name of every playlist)" -e "end tell";
break;
fi
break;;


[ Reply to This | # ]
Controlling iTunes from the Terminal
Authored by: chris_on_hints on Mar 09, '04 05:01:34AM

This hint has been great for controlling iTunes (v4.2 on OSX 10.3.2) from my windows XP machine (from accross the room!). I would prefer a web interface, but i can only find shareware ones - outside my budget at the moment....

But, in Panther, the fast user switching causes a problem... The applescript events only work while i remain logged in. As soon as a fast user switch happens, my remote terminal complains as follows:

"kCGErrorRangeCheck : Window server communication from outside of session allowed for root and console user only"

any ideas? Im going to post this in the forums as well....



[ Reply to This | # ]
Controlling iTunes from the Terminal script problem
Authored by: red_duc on Jul 10, '04 02:40:50PM

when running the script from David Schlosnagle and trying to change the vol up or vol down I get the following:

sh-2.05b$ ./itunes vol up
Changing iTunes volume level.
./itunes: line 80: [: up: integer expression expected



[ Reply to This | # ]
shuffling
Authored by: deef on Feb 24, '05 07:46:46PM
To enable shuffling add the following lines to the while command in David Schlosnagle's script above:

"shuf" ) echo "Shuffle is ON.";
osascript -e 'tell application "iTunes" to set shuffle of current playlist to 1';
break ;;

"nosh" ) echo "Shuffle is OFF.";
osascript -e 'tell application "iTunes" to set shuffle of current playlist to 0';
break ;;


You can also add the following to the showHelp section to display these commands in the list of options:

echo " shuf = Shuffle current playlist";
echo " nosh = Do not shuffle current playlist";


[ Reply to This | # ]
Controlling iTunes from the Terminal
Authored by: byronnash on Mar 01, '05 02:53:48PM

I am trying to add in a command to start itunes using this script. I've added this line:

tell application "iTunes" of machine "eppc://127.0.0.1" to activate

I get an error. It says the application isn't running.



[ Reply to This | # ]
Opening internet radio stream
Authored by: matt448 on Aug 14, '06 07:54:32AM
One more little addition. I added this to the script running on my machine. It allows you to play a streaming location from the command line.

Command section:
"openstream" ) echo "Playing $2."
osascript -e 'tell application "iTunes"' -e "open location "$2"" -e "end tell";
break ;;

Help section:
echo " openstream "@" = Play iTunes' stream named @";

Sample command line:
itunes openstream http://www.club977.com/CLUB977HI.pls


[ Reply to This | # ]
Controlling iTunes from the Terminal
Authored by: teahaile on Aug 30, '06 06:25:01AM
the following makes more sense to me, logically:
            "vol"    ) echo "Changing iTunes volume level."
;
            if [ $($2) ]; then
                vol=`osascript -e 'tell application "iTunes
" to sound volume as integer'`;
                if [ $2 = "up" ]; then
                    newvol=$(( vol+10 ));

                elif [ $2 = "down" ]; then
                    newvol=$(( vol-10 ));

                elif [ $2 -gt 0 ]; then
                    newvol=$2;
                fi

                osascript -e "tell application "iTunes" t
o set sound volume to $newvol";
            fi


[ Reply to This | # ]
added search function
Authored by: mconstant on Jan 28, '07 10:40:50AM

here's my kludgy version of Schlosnagle's old script.

-If you type 'itunes search beatles' at the terminal, it will do a query for beatles songs, populate a playlist called foo with the songs (you need to create this manually,) and start playing it.
-Every time you start a new query you need to type 'itunes clear' to clear playlist foo.

#!/bin/sh
#
#######################################
# iTunes Command Line Control v1.0.0.1
# written by David Schlosnagle
# created 2001.11.08
# modified 2007.01.28 by David West
# filename=itunes
#######################################

showHelp () {
echo "-----------------------------";
echo "iTunes Command Line Interface";
echo "-----------------------------";
echo "Usage: `basename $0` ";
echo;
echo "Options:";
echo " status = Shows iTunes' status, current artist and track.";
echo " play = Start playing iTunes.";
echo " pause = Pause iTunes.";
echo " next = Go to the next track.";
echo " prev = Go to the previous track.";
echo " mute = Mute iTunes' volume.";
echo " unmute = Unmute iTunes' volume.";
echo " vol up = Increase iTunes' volume by 10%";
echo " vol down = Increase iTunes' volume by 10%";
echo " vol # = Set iTunes' volume to # [0-100]";
echo " stop = Stop iTunes.";
echo " search = Run query, populate playlist named foo and play. "
echo " clear = clear playlist foo. ";
echo " quit = Quit iTunes.";
}

if [ $# = 0 ]; then
showHelp;
fi

while [ $# -gt 0 ]; do
arg=$1;
songname=$2;
echo $songname;
case $arg in
"status" ) state=`osascript -e 'tell application "iTunes" to player state as string'`;
echo "iTunes is currently $state.";
if [ $state = "playing" ]; then
artist=`osascript -e 'tell application "iTunes" to artist of current track as string'`;
track=`osascript -e 'tell application "iTunes" to name of current track as string'`;
echo "Current track $artist: $track";
fi
break ;;

"play" ) echo "Playing iTunes.";
osascript -e 'tell application "iTunes" to play';
break ;;

"pause" ) echo "Pausing iTunes.";
osascript -e 'tell application "iTunes" to pause';
break ;;

"next" ) echo "Going to next track." ;
osascript -e 'tell application "iTunes" to next track';
break ;;

"prev" ) echo "Going to previous track.";
osascript -e 'tell application "iTunes" to previous track';
break ;;

"mute" ) echo "Muting iTunes volume level.";
osascript -e 'tell application "iTunes" to set mute to true';
break ;;

"unmute" ) echo "Unmuting iTunes volume level.";
osascript -e 'tell application "iTunes" to set mute to false';
break ;;

"vol" ) echo "Changing iTunes volume level.";
vol=`osascript -e 'tell application "iTunes" to sound volume as integer'`;
if [ $2 = "up" ]; then
newvol=$(( vol+10 ));
fi

if [ $2 = "down" ]; then
newvol=$(( vol-10 ));
fi

if [ $2 -gt 0 ]; then
newvol=$2;
fi
osascript -e "tell application "iTunes" to set sound volume to $newvol";
break ;;

"stop" ) echo "Stopping iTunes.";
osascript -e 'tell application "iTunes" to stop';
break ;;

"quit" ) echo "Quitting iTunes.";
osascript -e 'tell application "iTunes" to quit';
exit 1 ;;

"search" ) echo "Searching Library.";
songname=$2;
osascript -e "tell application "iTunes"" -e "set searchResults to search playlist "Library" for "$songname"" -e "repeat with aTrack in searchResults" -e "copy aTrack to playlist "foo"" -e "end repeat" -e "play playlist "foo"" -e "end tell";
break ;;

"clear" ) echo "Clearing Query.";
osascript -e "tell application "iTunes" to delete tracks of playlist "foo"";
break ;;

"help" | * ) echo "help:";
showHelp;
break ;;
esac
done



[ Reply to This | # ]
Cannot rehash
Authored by: Schmoops on Jan 08, '08 05:30:17PM

I got to the part where it says run 'chmod 755 filename'. I did this without the quotes. for me the filename is ~/bin/itunes.txt
it won't let me rehash, it says command not found.
when i type echo $path, i get a blank line
I have no clue what i doing wrong



[ Reply to This | # ]
Cannot rehash
Authored by: the.hudi on Jun 18, '08 05:20:35AM
Try to append the line:
 export PATH=$PATH:/Users/<your user name>/bin 
to the file '~/bin/.bash_profile This should work.

[ Reply to This | # ]
Controlling iTunes from the Terminal
Authored by: the.hudi on Jun 18, '08 05:13:04AM
another suggestion: For the help:

echo " playlist = Show playlists saved in iTunes.";
And the code for showing available playlists, if there is no parameter given.

## addition playlist of choice
"playlist" )
if [ -n "$2" ]; then
echo "Changing iTunes playlists to '$2'.";
osascript -e 'tell application "iTunes"' -e "set new_playlist to \"$2\" as string" -e "play playlist new_playlist" -e "end tell"; 
break ;
else
# Show available iTunes playlists.
echo "Playlists:";
osascript -e 'tell application "iTunes"' -e "set allPlaylists to (get name of every playlist)" -e "end tell";
break;
fi
break;;


[ Reply to This | # ]
Combine bash w/ applescript
Authored by: jeberle-mac on Oct 25, '08 01:01:08PM
I'm glad I found this hint (I'm a former XMMS2 junkie on FreeBSD). You can actually open up the command structure of iTunes directly to the command line. This approach (below) uses a couple short mapping functions, and a mega hint function.

I store this in a file called ~/shell/itunes.sh, and then source it (dot it) from ~/.bashrc, e.g. . ~/shell/itunes.sh


itunes() {
  if [ -z "$1" -o "$1" = '-h' -o "$1" = '--help' ]; then
    itunes-hints
  else
    osascript -e 'tell app "iTunes" to '"$*"
  fi
}

vol() {
  local vol=$(itunes get sound volume)
  if [[ -z "$1" ]]; then
    echo $vol
  elif [[ "$1" = up ]]; then
    itunes set sound volume to $((vol+10))
  elif [[ "$1" = down ]]; then
    itunes set sound volume to $((vol-10))
  elif [[ "$1" =~ ^[0-9]{1,3}$ ]]; then
    itunes set sound volume to "$1"
  else
    echo vol '[up | down | <number>]'
  fi
}

itunes-hints() {
  cat - <<EOS
Usage:
  itunes play
  itunes stop
  itunes playpause
  itunes set mute to 1
  itunes set mute to 0
  itunes quit

  itunes next track
  itunes previous track
  itunes play playlist \"name\"
  itunes play track 3 of current playlist

  itunes get player state
  itunes get player position

  itunes get artist of current track
  itunes get name of current track

  itunes get current stream title
  itunes get current stream URL

  itunes get name of every playlist
  itunes get name of every track in current playlist
  itunes get name of track 3 of current playlist

  itunes set position of window \"iTunes\" to '{0,22}'
  itunes set bounds of window \"iTunes\" to '{0,22,800,600}'
  itunes set visible of window \"iTunes\" to 1

  : ... anything you can tell iTunes via OSA
  : See /Applications/AppleScript/Script Editor

  vol up       - Increase volume by 10%
  vol down     - Decrease volume by 10%
  vol <0-100>  - Set volume from 0 to 100%
EOS
}


[ Reply to This | # ]
Controlling iTunes from the Terminal
Authored by: reasonstoprefer on Jul 06, '09 10:51:50AM
I am using this script to control my itunes with my BlackBerry 8820. So far it has been a great success. All it takes is midpSSH which is SSH software for mobile phones. If you have a dynamic IP address it would be useful to use a service that will keep it updated and linked to a URL.

http://img197.imageshack.us/img197/6887/photo1afm.jpg
http://img196.imageshack.us/img196/2147/picture3qlf.jpg

I edited this script in an attempt to make it more useful. Since this was (almost) my first attempt to do something with applescript, I only made limited additions to the code. On the other hand, it hints at how much potential the script has. Here is what I added:
## search and play
"search" ) 
clear;
osascript -e "set searchTerm to\"$2\"" -e 'tell application "iTunes"
if playlist searchTerm exists then
		set max to number of tracks of playlist searchTerm
		set output to {}
		set counter to 0
		repeat while counter < max
			set counter to counter + 1
			set output to output & "[" & counter &".] " & name of track counter of playlist searchTerm & "
"
		end repeat
		reveal playlist searchTerm
		play playlist searchTerm
		set thisPlaylist to current playlist
		set thisArtist to artist of current track
		set thisTrack to name of current track
		set trackNumber to index of current track
		set output to "Currently playing track << " & trackNumber & ". " & thisTrack & " \\\\ " & thisArtist & " >> 

" & output
		get output
		
	else
		make playlist with properties {name:searchTerm}
		duplicate (tracks of library playlist 1 whose artist contains searchTerm or album contains searchTerm or name contains searchTerm) to playlist searchTerm
		set max to number of tracks of playlist searchTerm
		set output to {}
		set counter to 0
		repeat while counter < max
			set counter to counter + 1
			set output to output & "[" & counter &".] " & name of track counter of playlist searchTerm & "
"
		end repeat
		reveal playlist searchTerm
		play playlist searchTerm
		set thisPlaylist to current playlist
		set thisArtist to artist of current track
		set thisTrack to name of current track
		set trackNumber to index of current track
		set output to "Currently playing track << " & trackNumber & ". " & thisTrack & " \\\\ " & thisArtist & " >> 

" & output
		get output
	end if
	
	
end tell'; 
break ;; 

##skip to track in playlist
"skipto" ) 
clear;
osascript -e "set skipTo to $2" -e 'tell application "iTunes"
	play track (skipTo) of current playlist
	set max to number of tracks of current playlist
	set output to {}
	set counter to 0
	repeat while counter < max
		set counter to counter + 1
		set output to output & "[" & counter & ".] " & name of track counter of current playlist & "
"
	end repeat
	set thisPlaylist to current playlist
	set thisArtist to artist of current track
	set thisTrack to name of current track
	set trackNumber to index of current track
	set output to "Currently playing track << " & trackNumber & ". " & thisTrack & " \\\\ " & thisArtist & " >> 

" & output
	get output
end tell';
break;;
It adds a basic search function. You can search for a song, album, or artist with the command itunes search searchterm with itunes being the name of the script and searchterm being the search term. This will call up a list of search results and start playing them from the top. To skip to a number on that list you enter itunes skipto 10 (replace 10 with the number of the track you want to play).

[ Reply to This | # ]