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

查看:142
本文介绍了我需要在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

推荐答案

尝试以下操作:

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确保该字符串未经修改地传递(稍后由open进行URI编码).

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天全站免登陆