过去,我为类似的任务编写了脚本。您可能还更喜欢这种方法:
tell application "Safari"
open location "https://www.google.com/search?q=" & selectedText
activate
end tell
------------------------------- HANDLERS ------------------------------------
on getSelectedText(appName)
tell application appName to activate -- bring application to front
-- get selected text, if it is selected
set the clipboard to ""
tell application "System Events" to keystroke "c" using command down
set {aSelection, maxTimeOut} to {"", 0}
repeat while aSelection is ""
set maxTimeOut to maxTimeOut + 0.1
if maxTimeOut > 1 then error "NOTHING SELECTED."
set aSelection to (the clipboard) as text
end repeat
return aSelection
end getSelectedText
on encode_URL(txt)
set python_script to ¬
"import sys, urllib; print urllib.quote(sys.argv[1])"
set python_script to "/usr/bin/python -c " & ¬
quoted form of python_script & " " & ¬
quoted form of txt
return do shell script python_script
end encode_URL