我回来写Applescripts了。我正在为你制作一个谷歌的脚本。基本上,我需要用%20替换所有空格。我对文本项分隔符有一点了解,但在这种情况下我不知道如何实现它们。
这是我到目前为止所得到的:
if userInput contains "Google " then set {TID, text item delimiters} to {text item delimiters, {"Google "}}
if length of userInput is greater than or equal to 2 then set resultString to text item 2 of userInput
if userInput contains "Google " then set text item delimiters to TID
set openPage to (resultString as string)
if userInput contains "Google " then do shell script "open http://www.google.com/search?q=" & openPage
仅供参考,userInput变量是我在处理文本框时使用的变量。
由于
答案 0 :(得分:0)
我认为这个剧本演示了你要做的事情的原则:
tell application "Safari"
activate
display dialog "Search Google for:" default answer "" buttons {"Cancel", "OK"} default button "OK" with title (the name as text) with icon note giving up after 60
if the button returned of the result is equal to "OK" then
set theSearch to the text returned of the result
set AppleScript's text item delimiters to space
set theSearchList to every text item of theSearch
set AppleScript's text item delimiters to "+"
set theQuery to theSearchList as text
set AppleScript's text item delimiters to ""
set theLink to "http://www.google.com/search?hl=en&q=" & theQuery & "&btnG=Google+Search"
open location theLink
end if
end tell
但是,如果你只是问一个类似BBEdit的应用程序(或其免费版本,TextWrangler,它有相同的AppleScript字典)来为你进行查找和替换会更容易,因为BBEdit是查找和替换的专家文本,而AppleScript本身不是。 AppleScript是一种“小语言” - 它故意缺少大多数编程语言的95%的功能,因为该功能在您使用AppleScript命令的应用程序中。换句话说,AppleScript希望您有一个用于处理文本的文本编辑器,因此AppleScript故意不具备Perl等语言的内置文本处理功能(您也可以在AppleScripts中使用它)。 p>
因此,如果您将BBEdit(或免费的TextWrangler)添加到混合中,上面的脚本会变得更短,更容易编写,更容易阅读和更容易理解:
tell application "Safari"
activate
display dialog "Search Google for:" default answer "" buttons {"Cancel", "OK"} default button "OK" with title (the name as text) with icon note giving up after 60
if the button returned of the result is equal to "OK" then
set theSearch to the text returned of the result
tell application "BBEdit"
set theQuery to replace " " using "+" searchingString theSearch
end tell
set theLink to "http://www.google.com/search?hl=en&q=" & theQuery & "&btnG=Google+Search"
open location theLink
end if
end tell
另外,请注意,您不必运行shell脚本即可在浏览器中打开链接。所以你的脚本中的这一行:
if userInput contains "Google " then do shell script "open http://www.google.com/search?q=" & openPage
...可以这样写:
if userInput contains "Google " then open location "http://www.google.com/search?q=" & openPage
......或者像这样:
if userInput contains "Google " then
open location "http://www.google.com/search?q=" & openPage
end if
...您可以在任何应用中使用“开放位置”命令,而不仅仅是Safari。如果您告诉Finder“打开位置”,该链接将在您的默认浏览器中打开,该浏览器可能是Chrome。