Created
January 18, 2017 12:23
-
-
Save hiway/76b095ff7b47ff76dc23442e6feba7c8 to your computer and use it in GitHub Desktop.
Quick n' dirty HTTP API to play/pause and cue a playlist on iTunes in MacOS.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
from flask import Flask, request | |
app = Flask(__name__) | |
def osascript_multiline_compress(script): | |
script = ["-e '{line}'".format(line=line) for line in script.split('\n')] | |
return 'osascript ' + ' '.join(script) | |
def osascript(script): | |
os.system(osascript_multiline_compress(script)) | |
@app.route('/play') | |
def itunes_play(): | |
osascript("""tell app "iTunes" to play""") | |
return 'ok' | |
@app.route('/pause') | |
def itunes_pause(): | |
osascript("""tell app "iTunes" to pause""") | |
return 'ok' | |
@app.route('/playlist') | |
def itunes_playlist(): | |
playlist = request.args.get('name', 'My Top Rated') | |
command = """tell app "iTunes" to play playlist "{}" """.format(playlist) | |
print(command) | |
osascript(command) | |
return 'ok' | |
if __name__ == '__main__': | |
app.run('0.0.0.0', 5000) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment