#!/bin/bash #----------- # # File: spotify_osx.sh # Author: Nico. # Date: june 2012. # License: GNU GPL. # Supported OS: Linux (Fedora), OSX # #-------------------------- # # usage: # spotify_osx.sh # 'normal' mode # spotify_osx.sh echo # no action # #-------------------------- tmp1=/tmp/spotify1.$$ tmp2=/tmp/spotify2.$$ tmp3=/tmp/spotify3.$$ tmp4=/tmp/spotify4.$$ tmps="$tmp1 $tmp2 $tmp3 $tmp4" trap "rm -f $tmps; echo 'the end'; exit" 0 1 2 3 15 sec=3 tagdb=$HOME/spotDB.txt #----------- # # Mir:ror user config # #-------------------------- ## RFID tags cat > $tmp2 << EOF 00000462e6580c00000000000000 learn 000004a2e55b0c00000000000000 lock 000004f27a5f0c00000000000000 playpause 00000432f7620c00000000000000 volUp 000004e27d610c00000000000000 volDown EOF ## osx only NODE=/usr/local/bin/node #---------------------------------------------------- #-- end user config --------------------------------- #---------------------------------------------------- #----------- # # Mir:ror main program # #-------------------------- function main() { chmod 777 $tmp4 cmd="$HOME/hidtest" ## osx 10.5.8 if [ ! -f "$cmd" ]; then cmd="$NODE $tmp4" ## osx 10.6 fi # kill existing instances #------------------------- pids=$(ps -u$USER -opid -ocommand | grep "$(echo $cmd | sed -e 's;[0-9]*$;;g')" | grep -v grep | awk '{ print $1 }') if [ "$pids" != "" ]; then kill -INT ${pids} fi state=none # actual loop #------------------------- while [ 1==1 ]; do $cmd | while read line do NF=$(echo $line | awk '{ print NF }') if [ $NF -ne 4 ]; then echo "$line" fi if [ $NF -eq 1 -a "$line" = "again" ]; then break fi if [ "$*" = "echo" ]; then echo "$line" continue fi echo "$line" tag=$(echo $line | awk '{ print $3 }') mode=$(echo $line | awk '{ print $4 }') command=$(grep $tag $tmp2 | awk '{ print $2 }') if [ "$command" = "lock" -a "$mode" = "IN" -a "$state" = "none" ]; then state="locked" elif [ "$command" = "lock" -a "$mode" = "IN" -a "$state" = "locked" ]; then state="none" elif [ "$state" = "locked" ]; then echo "locked" continue elif [ "$command" = "volUp" -a "$mode" = "IN" ]; then vol=$(osascript -e 'tell application "Spotify" to sound volume') vol=$(($vol+10)) if [ $vol -gt 100 ]; then vol=100; fi osascript -e "tell application \"Spotify\" to set sound volume to $vol" elif [ "$command" = "volDown" -a "$mode" = "IN" ]; then vol=$(osascript -e 'tell application "Spotify" to sound volume') vol=$(($vol-10)) if [ $vol -lt 0 ]; then vol=0; fi osascript -e "tell application \"Spotify\" to set sound volume to $vol" elif [ "$command" = "learn" -a "$mode" = "IN" ]; then state="learn" elif [ "$command" = "learn" -a "$mode" = "OUT" ]; then state="none" elif [ "$command" = "playpause" -a "$mode" = "IN" ]; then osascript -e 'tell application "Spotify" to playpause' elif [ "$command" = "" -a "$mode" = "IN" -a "$state" = "learn" ]; then surl=$(osascript -e 'tell application "Spotify" to id of current track') artist=$(osascript -e 'tell application "Spotify" to artist of current track') album=$(osascript -e 'tell application "Spotify" to album of current track') name=$(osascript -e 'tell application "Spotify" to name of current track') grep -v $tag $tagdb 2>/dev/null > $tmp1 cat $tmp1 > $tagdb pstate=$(osascript -e 'tell application "Spotify" to player state') pos=0 if [ "$pstate" = "paused" ]; then pos=$(osascript -e 'tell application "Spotify" to player position') fi echo "$tag $surl $pos ;$name;$album;$artist;" >> $tagdb echo "${surl}" cat $tagdb elif [ "$command" = "" -a "$mode" = "IN" -a "$state" = "none" ]; then surl=$(grep $tag $tagdb 2>/dev/null | awk '{ print $2 }' ) pos=$(grep $tag $tagdb 2>/dev/null | awk '{ print $3 }' ) grep $tag $tagdb 2>/dev/null if [ "$surl" != "" ]; then osascript -e "tell application \"Spotify\" to play track \"${surl}\"" fi if [ "$pos" != "" ]; then osascript -e "tell application \"Spotify\" to set player position to \"${pos}\"" fi fi #echo "tag $tag mode $mode command $command -> state $state" #osascript -e 'tell application "Spotify" to player state' done echo "wait $sec seconds" sleep $sec done } #----------- # # Mir:ror node.js loop # #-------------------------- cat > $tmp4 << EOF try { var HID = require('HID'); var devices = new HID.devices(7592, 4865); var hid; if (!devices.length) { console.log("No mir:ror found"); } else { hid = new HID.HID(devices[0].path); hid.write([03, 01]); //Disable sounds and lights hid.read(onRead); } } catch (err) { console.log("Error:", err) console.log("Again") } function onRead(error, data) { var size; var id; //get 64 bytes if (data[0] != 0) { //console.log("\n" + data.map(function (v) {return ('00' + v.toString(16)).slice(-2)}).join(',')); switch (data[0]) { case 1: //Orientation change switch (data[1]) { case 4: console.log("-> mir:ror up"); break; case 5: console.log("-> mir:ror down"); break; } break; case 2: //RFID size = data[4]; switch (data[1]) { case 1: id = (data.splice(0)).splice(2, 14); console.log("Tag identified " + id.map(function (v) {return ('00' + v.toString(16)).slice(-2)}).join('') + " IN"); break; case 2: id = (data.splice(0)).splice(2, 14); console.log("Tag identified " + id.map(function (v) {return ('00' + v.toString(16)).slice(-2)}).join('') + " OUT"); break; } break; } } hid.read(onRead); } EOF main $* rm -f $tmps exit # read-only osascript -e 'tell application "Spotify" to player state' # stopped,playing,paused osascript -e 'tell application "Spotify" to current track' # The current playing track. osascript -e 'tell application "Spotify" to artwork of current track' # Image data in TIFF format. osascript -e 'tell application "Spotify" to artist of current track' # The artist of the track. osascript -e 'tell application "Spotify" to album of current track' # The album of the track. osascript -e 'tell application "Spotify" to disc number of current track' # The disc number of the track. osascript -e 'tell application "Spotify" to duration of current track' # The length of the track in seconds. osascript -e 'tell application "Spotify" to played count of current track' # The number of times this track has been played. osascript -e 'tell application "Spotify" to track number of current track' # The index of the track in its album. osascript -e 'tell application "Spotify" to starred of current track' # Is the track starred? osascript -e 'tell application "Spotify" to popularity of current track' # How popular is this track? 0-100 osascript -e 'tell application "Spotify" to id of current track' # The ID of the item. osascript -e 'tell application "Spotify" to name of current track' # The name of the track. osascript -e 'tell application "Spotify" to artwork of current track' # The track s album cover. osascript -e 'tell application "Spotify" to album artist of current track' # That album artist of the track. osascript -e 'tell application "Spotify" to spotify url of current track' # The URL of the track. # read/write osascript -e 'tell application "Spotify" to player position' # The player s position within the currently playing track in seconds. osascript -e 'tell application "Spotify" to set player position to 20' osascript -e 'tell application "Spotify" to repeating enabled' # Is repeating enabled in the current playback context? osascript -e 'tell application "Spotify" to set repeating enabled to true' osascript -e 'tell application "Spotify" to repeating' # Is repeating on or off? osascript -e 'tell application "Spotify" to set repeating to true' osascript -e 'tell application "Spotify" to shuffling enabled' # Is shuffling enabled in the current playback context? osascript -e 'tell application "Spotify" to shuffling' # Is shuffling on or off? osascript -e 'tell application "Spotify" to sound volume' # The sound output volume (0 = minimum, 100 = maximum) osascript -e 'tell application "Spotify" to set sound volume to 50' # commands osascript -e 'tell application "Spotify" to next track' osascript -e 'tell application "Spotify" to previous track' osascript -e 'tell application "Spotify" to playpause' osascript -e 'tell application "Spotify" to pause' osascript -e 'tell application "Spotify" to play' osascript -e 'tell application "Spotify" to play track "spotify:track:3941McqnrX9blUEelPxgot"' osascript -e 'tell application "Spotify" to play track "spotify:user:ni_co:playlist:7gsVkVqVPzw2pBDkJVHYYv"' # read-only osascript -e 'tell application "Spotify" to name' # The name of the application. osascript -e 'tell application "Spotify" to frontmost' # Is this the frontmost (active) application? osascript -e 'tell application "Spotify" to version' # The version of the application. osascript -e 'tell application "Spotify" to quit'