Python to replace AppleScript targeting Apple Music

Posted on
Wed Nov 22, 2023 5:11 pm
jltnol offline
Posts: 994
Joined: Oct 15, 2013

Re: Python to replace AppleScript targeting Apple Music

So like you, Music does play an important part in my household as well. I lament the demise of the iTunes/Music plugin, but don't look back, don't look down. Not to disagree with your assessment of the Airfoil Plugin, but I've been using it to populate an Indigo Page with Song Title, Artist, and Album. There is some basic Previous / Next Track as well as Play/Pause as well.

One of my biggest issues with the iTunes plugin was it didn't seem to know if iTunes was actually playing or not. I had a few Action Groups for when I left the house to stop iTunes, but of course, just like a single button both opens and closes the garage door, a single button both starts and stops iTunes . If it was actually playing, it would stop.... but if it were stopped, it would play. Not what I needed/wanted to happen. I think I ended up Force Quitting the iTunes/Music app in the Action Group(which was on a remote computer as well), but it was always a pain that the plugin just didn't know.

The iTunes Local Control seems to be working just fine, but unfortunately for me, my Music Mac is NOT my Indigo Mac, so that plugin isn't as useful for me as it might be. (but as I'm writing this, just thought maybe "sharing" my libraries might be an option)

I've also become so disenchanted with the Music App in general, as Apple's relentless push to subscribe to everything the Music app is a pale comparison to what iTunes was back in the day. There is another music app that I play with occasionally called Swinsian, which is much like iTunes was in its prime before Apple made it almost impossible to use. But writing a plug-in for the app is way beyond my pay grade.....

Posted on
Sat Dec 02, 2023 12:32 am
Korey offline
User avatar
Posts: 813
Joined: Jun 04, 2008
Location: Henderson, NV

Re: Python to replace AppleScript targeting Apple Music

FYI: This python code updates the Variable "AppleMusicIsPlaying" to either playing or paused.

I'm quite stoked that it works as well as it does, it's a scheduled action group repeating every 5 seconds along with my Spotify action group that does the same for Spotify.

How it works in my situation: if Music starts playing , Spotify stops and Airfoil changes input source to Music, and vice versa for Spotify.

As you can see from my Airfoil devices, this is one of the main features of Indigo for our home!

:D :D :D

Code: Select all
import applescript

musicScript = '''
tell application "Music"
  set strState to (get player state) as string
end tell
set blnPlaying to (strState is equal to "playing" or strState is equal to "paused")
set returnRec to {AppleMusicPlayerState:strState}
set returnRec to returnRec & {AppleMusicIsPlaying:blnPlaying as string}

return returnRec
'''

a_script = applescript.AppleScript(source=musicScript)
reply = a_script.run()

for var_name, value in reply.items():
    indigo.variable.updateValue(str(var_name), value=value)
Attachments
Untitled.png
Untitled.png (201.96 KiB) Viewed 596 times

--
Korey

Who is online

Users browsing this forum: No registered users and 5 guests