Applescript play music from iTunes URL

有些话、适合烂在心里 提交于 2021-01-27 11:48:58

问题


The following script will open a track in iTunes

use application "iTunes"
property trackURL : "itmss://itunes.apple.com/us/album/brahms-violin-concerto-in-d-major-op-77-iii-allegro/145533236?i=145533044&uo=4"
open location trackURL

Now, asking "iTunes" to play it does not work because the track is highlighted but not properly selected, i.e., it requires a manual mouse click to select it and play it.

How can I select the highlighted track? Or how could I ask "iTunes" to play the song?! Alternatively, is there a way to add a music to my library from an URL directly?


回答1:


Disclaimer: I don't have the Apple Music subscription, so the UI on my end may not be exactly the same as yours. However, if I click the "Play" button, I get the little advertisement asking me to sign up for the service, which I assume would just play the music if you had the service. So, these are the steps I've been able to follow to get that box to pop up:

The first, and most convenient from AppleScript, thing to try is just to hit the space bar to start the music playing. This actually works great if I've selected the item manually by clicking on it. However, after open location, it doesn't work, and this appears to be because even though the row is highlighted in the viewer, the actual keyboard focus seems to be on the page itself (the iTunes Store and Apple Music appear to have their entire UI presented as web pages rendered by WebKit). You can verify this by tapping the up and down arrow keys on the keyboard; the page scrolls up and down instead of you switching to adjacent tracks.

My opinion is that this is actually a bug in iTunes; I'd consider the true solution to the problem to be to report this to Apple via the bug reporter. Using open location really should set the keyboard focus to the track you navigated to.

With that said, we can work around it in the short term by simulating a click on the "Play" button. Note that you'll probably need to add your app in System Preferences > Security and Privacy > Accessibility. Note also that this is incredibly fragile, and if Apple ever changes anything in the layout of the web pages they're serving, this whole thing will break. Finally, please note that this code is extremely ugly; the whole thing gives me hives just by looking at it, but it's the only thing I was able to get to work. Side effects of reading this code may include nausea, headaches, and suicidal thoughts. Do not read this code immediately after eating. Consult your doctor before reading this code if you have a history of depression or obsessive-compulsive disorder.

property trackURL : "itmss://itunes.apple.com/us/album/brahms-violin-concerto-in-d-major-op-77-iii-allegro/145533236?i=145533044&uo=4"
property trackTitle : "III. Allegro giocoso, ma non troppo vivace"

tell application "iTunes"
    activate
    open location trackURL
    delay 1 -- give the page a second to load
end tell

tell application "System Events"
    tell process "iTunes"
        set theRows to the rows of table 1 of UI element 1 of scroll area 1 of group 1 of group 1 of front window

        -- "repeat with eachRow in theRows" isn't working. I don't know why. Freaking AppleScript
        repeat with i from 1 to the number of theRows
            set eachRow to item i of theRows

            if exists group 2 of UI element 2 of eachRow then
                if value of static text 1 of group 1 of group 2 of UI element 2 of eachRow is trackTitle then
                    tell group 1 of UI element 2 of eachRow to click
                end if
            end if
        end repeat
    end tell
end tell

If Apple ever fixes the bug, of course, we should be able to just:

tell application "iTunes"
    activate
    open location trackURL
    delay 1 -- give the page a second to load
end tell

tell application "System Events" to keystroke space


来源:https://stackoverflow.com/questions/49647648/applescript-play-music-from-itunes-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!