微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在Apple Music中打开/搜索Spotify曲目使用Applescript

如何解决在Apple Music中打开/搜索Spotify曲目使用Applescript

我想要一种从Spotify版本切换到Apple Music中相同版本的简单方法

我已经找到了一种使用Applescript在Apple Music网络播放器中搜索当前播放的Spotify曲目的方法

tell application "Spotify"
    if player state is not stopped then
        set currentArtist to artist of current track as string
        set currentTrack to name of current track as string
        open location "https://music.apple.com/search?term=" & currentArtist & " " & currentTrack
    end if
end tell

我想:

  • 在本机Music.app中(而不是在网络播放器中)打开搜索支持吗?
  • 理想情况下不进行搜索,而是直接转到相同版本。也许和ISRC codes在一起?
  • 选择任何选定的Spotify曲目,而不仅仅是当前正在播放的曲目。看一下Spotify Applescript字典告诉我这不可能。

解决方法

现在也有类似的问题,并很快将其破解。就我而言,我只是想在“音乐”应用上触发搜索。

打开Automator并创建一个新的“服务”。

工作流在“每个应用程序”中接收当前的“文本”>。

这是AppleScript:

on run {input,parameters}
    
    tell application "Music"
        activate
    end tell

    tell application "System Events"
        tell process "Music"
            set window_name to name of front window
            set value of text field 1 of UI element 1 of row 1 of outline 1 of splitter group 1 of window window_name to input
            keystroke ""
            key code 36
        end tell
    end tell
    
    return input
end run

我在Automator中将其保存为“在音乐上查找”,现在我可以选择文本,右键单击>“服务”>“在音乐上查找”,然后打开音乐图并向我显示所选文本的结果。我希望您可以使用其中的某些部分。

,

在守护进程的回答的帮助下,我刚刚想出了如何将文本从任何地方传递到音乐中的搜索字段,但不再有效。这应该适用于您想要做的事情以及您拥有的事情。

用连接字符串的变量名替换“open location”行。在您的代码下方添加此代码并传递该变量代替“输入”(在我的情况下,“输入”是来自任何应用程序的文本,我用它来选择要发送的电子邮件/网页/消息中的艺术家姓名文本到音乐搜索)。

首先它检查主音乐窗口是否与 MiniPlayer 打开,如果没有打开它,通过 cmd-O 启用搜索,cmd-F 查找,然后传递输入并点击返回:

tell application "Music"
    activate
end tell

tell application "System Events"
    if not (exists (window "Music" of process "Music")) then
        tell process "Music"
            keystroke "0" using command down
        end tell
    end if
    tell process "Music"
        keystroke "f" using command down
        keystroke input
        key code 36
    end tell
end tell

所以,像这样(我没有 Spotify 来检查该部分,但假设您的代码正确,这应该可以工作):

tell application "Spotify"
    if player state is not stopped then
        set currentArtist to artist of current track as string
        set currentTrack to name of current track as string
        set spotTrack to currentArtist & " " & currentTrack
    end if
end tell

tell application "Music"
    activate
end tell

tell application "System Events"
    if not (exists (window "Music" of process "Music")) then
        tell process "Music"
            keystroke "0" using command down
        end tell
    end if
    tell process "Music"
        keystroke "f" using command down
        keystroke spotTrack
        key code 36
    end tell
end tell

我唯一想不通的是如何检查搜索字段是否已经在焦点上,因为如果是,cmd-F 会引起系统警报声。通常不是问题,因为在再次运行此脚本之前,您通常会搜索其他内容并与之交互,因此称其为好。 :)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。