Skip to content

Instantly share code, notes, and snippets.

@Leask
Last active May 14, 2018 14:15
Show Gist options
  • Save Leask/4149767 to your computer and use it in GitHub Desktop.
Save Leask/4149767 to your computer and use it in GitHub Desktop.
Control iTunes in Terminal
#!/usr/bin/php
<?php
# Flora iTunes CLI by LeaskH.com
# Based on David Schlosnagle & David West's work
$based = 'tell application "iTunes"';
$list = 'Instance';
$actions = array(
'status' => array(
'',
array(),
),
'play' => array(
'Start playing iTunes.',
array("{$based} to play"),
),
'pause' => array(
'Pause iTunes.',
array("{$based} to pause"),
),
'stop' => array(
'Stop iTunes.',
array("{$based} to stop"),
),
'next' => array(
'Go to the next track.',
array("{$based} to next track"),
),
'prev' => array(
'Go to the previous track.',
array("{$based} to previous track"),
),
'mute' => array(
'Mute iTunes.',
array("{$based} to mute to true"),
),
'unmute' => array(
'Unmute iTunes.',
array("{$based} to set mute to false"),
),
'quit' => array(
'Quit iTunes.',
array("{$based} to quit"),
),
'search' => array(
"Run query, populate playlist named \"{$list}\" and play.",
array("{$based}",
'set searchResults to search playlist "Library" for "$0"',
"make user playlist with properties {name:(\"{$list}\")}",
'repeat with aTrack in searchResults',
"copy aTrack to playlist \"{$list}\"",
'end repeat',
"play playlist \"{$list}\"",
'end tell'),
),
'clear' => array(
"Clear playlist \"{$list}\".",
array("{$based} to delete playlist \"{$list}\""),
),
);
function help() {
}
function osascript($scripts, $args = array()) {
if ($scripts) {
$strScript = 'osascript';
foreach ($scripts as $script) {
if ($args) {
foreach ($args as $aI => $aItem) {
$strScript .= ' -e ' . escapeshellarg(preg_replace(
'/\$' . $aI . '/', $aItem, $script
));
}
} else {
$strScript .= ' -e ' . escapeshellarg($script);
}
}
echo $strScript;
return shell_exec($strScript);
}
return null;
}
function despatch($action, $args = array()) {
global $actions;
$action = strtolower($action);
if (isset($actions[$action])) {
osascript($actions[$action][1], $args);
} else {
echo "Unknow command.\n";
}
}
# main
array_shift($argv);
if ($argv) {
despatch(array_shift($argv), $argv);
} else {
help();
}
# showHelp () {
# echo "-----------------------------"
# echo "iTunes Command Line Interface"
# echo "-----------------------------"
# echo "Usage: `basename $0`"
# echo "Options:"
# echo " status = Shows iTunes' status, current artist and track."
# echo " vol up = Increase iTunes' volume by 10%"
# echo " vol down = Increase iTunes' volume by 10%"
# echo " vol # = Set iTunes' volume to # [0-100]"
# }
# "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'`
# if [ "$track" = '' ]; then
# exit 0
# fi
# string="You're listening to '$track'"
# if [ ! "$artist" = '' ]; then
# string="$string by $artist"
# fi
# string="$string."
# echo $string
# fi
# "lyric")
# state=`osascript -e 'tell application "iTunes" to player state as string'`
# 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'`
# /Users/leask/Documents/.flora/lyric -n "$track" -a "$artist"
# fi
# "play")
# echo "Playing iTunes."
# "pause")
# echo "Pausing iTunes."
# "next")
# echo "Going to next track."
# "prev")
# echo "Going to previous track."
# "mute")
# echo "Muting iTunes volume level."
# "unmute")
# echo "Unmuting iTunes volume level."
# "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"
# "stop")
# echo "Stopping iTunes."
# "quit")
# echo "Quitting iTunes."
# "search")
# echo "Searching Library."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment