Google Search scripts

The Google search methods shown HERE are hopelessly outdated and have serious flaws.

  1. They don’t work with non-English text.
  2. For text selected but not copied to the clipboard, GUI scripting is used.
  3. For some reason, the text is enclosed in quotation marks, it is not clear why.

Here, I offer users 4 better solutions.

Following 2 scripts was written by me for Google search of the text copied to the clipboard:
.


-- script: Google Search from Clipboard text (plain AppleScript)

open location "http://www.google.com/search?q=" & encode_URL(get the clipboard)

on encode_URL(origStr)
	return (do shell script "python -c 'import sys, urllib; print urllib.quote(sys.argv[1])' " & quoted form of origStr)
end encode_URL

.


-- script: Google Search from Clipboard text (AsObjC) 
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

open location "http://www.google.com/search?q=" & encodeURLencoding(get the clipboard)

on encodeURLencoding(origStr as text)
	set aStr to current application's NSString's stringWithString:origStr
	set encodedStr to aStr's stringByAddingPercentEscapesUsingEncoding:(current application's NSUTF8StringEncoding)
	return encodedStr as text
end encodeURLencoding

.

Following 2 scripts was written by me for Google search of the text selected in any application. They should be implemented as Automator service (Quick Action). When creating service, following setting should be implemented: Workflow receives current text in any application. Do not set Workflow receives current Automatic (text) in any application!!!. Then the user add Run AppleScript action with following contents. Having the service, user selects text in any app, performs right-click, then runs the service.
.


-- script: Google Search from Selected text in any application (plain AppleScript)

on run {input, parameters}
	open location "http://www.google.com/search?q=" & encode_URL(input as text)
end run

on encode_URL(origStr)
	return (do shell script "python -c 'import sys, urllib; print urllib.quote(sys.argv[1])' " & quoted form of origStr)
end encode_URL

.


-- script: Google Search from Selected text in any application (AsObjC) 
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"

on run {input, parameters}
	set aStr to current application's NSString's stringWithString:(input as text)
	set encodedStr to aStr's stringByAddingPercentEscapesUsingEncoding:(current application's NSUTF8StringEncoding)
	open location "http://www.google.com/search?q=" & encodedStr
end run

NOTE: To register the service properly always press DEBUG button on top of the script, only then save it. The user services are saved in the folder Services of folder Library of user domain.