Applescript:搜索谷歌的iTunes歌曲歌词

时间:2012-02-01 10:30:10

标签: search location applescript

我想要一个脚本在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

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

1 个答案:

答案 0 :(得分:1)

我认为您忘记了大多数浏览器在地址字段中解码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