如何在applescript的url地址中放置变量
我试图将用户搜索词放入url,但它一直认为我拥有的变量就是我想要搜索的东西如何在applescript的url地址中放置变量,applescript,Applescript,我试图将用户搜索词放入url,但它一直认为我拥有的变量就是我想要搜索的东西 set v to text returned of result tell application "Google Chrome" to open location"https://www.google.co.in/search?q=v" 如何确保它知道v是一个变量而不是url的一部分?您必须使用和运算符连接字符串: "https://www.google.co.in/search?q=" & v 请注意,
set v to text returned of result
tell application "Google Chrome" to open
location"https://www.google.co.in/search?q=v"
如何确保它知道v是一个变量而不是url的一部分?您必须使用
和运算符连接字符串:
"https://www.google.co.in/search?q=" & v
请注意,如果v
包含空格字符,则必须添加转义百分比。不应使用打开位置
,该位置实际上会在系统的默认浏览器中打开URL,而不管您将命令发送到哪个应用程序。如果你的默认浏览器是Chrome,这没关系,但在这种情况下,你可以省略告诉应用程序“GoogleChrome”到
更稳健的方法是:
set v to the text returned of the result
tell application "Google Chrome"
activate
if the number of windows is 0 then make new window
tell the front window to make new tab ¬
with properties ¬
{URL:"https://www.google.co.in/search?q=" & v}
end tell
谢天谢地,使用macOS“10.13.4”上的AppleScript版本“2.7”和Chrome“67”(可能还有其中任何一个版本的早期版本),您不需要对字符串(v
)进行百分比编码以适应自动完成的空格(但不适用于其他特殊字符)