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

在Apple Music中打开/搜索Spotify曲目(使用Applescript),applescript,spotify,apple-music,Applescript,Spotify,Apple Music,我想用一种简单的方法在苹果音乐中从Spotify版本切换到同一版本 我已经找到了在Apple Music web player中使用Applescript搜索当前正在播放的Spotify曲目的方法: tell application "Spotify" if player state is not stopped then set currentArtist to artist of current track as string se

我想用一种简单的方法在苹果音乐中从Spotify版本切换到同一版本

我已经找到了在Apple Music web player中使用Applescript搜索当前正在播放的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
我很乐意:

  • 在native Music.app(而不是web播放器)中打开搜索。这是否得到支持
  • 理想情况下,不要进行搜索,而是直接进入同一版本。也许和你一起
  • 选择任何选定的Spotify曲目,而不仅仅是当前播放的曲目。看看Spotify Applescript字典,我知道这是不可能的

    • 现在也有类似的问题,很快就解决了。在我的例子中,我只想在音乐应用程序上触发搜索

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

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

      以下是应用脚本:

      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中将其保存为“查找音乐”,现在我可以选择文本,右键单击>服务>查找音乐,打开音乐并显示所选文本的结果。我希望您可以使用其中的一些部分。

      我刚刚在daemon的答案的帮助下,想出了如何将文本从任何地方传递到音乐中的搜索字段,而daemon的答案已经不起作用了。这应该适用于你想要做的事情和你拥有的事情

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

      首先,它检查主音乐窗口相对于迷你播放器是否打开,如果没有打开,则打开它以通过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来检查该部分,但假设您的代码是正确的,这应该可以工作):


      我唯一搞不清楚的是如何检查搜索字段是否已经处于焦点位置,因为如果已经处于焦点位置,cmd-F会发出系统警报声。通常情况下,这不是一个问题,因为在再次运行此脚本之前,您通常会搜索其他内容并与之交互,所以称之为“好”。)

      谢谢。试过你的脚本后,出于某种原因,我遇到了一个“错误号-1719”——我对AppleScript知之甚少,无法理解到底出了什么问题。然而,我最近尝试了,我通过运行AppleScript将艺术家和曲目复制到剪贴板,然后使用Keysmith逻辑将剪贴板粘贴到搜索字段中,使它非常容易工作。非常好。你有最新的音乐和macOS版本吗?也许现场秩序改变了,弄坏了什么。但很高兴您找到了另一个解决方案:)这不再有效,但它帮助我找到了一个可行的解决方案(请参阅其他解决方案)
      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