Applescript 从 iTunes URL 播放音乐 [英] Applescript play music from iTunes URL

查看:26
本文介绍了Applescript 从 iTunes URL 播放音乐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下脚本将在 iTunes 中打开一首曲目

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

现在,要求iTunes"播放它不起作用,因为该曲目突出显示但未正确选择,即需要手动单击鼠标才能选择并播放.

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.

如何选择突出显示的曲目?或者我怎么能要求iTunes"播放这首歌?!或者,有没有办法直接从 URL 添加音乐到我的音乐库?

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?

推荐答案

免责声明:我没有订阅 Apple Music,所以我的用户界面可能与您的不完全相同.但是,如果我单击播放"按钮,我会收到一个小广告,要求我注册该服务,我认为如果您有该服务,它只会播放音乐.所以,这些是我能够遵循的步骤来弹出该框:

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 似乎将其整个 UI 呈现为 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

这篇关于Applescript 从 iTunes URL 播放音乐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆