Skip to content

Instantly share code, notes, and snippets.

@mateuszgachowski-snippets
Created December 6, 2013 22:19
Show Gist options
  • Save mateuszgachowski-snippets/7833114 to your computer and use it in GitHub Desktop.
Save mateuszgachowski-snippets/7833114 to your computer and use it in GitHub Desktop.

Revisions

  1. mateuszgachowski-snippets revised this gist Dec 8, 2013. 1 changed file with 0 additions and 11 deletions.
    11 changes: 0 additions & 11 deletions itunes.sh
    Original file line number Diff line number Diff line change
    @@ -22,8 +22,6 @@ showHelp () {
    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 " albums = Shows the list of albums with IDs";
    echo " search = Search for a given album name and tries to play it";
    @@ -80,15 +78,6 @@ while [ $# -gt 0 ]; do
    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
  2. mateuszgachowski-snippets revised this gist Dec 8, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion itunes.sh
    Original file line number Diff line number Diff line change
    @@ -16,7 +16,7 @@ showHelp () {
    echo "Options:";
    echo " status = Shows iTunes' status, current artist and track.";
    echo " start = Starts iTunes' if not yet running.";
    echo " play (#) = Start playing iTunes. If # set, plays the given album";
    echo " play = Start playing iTunes.";
    echo " pause = Pause iTunes.";
    echo " next = Go to the next track.";
    echo " prev = Go to the previous track.";
  3. mateuszgachowski-snippets revised this gist Dec 8, 2013. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions itunes.sh
    Original file line number Diff line number Diff line change
    @@ -152,6 +152,7 @@ while [ $# -gt 0 ]; do
    osascript -e 'tell application "iTunes"' -e 'if (exists playlist "temp_playlist") then' -e 'delete playlist "temp_playlist"' -e 'end if' -e 'set name of (make new playlist) to "temp_playlist"' -e 'set theseTracks to every track of playlist "Library" whose artist contains "'"$2"'"' -e 'repeat with thisTrack in theseTracks' -e 'duplicate thisTrack to playlist "temp_playlist"' -e 'end repeat' -e 'set theseTracks to every track of playlist "Library" whose album contains "'"$2"'"' -e 'repeat with thisTrack in theseTracks' -e 'duplicate thisTrack to playlist "temp_playlist"' -e 'end repeat' -e 'set theseTracks to every track of playlist "Library" whose name contains "'"$2"'"' -e 'repeat with thisTrack in theseTracks' -e 'duplicate thisTrack to playlist "temp_playlist"' -e 'end repeat' -e 'play playlist "temp_playlist"' -e 'end tell'
    fi
    break;;

    "stop" ) echo "Stopping iTunes.";
    osascript -e 'tell application "iTunes" to stop';
    break ;;
  4. mateuszgachowski-snippets revised this gist Dec 7, 2013. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions itunes.sh
    Original file line number Diff line number Diff line change
    @@ -44,6 +44,12 @@ while [ $# -gt 0 ]; do
    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";
    echo;
    tracks=`osascript -e 'tell application "iTunes" to get name of every track in current playlist'`;
    export IFS=','
    for t in ${tracks[@]}; do
    echo "$t";
    done
    fi
    break ;;
    'start' ) echo 'Launching iTunes'
  5. mateuszgachowski-snippets revised this gist Dec 7, 2013. 1 changed file with 14 additions and 12 deletions.
    26 changes: 14 additions & 12 deletions itunes.sh
    Original file line number Diff line number Diff line change
    @@ -4,6 +4,7 @@
    # iTunes Command Line Control v1.0
    # written by David Schlosnagle
    # created 2001.11.08
    # @edited Mateusz Gachowski
    ####################################

    showHelp () {
    @@ -14,7 +15,8 @@ showHelp () {
    echo;
    echo "Options:";
    echo " status = Shows iTunes' status, current artist and track.";
    echo " play = Start playing iTunes.";
    echo " start = Starts iTunes' if not yet running.";
    echo " play (#) = Start playing iTunes. If # set, plays the given album";
    echo " pause = Pause iTunes.";
    echo " next = Go to the next track.";
    echo " prev = Go to the previous track.";
    @@ -23,6 +25,8 @@ showHelp () {
    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 " albums = Shows the list of albums with IDs";
    echo " search = Search for a given album name and tries to play it";
    echo " stop = Stop iTunes.";
    echo " quit = Quit iTunes.";
    }
    @@ -46,14 +50,7 @@ while [ $# -gt 0 ]; do
    osascript -e 'tell application "iTunes" to launch';
    break;;
    "play" ) echo "Playing iTunes.";
    if [ $# -eq 2 ]; then
    track_id=$2
    echo 'Play track with ID' $track_id
    osascript -e "tell application \"iTunes\" to play ((first track of current playlist) whose database ID is $track_id)"

    else
    osascript -e 'tell application "iTunes" to play';
    fi
    osascript -e 'tell application "iTunes" to play';
    break ;;

    "pause" ) echo "Pausing iTunes.";
    @@ -92,9 +89,9 @@ while [ $# -gt 0 ]; do
    osascript -e "tell application \"iTunes\" to set sound volume to $newvol";
    break ;;

    "album" )
    id=`osascript -e "tell application \"iTunes\" to (get database ID of every track in current playlist)"`
    album=`osascript -e "tell application \"iTunes\" to (get album of every track in current playlist)"`
    "albums" )
    id=`osascript -e "tell application \"iTunes\" to (get database ID of every track in playlist \"Library\")"`
    album=`osascript -e "tell application \"iTunes\" to (get album of every track in playlist \"Library\")"`

    #split IDs based on ','
    export IFS=","
    @@ -144,6 +141,11 @@ while [ $# -gt 0 ]; do

    break;;

    "search" ) echo 'Searching albums...';
    if [ $# -eq 2 ]; then
    osascript -e 'tell application "iTunes"' -e 'if (exists playlist "temp_playlist") then' -e 'delete playlist "temp_playlist"' -e 'end if' -e 'set name of (make new playlist) to "temp_playlist"' -e 'set theseTracks to every track of playlist "Library" whose artist contains "'"$2"'"' -e 'repeat with thisTrack in theseTracks' -e 'duplicate thisTrack to playlist "temp_playlist"' -e 'end repeat' -e 'set theseTracks to every track of playlist "Library" whose album contains "'"$2"'"' -e 'repeat with thisTrack in theseTracks' -e 'duplicate thisTrack to playlist "temp_playlist"' -e 'end repeat' -e 'set theseTracks to every track of playlist "Library" whose name contains "'"$2"'"' -e 'repeat with thisTrack in theseTracks' -e 'duplicate thisTrack to playlist "temp_playlist"' -e 'end repeat' -e 'play playlist "temp_playlist"' -e 'end tell'
    fi
    break;;
    "stop" ) echo "Stopping iTunes.";
    osascript -e 'tell application "iTunes" to stop';
    break ;;
  6. mateuszgachowski-snippets revised this gist Dec 6, 2013. 1 changed file with 63 additions and 2 deletions.
    65 changes: 63 additions & 2 deletions itunes.sh
    Original file line number Diff line number Diff line change
    @@ -42,9 +42,18 @@ while [ $# -gt 0 ]; do
    echo "Current track $artist: $track";
    fi
    break ;;

    'start' ) echo 'Launching iTunes'
    osascript -e 'tell application "iTunes" to launch';
    break;;
    "play" ) echo "Playing iTunes.";
    osascript -e 'tell application "iTunes" to play';
    if [ $# -eq 2 ]; then
    track_id=$2
    echo 'Play track with ID' $track_id
    osascript -e "tell application \"iTunes\" to play ((first track of current playlist) whose database ID is $track_id)"

    else
    osascript -e 'tell application "iTunes" to play';
    fi
    break ;;

    "pause" ) echo "Pausing iTunes.";
    @@ -83,6 +92,58 @@ while [ $# -gt 0 ]; do
    osascript -e "tell application \"iTunes\" to set sound volume to $newvol";
    break ;;

    "album" )
    id=`osascript -e "tell application \"iTunes\" to (get database ID of every track in current playlist)"`
    album=`osascript -e "tell application \"iTunes\" to (get album of every track in current playlist)"`

    #split IDs based on ','
    export IFS=","

    #get database ID of tracks
    i=0
    for id in $id
    do
    id_a[$i]=`echo $id`
    i=$((i+1))
    done

    echo "Total number of songs in current playlist:" ${#id_a[@]}
    echo "\tFor every album in DB, display album name and first track ID of album."

    #disable separator
    export IFS=""

    ALBUM=( )
    for (( i = 0 ; i < ${#id_a[@]} ; i++ ))
    do
    NEW_ALBUM=`osascript -e "tell application \"iTunes\" to get album of (every track where database ID is ${id_a[$i]})"`
    if [ ${#ALBUM[@]} -eq 0 ]; then

    ALBUM=( "${ALBUM[@]}" "$NEW_ALBUM" )

    else

    MARK=1

    for j in ${ALBUM[@]}; do

    if [[ $j == "$NEW_ALBUM" ]] ; then
    MARK=0
    break
    fi

    done

    if [ $MARK -ne 0 ]; then
    ALBUM=( "${ALBUM[@]}" "$NEW_ALBUM" )
    echo ${id_a[$i]} $NEW_ALBUM
    fi

    fi
    done

    break;;

    "stop" ) echo "Stopping iTunes.";
    osascript -e 'tell application "iTunes" to stop';
    break ;;
  7. mateuszgachowski-snippets created this gist Dec 6, 2013.
    98 changes: 98 additions & 0 deletions itunes.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,98 @@
    #!/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