Sunday, December 4, 2022

#1 2022-09-26 02:02:05 am

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 2589

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:
.

Applescript:


-- 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

.

Applescript:


-- 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.
.

Applescript:


-- 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

.

Applescript:


-- 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.

Last edited by KniazidisR (2022-09-26 03:20:26 am)


Model: MacBook Pro
OS X: Catalina 10.15.7
Web Browser: Safari 15.6.1
Ram: 4 GB

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)