BeoGateway/Resources/ASBridge.py

125 lines
4.6 KiB
Python
Raw Normal View History

2021-12-08 13:03:50 +00:00
#!/usr/bin/python
import os
2021-12-17 18:45:05 +00:00
import unicodedata
2021-12-08 13:03:50 +00:00
from Foundation import NSAppleScript
from ScriptingBridge import SBApplication
''' Module defining a MusicController class for Apple Music, enables:
basic transport control of the player,
query of player status,
2021-12-17 18:45:05 +00:00
playing existing playlists,
running external appleScripts for more complex control, and
2021-12-08 13:03:50 +00:00
reporting messages in the notification centre'''
PLAYSTATE = dict([
(1800426320, "Play"),
(1800426352, "Pause"),
(1800426323, "Stop"),
(1800426310, "Wind"),
(1800426322, "Rewind")
])
class MusicController(object):
def __init__(self):
self.app = SBApplication.applicationWithBundleIdentifier_("com.apple.Music")
# Player information
def get_current_track_info(self):
2021-12-17 18:45:05 +00:00
name = self.app.currentTrack().name()
album = self.app.currentTrack().album()
artist = self.app.currentTrack().artist()
if name:
# Deal with tracks with non-ascii characters such as accents
name = unicodedata.normalize('NFD', name).encode('ascii', 'ignore')
album = unicodedata.normalize('NFD', album).encode('ascii', 'ignore')
artist = unicodedata.normalize('NFD', artist).encode('ascii', 'ignore')
2021-12-08 13:03:50 +00:00
return [name, album, artist]
def get_current_play_state(self):
return PLAYSTATE.get(self.app.playerState())
def get_current_track_position(self):
return self.app.playerPosition()
# ########################################################################################
# Transport Controls
def playpause(self):
self.app.playpause()
def play(self):
if PLAYSTATE.get(self.app.playerState()) in ['Wind', 'Rewind']:
self.app.resume()
elif PLAYSTATE.get(self.app.playerState()) == 'Pause':
self.app.playpause()
elif PLAYSTATE.get(self.app.playerState()) == 'Stop':
2021-12-17 18:45:05 +00:00
self. app.setValue_forKey_('true', 'shuffleEnabled')
2021-12-08 13:03:50 +00:00
playlist = self.app.sources().objectWithName_("Library")
playlist.playOnce_(None)
def pause(self):
if PLAYSTATE.get(self.app.playerState()) == 'Play':
self.app.pause()
def stop(self):
if PLAYSTATE.get(self.app.playerState()) != 'Stop':
self.app.stop()
def next_track(self):
self.app.nextTrack()
def previous_track(self):
self.app.previousTrack()
def wind(self, time):
# Native wind function can be a bit annoying
# I provide an alternative below that skips a set number of seconds forwards
# self.app.wind()
self.set_current_track_position(time)
def rewind(self, time):
# Native rewind function can be a bit annoying
# I provide an alternative below that skips a set number of seconds back
# self.app.rewind()
self.set_current_track_position(time)
# ########################################################################################
# More complex playback control functions
2021-12-17 18:45:05 +00:00
def shuffle(self):
if self.app.shuffleEnabled():
self.app.setValue_forKey_('false', 'shuffleEnabled')
else:
self.app.setValue_forKey_('true', 'shuffleEnabled')
2021-12-08 13:03:50 +00:00
def set_current_track_position(self, time, mode='Relative'):
if mode == 'Relative':
# Set playback position in seconds relative to current position
self.app.setPlayerPosition_(self.app.playerPosition() + time)
elif mode == 'Absolute':
# Set playback position in seconds from the start of the track
self.app.setPlayerPosition_(time)
def play_playlist(self, playlist):
self.app.stop()
playlist = self.app.sources().objectWithName_("Library").playlists().objectWithName_(playlist)
playlist.playOnce_(None)
# ########################################################################################
# Accessory functions
@staticmethod
def run_script(script):
# Run an external applescript file
script = 'run script ("' + script + '" as POSIX file)'
s = NSAppleScript.alloc().initWithSource_(script)
s.executeAndReturnError_(None)
@staticmethod
2021-12-17 18:45:05 +00:00
def notify(message, subtitle):
2021-12-08 13:03:50 +00:00
# Post message in notification center
path = os.path.dirname(os.path.abspath(__file__))
script = 'tell application "' + path + '/Notify.app" to notify("BeoGateway", "' + \
2021-12-17 18:45:05 +00:00
message + '", "' + subtitle + '")'
2021-12-08 13:03:50 +00:00
s = NSAppleScript.alloc().initWithSource_(script)
s.executeAndReturnError_(None)