我需要在 AppleScript 中对字符串进行 URL 编码 [英] I need to URL-encode a string in AppleScript

查看:34
本文介绍了我需要在 AppleScript 中对字符串进行 URL 编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的脚本在网站上搜索歌曲,但是当有空格时它不会搜索,您必须添加下划线.我想知道是否有办法用下划线替换我的空格.能否请您使用下面我当前的代码向我展示如何操作?

My script searches a website for songs, but when there are spaces it doesn't search, you have to add underscores. I was wondering if there was a way to replace my spaces with underscores. Could you please use my current code below to show me how to do it?

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

推荐答案

注意:该解决方案不再适用自 Big Sur (macOS 11) - 听起来像是一个bug;如果您有更多信息,请告诉我们.

Note: The solution no longer works as of Big Sur (macOS 11) - it sounds like a bug; do tell us if you have more information.

尝试以下操作:

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

您需要的是URL 编码(即,对字符串进行编码以安全包含在 URL 中),这不仅仅涉及替换空格.谢天谢地,open 命令行实用程序为您执行此编码,因此您可以直接将字符串传递给它;您需要 do shell script 来调用 open,并且 quoted form of 确保字符串通过未修改的(由 <代码>打开稍后).

What you need is URL encoding (i.e., encoding of a string for safe inclusion in a URL), which involves more than just replacing spaces. The open command-line utility, thankfully, performs this encoding for you, so you can just pass it the string directly; you need do shell script to invoke open, and quoted form of ensures that the string is passed through unmodified (to be URI-encoded by open later).

如您所见,open 执行的 URL 编码类型将空格替换为 %20,而不是下划线,但这应该仍然有效.

As you'll see, the kind of URL encoding open performs replaces spaces with %20, not underscores, but that should still work.

这篇关于我需要在 AppleScript 中对字符串进行 URL 编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆