1

Using Automator, I want to do Google searches of selected texts, but the selected text should be within quotes!

I do not know how to make the script put quotes on the selected text (input), like "selected text".

on run {input, parameters}
   open location "https://www.google.com/search?q=" & input
end run

EDIT

This is the solution:

on run {input, parameters}
   open location "https://www.google.com/search?q=" & quote & input & quote
end run

3 Answers3

0

Assuming you want the double-quotes to be part of the search query (i.e. you're doing a literal string search at Google), you need to add literal double-quotes around the search string. Well, except that literal double-quotes are not allowed in URLs, so they should be URL-encoded as %22, like this:

open location "https://www.google.com/search?q=%22" & input & "%22"

Note that you might need to URL-encode the input string as well, although it seems to handle basic things like spaces automatically. See "I need to URL-encode a string in AppleScript" (the question aaplmath linked) if you need better handling than that.

Gordon Davisson
  • 118,432
  • 16
  • 123
  • 151
0

If you want Google to give you results with the exact phrase you are looking for, the search query must be within quotation marks, and for that this is how an Automator Service must look like:

on run {input, parameters}
   open location "https://www.google.com/search?q=" & quote & input & quote
end run
0

I highly recommend to use AppleScriptObjC's skills – here with help from Foundation framework – to escape the search string reliably (macOS 10.10+)

use framework "Foundation"
use scripting additions

on run {input, parameters}
    set nsInput to current application's NSString's stringWithString:("https://www.google.com/search?q=" & input)
    set characterSet to current application's NSCharacterSet's URLQueryAllowedCharacterSet()
    set escapedInput to (nsInput's stringByAddingPercentEncodingWithAllowedCharacters:characterSet) as text
    open location escapedInput
end run

It handles quotes as well as spaces or other special characters

vadian
  • 274,689
  • 30
  • 353
  • 361