Puf*_*ses 5 applescript urlencode
我的脚本在网站上搜索歌曲,但是当有空格时它不会搜索,您必须添加下划线。我想知道是否有办法用下划线替换我的空格。你能不能用我现在的代码告诉我怎么做?
set search to text returned of (display dialog "Enter song you wish to find" default answer "" buttons {"Search", "Cancel"} default button 1)
open location "http://www.mp3juices.com/search/" & search
end
Run Code Online (Sandbox Code Playgroud)
注意:该解决方案从 Big Sur (macOS 11) 开始不再有效- 这听起来像是一个错误;如果您有更多信息,请告诉我们。
请尝试以下操作:
set search to text returned of (display dialog "Enter song you wish to find" default answer "" buttons {"Search", "Cancel"} default button 1)
do shell script "open 'http://www.mp3juices.com/search/'" & quoted form of search
end
Run Code Online (Sandbox Code Playgroud)
您需要的是URL 编码(即,对字符串进行编码以安全包含在 URL 中),这不仅仅是替换空格。该open命令行实用程序,幸运的是,执行该编码你,所以可以只是直接传递它的字符串; 您需要do shell script调用open,并quoted form of确保字符串未经修改地通过(open稍后进行 URI 编码)。
正如您将看到的,这种 URL 编码open执行用 代替空格%20,而不是下划线,但这应该仍然有效。