Retrieve variable from Applescript in Shell
因此,我正在尝试构建一个非常小的Shell脚本,以抓取当前正在播放的Spotify歌曲并在终端中返回该歌曲的歌词。
有效的方法
applescript返回/回溯到终端的曲目名称
我需要什么帮助
我似乎无法从applescript中检索
有关如何进行此工作的任何提示? :)
1 2 3 4 5 6 7 8 9 10 | echo"tell application "Spotify" set theTrack to current track set theArtist to artist of theTrack set theName to name of theTrack end tell" | osascript song=`curl -s"http://makeitpersonal.co/lyrics?artist=$theArtist&title=$theName"` echo -e"$theArtist - $theName\ $song" |
尝试:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | # Get information from Spotify via AppleScript and store it in shell variables: IFS='|' read -r theArtist theName <<<"$(osascript <<<'tell application"Spotify" set theTrack to current track set theArtist to artist of theTrack set theName to name of theTrack return theArtist &"|" & theName end tell')" # Create *encoded* versions of the artist and track name for inclusion in a URL: theArtistEnc=$(perl -MURI::Escape -ne 'print uri_escape($_)' <<<"$theArtist") theNameEnc=$(perl -MURI::Escape -ne 'print uri_escape($_)' <<<"$theName") # Retrieve lyrics via `curl`: lyrics=$(curl -s"http://makeitpersonal.co/lyrics?artist=$theArtistEnc&title=$theNameEnc") # Output combined result: echo -e"$theArtist - $theName\ $lyrics" |
-
AppleScript隐式返回最后一条语句的结果;因此,为了返回多个信息项,构建一个字符串以使用显式的
return 语句返回。 -
然后,您需要使用
read (这里选择| 作为分隔符,因为它不太可能包含在艺术家名称或歌曲名称中)将输出字符串解析为其组件,并将它们分配给shell变量(AppleScript是一个完全独立的世界,shell程序无法访问其变量-信息必须通过输出字符串传递)。 -
为了使
curl 命令起作用,必须正确地将您拼接到URL中的信息编码为URL编码(例如,Pink Floyd 必须被编码为Pink%20Floyd ),这就是perl 命令的作用。