成功创建并注册了“search with google”系统服务:通过applescript调用python3编码选中文本,拼接google搜索url并用默认浏览器打开,全程键盘操作(control+点击→服务→选择),需重启finder生效且必须勾选系统设置中的服务开关。

你需要在Mac上实现划词后不打开浏览器、不切换窗口,直接用终端命令注册一条系统级服务,让任意应用中选中文本→右键→“服务”→点击即调用搜索引擎查词,整个流程必须全程键盘完成、无需鼠标点选菜单层级。
创建Shell脚本并赋予可执行权限
打开终端,执行以下命令创建服务脚本文件:
mkdir -p ~/Library/Services/SearchWithGoogle && cd ~/Library/Services/SearchWithGoogle && touch search_with_google.scpt
用文本编辑器写入AppleScript内容(注意:必须用touch创建空文件后再写入,否则系统服务注册会失败):
echo 'on run {input, parameters}
if (count of input) > 0 then
set searchTerm to item 1 of input
set encoded to do shell script "python3 -c \"import urllib.parse; print(urllib.parse.quote(\"" & searchTerm & "\"))\""
do shell script "open \"https://www.google.com/search?q=" & encoded & "\""
end if
end run' > search_with_google.scpt
这一步必须用python3而非python,macOS 26默认已移除系统Python 2,且urllib.parse在Python 2中不可用——不改会报错退出,服务无法激活。
将脚本注册为系统服务
在终端中执行:
plutil -convert xml1 ~/Library/Services/SearchWithGoogle/search_with_google.scpt
然后运行以下命令注入服务定义:
defaults write ~/Library/Preferences/com.apple.services MenuItems -dict-add 'Search With Google' '{ "ServiceType" = "NSApplicationService"; "ServiceDescription" = "Search selected text on Google"; "ServiceProvider" = "SearchWithGoogle"; "ServicePath" = "~/Library/Services/SearchWithGoogle/search_with_google.scpt"; }'
【必须重启访达进程才能生效】:执行 killall Finder,否则右键菜单里永远看不到新服务。
验证并启用服务
第一步:打开“系统设置”→“键盘”→“快捷键”→左侧选“服务”,在右侧滚动列表找到“搜索文本”类目,确认“Search With Google”已出现在其中且已被勾选。
第二步:打开任意支持文本选择的应用(如备忘录、Safari网页正文),选中一段文字(例如“macOS Ventura”),按下Control + 点击呼出上下文菜单,向下滚动至“服务”子菜单,应可见“Search With Google”选项。
第三步:点击该选项,系统将自动编码选中文本、拼接URL、调用默认浏览器打开Google搜索结果页——整个过程无弹窗、无确认提示、不中断当前操作流。











