Introduction: RFID Controls for Spotify on OSX Using Hacked Mir:ror.

Crappy weather in Paris... So this weekend is RFID weekend !

My Spotify account is used by my kids on a regular basis now and not all of them are old enough to read. A few lines of script later, and Spotify is accessible to all of them (which seems to already cause healthy fights, but let's stay focused...).

I used Mir:ror in a previous Instructable: it is a RFID reader that used to need a web service from a now dead company (Violet).

Said RFID reader can be accessed locally by simple scripts (including osascript).


What you will need for this project:

- a Mir:ror from violet (about 10 / 15 euros on ebay / in a garage sale these days)
- compatible RFID cards (any ISO14443A card, ebay is your friend)
- a Mac running OSX (mine is running 10.6.8). [minimum Intel-mac, running 10.5]

Step 1: OSX

[note: I used to use node.js here but due to incompatibility with 10.5.8, I ended up modifying a hidapi example instead.]


Hidtest is a program that opens the device and reads from it. It is a modified example of the hidapi package. Details in step 4.


Copy the attached hidtest and spotify_osx.sh files into your home directory.
Then, in a terminal:

bash-3.2$ chmod 777 spotify_osx.sh
bash-3.2$ chmod 777 hidtest

start the script in a terminal

bash-3.2$ ./spotify_osx.sh

Step 2: Configuration / Usage

1. Configuration

Basic commands (learn, lock, playpause, volUp, volDown) are stored in the script itself. You need to update tag ids with your own.

## RFID tags
cat > $tmp2 << EOF
00000462e6580c00000000000000 learn
000004a2e55b0c00000000000000 lock
000004f27a5f0c00000000000000 playpause
00000432f7620c00000000000000 volUp
000004e27d610c00000000000000 volDown
EOF



2. Usage

- lock/unlock the device using the lock card.

- playpause, volUp and volDown just do what they say.

- learn: with this card on the reader, the script will pair the next card with the current song. If the current song is paused, then the current location is also stored.


Voila...

Step 3: Spotify Osascript Commands

Here is a recap of the commands supported by Spotify.

I have built this reference using the following file:

/Applications/Spotify.app/Contents/Resources/Spotify.sdef

# 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'

Step 4: Build Hidtest

This steps is not necessary !

If you are curious, tech-minded or if you want to adapt this procedure to your particular needs, you might need to build hidtest yourself. When trying to use/compile node.js or hidapi on mac OSX 10.5.8, I have got my share of :

- "ld: library not found for -lcrt1.10.6.o"
- "dyld: unknown required load command 0x80000022"
-...

Here is what I had to do to get hidtest compiled for 10.5.8 (mac OSX 10.6 is just a breeze):

git clone git://github.com/signal11/hidapi.git
# replace hidapi/hidtest/hidtest.cpp by attached hidtest.cpp
cd hidapi/mac
export MACOSX_DEPLOYMENT_TARGET=10.5
make

Voila !