且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Applescript从iTunes URL播放音乐

更新时间:2023-12-04 13:14:16

免责声明:我没有Apple Music订阅,因此我端的UI可能与您的UI并不完全相同.但是,如果我单击播放"按钮,则会收到一个小的广告,要求我注册该服务,如果您拥有该服务,我认为它只会播放音乐.因此,以下是我可以按照以下步骤操作以弹出该框的步骤:

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:

AppleScript的第一个也是最方便的方法是尝试按空格键开始播放音乐.如果我通过单击手动选择了该项目,则实际上效果很好.但是,在open location之后,它不起作用,这似乎是因为即使在查看器中突出显示了该行,实际的键盘焦点似乎仍在页面本身上(iTunes Store和Apple Music似乎具有其整个用户界面以WebKit呈现的网页形式呈现).您可以通过点击键盘上的向上和向下箭头键进行验证.页面会上下滚动,而无需切换到相邻轨道.

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.

我认为这实际上是iTunes中的错误;我认为,解决该问题的真正方法是通过错误报告程序将此问题报告给Apple.实际上,使用open location应该将键盘焦点设置为导航到的轨道.

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.

话虽如此,我们可以在短期内通过模拟点击播放"按钮来解决该问题.请注意,您可能需要在系统偏好设置>安全和隐私>可访问性中添加您的应用.还要注意,这是非常脆弱的,如果Apple改变了他们所服务的网页布局中的任何内容,那么整个事情都会崩溃.最后,请注意,此代码非常丑陋.整个过程只看一眼就给了我荨麻疹,但这是我唯一能够上班的事情.阅读此代码的副作用可能包括恶心,头痛和自杀念头.吃完后不要立即阅读此代码.如果您有抑郁症或强迫症病史,请在阅读此代码之前咨询您的医生.

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

当然,如果Apple修复了该错误,我们应该能够:

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