0

I am trying to make a script that will do a google search for you, but if i do a multiple word search, it doesn't work. Anyone know how to fix this?

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
Charles
  • 50,943
  • 13
  • 104
  • 142
Tyler Mullins
  • 77
  • 3
  • 10

3 Answers3

0

You just need to account for the space and convert it/them to "%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

The encoding code is from here

markhunte
  • 6,805
  • 2
  • 25
  • 44
  • When I run it, it says it expected the "on" in "on encode and on tid" to be else – Tyler Mullins Apr 08 '13 at 18:08
  • Hi, That does not really make sense. Can you post the full error. The script works fine here. No errors. Did you change the script I posted. If so how? – markhunte Apr 08 '13 at 20:42
  • Nevermind. All I had to do was specify a browser. Thanks! `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` – Tyler Mullins Apr 08 '13 at 22:15
0

You also have to escape special characters like % and &. And open location doesn't work with URLs that have some non-ASCII characters:

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

You can url encode using a shell though:

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

Community
  • 1
  • 1
Lri
  • 26,768
  • 8
  • 84
  • 82
0

You have two dialogs opening when you can have only one :

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

I don't see what you mean with you multiple search no working. For me it works just fine if you enter in the dialog box multiple words separed by spaces.

Zitoun
  • 446
  • 3
  • 13