13

我试图在 OSX 下定义一个新的 URL 处理程序,它将指向一个 python 脚本。

我已经将 Python 脚本打包成一个小程序(右键单击 .py,然后打开方式 -> 构建小程序)

我在小程序的 Info.plist 中添加了以下内容:

<key>CFBundleURLTypes</key>
<array>
    <dict>
        <key>CFBundleURLName</key>
        <string>Do My Thing</string>
        <key>CFBundleURLSchemes</key>
        <array>
            <string>dmt</string>
        </array>
    </dict>
</array>

我还使用“更多 Internet 首选项”窗格将“dmt”指定为协议,但是当我尝试让它将该协议链接到我的小程序时,它说“将应用程序设置为帮助程序时出现问题”

有人知道我应该从这里去哪里吗?

谢谢

4

1 回答 1

15

经过一番折腾,我设法让它在 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
于 2010-03-23T10:35:10.217 回答