【问题标题】:Is it possible to access the song name of the currently playing song in the Spotify Mac app via Applescript?是否可以通过 Applescript 访问 Spotify Mac 应用程序中当前播放歌曲的歌曲名称?
【发布时间】:2019-09-12 20:41:17
【问题描述】:

是否可以通过 Applescript 访问 Spotify Mac 应用中当前播放歌曲的歌曲名称?我希望能够将当前播放歌曲的名称复制到我的剪贴板。

如果不能通过 Applescript 实现,我还能如何做到这一点?

【问题讨论】:

标签: macos applescript spotify spotify-desktop


【解决方案1】:
tell application "Spotify"
    set nowplaying to the current track's name
end tell

set the clipboard to nowplaying

尽管set the clipboard 是一个标准添加命令,但通常应该能够将以上内容组合成一行:

tell app "Spotify" to set the clipboard to the current track's name

但这似乎不适用于我的系统。但是,将剪贴板命令放在 Spotify tell 块之外可以正常工作。

【讨论】:

  • 第二个例子不起作用,因为“当前曲目”是 Spotify 应用程序定义的属性,所以它在 **tell application Spotify** 块之外没有任何意义。
  • @RonReuter 我已经编辑了答案以更清楚地说明我的观点。
  • @CJK,我没有 Spotify,但很想知道这是否有效:tell application "Spotify" to set the clipboard to (get name of current track) 即使可以,有时我也会尝试将标准添加中的一些内容保留在另一个应用程序的 tell 之外阻止。通常是在脚本编辑器的事件窗格中运行时引发非致命错误的那些。就像我前几天向 wch1zpink 提到的那样,它在应用程序的tell block 中运行时出现了两个非致命错误。在播放曲目时,在此 command 中替换“iTunes”对我来说没有错误。
  • @user3439894 刚刚测试过,你是对的,使用get 确实有效。我同意您的观点,即尽可能将应用程序和脚本添加命令分开,因为这样做似乎是一种好习惯。但是你关于非致命错误的观点是进一步的理由,因为抛出的每个错误都需要时间让 AppleScript 在继续执行之前恢复,并且至少在理论上会降低脚本的效率。
  • 这两种变体都有效:tell application "Spotify" to set the clipboard to (get name of the current track) 如@CJK 和@user3439894 所说,以及tell application "Spotify" to set the clipboard to (name of the current track as string)。发生的事情是 name of the current track 被评估为 AppleScript reference,而不是字符串。解释器需要额外的语法来告诉它解析引用并产生一个字符串。在第一个示例中,get 导致对引用进行评估,在第二个示例中,as string 导致评估。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-04-07
  • 2013-12-29
  • 1970-01-01
  • 1970-01-01
  • 2019-03-23
  • 1970-01-01
  • 2013-04-01
相关资源
最近更新 更多