我正在尝试在 macOS 上配置自定义方案处理程序从命令行为其他 mac 用户提出可重现的过程。
我愿意接受任何替代方式,但这里有一个对我有用的等效手动程序:
on open location this_URL
tell application "Terminal"
activate
do script "/tmp/my-custom-handler.sh '" & this_URL & "' ; exit"
end tell
end open location
Save as: my-custom-handler
和 Format: Application
这将创建一个已可用的工作
my-custom-handler.app
:
open -a my-custom-handler.app my-custom-handler://anexample?of=uri&with=parameter
我们必须注意到,该应用程序有一个
my-custom-handler.app/Contents/Info.plist
文件,其中 CFBundleIdentifier
设置为 com.apple.ScriptEditor.id.my-custom-handler
。
-a
命令指定 open
参数并让它自动使用正确的应用程序本身。可以使用以下命令:
plutil -insert "LSHandlers.0" -json '{"LSHandlerPreferredVersions":{"LSHandlerRoleAll":"-"},"LSHandlerRoleAll":"com.apple.ScriptEditor.id.my-custom-handler","LSHandlerURLScheme":"my-custom-handler"}' ~/Library/Preferences/com.apple.LaunchServices/com.apple.launchservices.secure.plist
注意它需要应用程序标识符ID
现在使用简单的以下命令即可完美运行:
open my-custom-handler://anexample?of=uri&with=parameter
好吧,现在很酷,我想要一种自动/命令行方式来执行相同的操作。 正如你所看到的,我已经使用
plutil
获得了方案关联部分。
所以我错过的是相当于从命令行将应用程序保存为 Script Editor
的操作。这是我测试的内容,其中Application
是一个纯文本文件,其中包含在上面的手动过程中粘贴到脚本编辑器的程序:
my-custom-handler.applescript
它有效,我可以再次使用:
osacompile -o my-custom-handler.app my-custom-handler.applescript
open -a my-custom-handler.app my-custom-handler://anexample?of=uri&with=parameter
命令创建的应用程序没有
osacompile
,所以基本上不可能像以前一样使用 CFBundleIdentifier
设置与我们的自定义方案的关联...好吧,没什么大不了的,我只需编辑新应用程序的 plutil
文件,使用像
Info.plist
这样的命令来附加即可轻松实现自动化:sed
现在我可以使用此标识符来使用命令关联我的方案:
<key>CFBundleIdentifier</key>
<string>org.toto.my-custom-handler</string>
但是......遗憾的是,一旦我编辑应用程序的
plutil -insert "LSHandlers.0" -json '{"LSHandlerPreferredVersions":{"LSHandlerRoleAll":"-"},"LSHandlerRoleAll":"org.toto.my-custom-handler","LSHandlerURLScheme":"my-custom-handler"}' ~/Library/Preferences/com.apple.LaunchServices/com.apple.launchservices.secure.plist
,它就不再在运行时工作,我看到它尝试打开应用程序,但什么也没发生。
所以短期内:我有一种使用
Info.plist
osacompile
所以我现在陷入困境,以下之一可以解决这个问题:
告诉
osacompile
编辑:我使用的是 MacO,大确定。
EDIT2:如果我们忘记“方案”部分并简化苹果脚本以删除事件部分,例如:
osacompile
我也有类似的行为:
它会一直工作,直到我不编辑 .app 中的任何内容为止
无权将 Apple 事件发送到系统事件
所以我对防止编辑应用程序文件的安全保护的猜测似乎至少部分正确。希望它可以帮助一些Macos专家:)
tell application "Terminal"
activate
do script "/tmp/my-custom-handler.sh '" & this_URL & "' ; exit"
end tell
中指示它可以处理
Info.plist
方案:my-custom-scheme
plutil -insert CFBundleURLTypes -json '[{"CFBundleURLName":"my-custom-scheme URL","CFBundleURLSchemes":["my-custom-scheme"]}]' my-custom-handler.app/Contents/Info.plist
/System/Library/Frameworks/CoreServices.framework/Versions/Current/Frameworks/LaunchServices.framework/Versions/Current/Support/lsregister my-custom-handler.app
URL),您可能还需要在
file:
下注册(从而遇到需要更新包 ID 的原始障碍)。不过,我似乎没有遇到任何代码签名问题。请参阅下面的完整脚本:LSHandlers