0

我正在尝试制作一个可以为您进行谷歌搜索的脚本,但如果我进行多个单词搜索,它就不起作用。有人知道怎么修这个东西吗?

set keyword to text returned of (display dialog "Enter Search Query" default answer "")
display dialog "Enter Search Query" buttons {"Cancel", "Ok"} default button 2
if the button returned of the result is "Ok" then
    open location "http://search.yahoo.com/search?p=" & keyword
end if
4

3 回答 3

0

您还必须转义特殊字符,如 % 和 &。并且open location不适用于包含一些非 ASCII 字符的 URL:

open location "http://ja.wikipedia.org/wiki/漢字"

您可以使用 shell 进行 url 编码

do shell script "open http://ja.wikipedia.org/wiki/$(ruby -rcgi -e 'print CGI.escape(ARGV[0])' " & quoted form of "字%'\\&" & ")"

于 2013-04-08T05:50:32.393 回答
0

您只需要考虑空间并将其/它们转换为“%20”

set aSpace to "%20"
    display dialog "Enter Search Query" default answer "" buttons {"Cancel", "Ok"} default button 2
    copy the result as list to {text_returned, button_pressed}
    set t to words of text_returned

    if the button_pressed is "Ok" then

        open location "http://search.yahoo.com/search?p=" & encode(text_returned, space, aSpace)
    end if

    on encode(x, y, z)
        tid(y)
        set x to text items of x
        tid(z)
        return x as string
    end encode

    on tid(x)
        set AppleScript's text item delimiters to x
    end tid

编码代码来自这里

于 2013-04-07T16:55:14.580 回答
0

当您只能打开一个对话框时,您会打开两个对话框:

set myDialog to display dialog "Enter Search Query" default answer "" buttons {"Cancel", "Ok"} default button 2
set keyword to text returned of myDialog
if the button returned of myDialog is "Ok" then open location "http://search.yahoo.com/search?p=" & keyword

我不明白你对多次搜索不起作用的意思。对我来说,如果您在对话框中输入以空格分隔的多个单词,它就可以正常工作。

于 2013-04-19T11:44:01.147 回答