Skip to content

Instantly share code, notes, and snippets.

@Mumakil
Last active June 19, 2021 05:06
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Mumakil/0ada5d24fd07dbe3966d0c2222702574 to your computer and use it in GitHub Desktop.
Save Mumakil/0ada5d24fd07dbe3966d0c2222702574 to your computer and use it in GitHub Desktop.
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