经过一番折腾,我设法让它在 OSX 下工作......
这就是我的做法:
在 AppleScript 脚本编辑器中,编写以下脚本:
on open location this_URL
do shell script "/scripts/runLocalCommand.py '" & this_URL & "'"
end open location
如果您想确保从某个 shell 运行 Python(在我的情况下,我通常使用 tcsh,并且有一个 .tcshrc 文件,该文件定义了我想要访问的一些环境变量)然后那个中间线可能想要:
do shell script "tcsh -c \"/scripts/localCommand.py '" & this_URL & "'\""
我想在 python 脚本中进行所有实际处理 - 但由于 URL 处理程序在 OSX 中的工作方式,它们必须调用应用程序包而不是脚本,因此在 AppleScript 中执行此操作似乎是最简单的方法做。
在脚本编辑器中,另存为“应用程序包”
找到保存的应用程序包,然后打开内容。找到 Info.plist 文件,然后打开它。添加以下内容:
<key>CFBundleIdentifier</key>
<string>com.mycompany.AppleScript.LocalCommand</string>
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLName</key>
<string>LocalCommand</string>
<key>CFBundleURLSchemes</key>
<array>
<string>local</string>
</array>
</dict>
</array>
就在最后两行之前,应该是:
</dict>
</plist>
那里可能需要更改三个字符串:
com.mycompany.AppleScript.LocalCommand
LocalCommand
local
其中第三个是处理程序 ID - 因此 URL 将是 local://something
因此,这将传递给 Python 脚本。
这就是我所拥有的:
#!/usr/bin/env python
import sys
import urllib
arg = sys.argv[1]
handler, fullPath = arg.split(":", 1)
path, fullArgs = fullPath.split("?", 1)
action = path.strip("/")
args = fullArgs.split("&")
params = {}
for arg in args:
key, value = map(urllib.unquote, arg.split("=", 1))
params[key] = value