Applescript:在 Google 中搜索 iTunes 曲目歌词

发布于 2025-01-01 16:08:58 字数 418 浏览 3 评论 0原文

我想要一个脚本来在 Google 中搜索当前播放歌曲的歌词。为什么以下不起作用?

tell application "iTunes"
    set trackArtist to artist of current track
    set trackName to name of current track
end tell
set search to trackArtist & " - " & trackName & " lyrics"
open location "https://www.google.com/search?q=" & search

如果我“返回搜索”,我可以看到变量设置正确。如果我将最后一行中的“搜索”替换为“测试歌词”,浏览器将按预期打开。但上面的脚本不执行任何操作,也不返回任何错误。

I want a script to search Google for the lyrics of the currently playing song. Why doesn't the following work?

tell application "iTunes"
    set trackArtist to artist of current track
    set trackName to name of current track
end tell
set search to trackArtist & " - " & trackName & " lyrics"
open location "https://www.google.com/search?q=" & search

If I "return search", I can see the variable is set correctly. And if I replace "search" in the last line with "test lyrics" the browser opens as expected. But the script above performs no action whatsoever, nor does it return any errors.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

°如果伤别离去 2025-01-08 16:08:58

我认为您忘记了大多数浏览器都会对地址字段中的 URL 进行解码,并且在请求之前它们会再次对 URL 进行编码。所以你需要做的就是对 url 进行编码。

tell application "iTunes"
    set trackArtist to artist of current track
    set trackName to name of current track
end tell
open location "http://www.google.com/search?q=" & rawurlencode(trackArtist & " - " & trackName & " lyrics")

on rawurlencode(theURL)
    set PHPScript to "<?php echo rawurlencode('%s');?>"
    set theURL to do shell script "echo " & quoted form of theURL & " | sed s/\\'/\\\\\\\\\\'/g"
    return do shell script "printf " & quoted form of PHPScript & space & quoted form of theURL & " | php"
end rawurlencode

I think you forgot that most browsers decode the URL in the address field and before requested they encode the URL again before. So what you need to do is encode the url too.

tell application "iTunes"
    set trackArtist to artist of current track
    set trackName to name of current track
end tell
open location "http://www.google.com/search?q=" & rawurlencode(trackArtist & " - " & trackName & " lyrics")

on rawurlencode(theURL)
    set PHPScript to "<?php echo rawurlencode('%s');?>"
    set theURL to do shell script "echo " & quoted form of theURL & " | sed s/\\'/\\\\\\\\\\'/g"
    return do shell script "printf " & quoted form of PHPScript & space & quoted form of theURL & " | php"
end rawurlencode
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文