Skip to content

Instantly share code, notes, and snippets.

@Mumakil Mumakil/.gitignore
Last active Oct 19, 2016

Embed
What would you like to do?
Luxafor Spotify integration
node_modules/*
STATE

Luxafor <-> Spotify integration

This set of scripts will change your Luxafor light's colors based on your Spotify player's state. It polls every 10 seconds for Spotify's play state and changes the color accordingly (playing -> red, not playing -> green).

The script will record what color it set on the last run and will not set it again if it does not change. This way you can manually override the setting and the script won't fight you before changing play states again.

Installation

  • Checkout the files (I've used ~/Code/luxafor_toggle) and npm install
  • Copy the toggle_luxafor.sh to ~/bin and make sure it's executable. Make sure it points to an existing node executable and where you copied the gist repo.
  • Open the .scpt file with AppleScript editor and make sure it points to the .sh file.
  • Make sure the .plist points to where your Apple script file is located. You can change the poll interval (default 10s) by changing the StartInterval value.
  • Link the .plist file to ~/Library/LaunchAgents/
  • Start it by running launchctl load -w ~/Library/LaunchAgents/fi.mumakil.luxafor_spotify.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"
"http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>fi.mumakil.luxafor_spotify</string>
<key>ProgramArguments</key>
<array>
<string>osascript</string>
<string>/Users/otto/Code/luxafor_toggle/spotify_state.scpt</string>
</array>
<key>StartInterval</key>
<integer>10</integer>
</dict>
</plist>
{
"name": "luxafor_toggle",
"version": "0.0.1",
"description": "Toggle luxafor from commandline",
"main": "toggle.js",
"dependencies": {
"luxafor-api": "^2.0.0"
},
"devDependencies": {
"eslint": "^3.5.0"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Otto Vehviläinen <otto@mumakil.fi>",
"license": "ISC"
}
const Luxafor = require('luxafor-api');
const fs = require('fs');
const STATEFILE = __dirname + '/STATE';
function loadCurrentState() {
try {
return fs.readFileSync(STATEFILE, {encoding: 'utf8'}).trim();
} catch (e) {
return null;
}
}
function writeCurrentState(newState) {
try {
fs.writeFileSync(STATEFILE, newState, {encoding: 'utf8'});
} catch (e) {
// probably can't do anything here?
}
}
const device = new Luxafor();
const color = (process.argv[2] || '').trim();
const currentState = loadCurrentState();
if (currentState && currentState === color) {
process.exit(0);
}
if (color === 'green') {
device.setColor('#0f0');
} else if (color === 'red') {
device.setColor('#f00');
}
writeCurrentState(color);
#!/usr/bin/env bash
/Users/otto/.nvm/versions/node/v6.6.0/bin/node ~/Code/luxafor_toggle/toggle.js "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.