NSUserUnixTask静默忽略脚本 [英] NSUserUnixTask ignores script quietly
问题描述
我正在使用Xcode 9在macOS High Sierra上的Swift 4中编写一个沙盒应用程序.我的应用程序具有复制到 Bundle.main的shell脚本文件(例如,
目录.我想使用 myscript
).安装时的resourceURL NSUserUnixTask
运行此Shell脚本,如下所示:
if let scriptUrl = URL(string:"myscript",relativeTo:Bundle.main.resourceURL){做 {让任务=尝试NSUserUnixTask(URL:scriptUrl)task.execute(withArguments:["hello"])debugPrint(确定")}捕获让错误{debugPrint(错误)}} 别的 {debugPrint(找不到脚本")}//输出:确定"
这不会显示任何错误消息,并且正确显示"OK".但是 myscript
脚本似乎被完全忽略了.当我改为从 Terminal
运行 myscript
时,它将按预期运行.
测试的 myscript
文件如下所示:
#!/bin/sh/usr/bin/osascript -e告诉应用程序\"系统事件\以显示对话框\"消息:$ 1 \"
(这只是一个测试脚本;真正的脚本调用 emacsclient
.)Shell脚本文件设置为可执行文件(权限755).不仅这个shell脚本而且其他任何shell脚本似乎都被忽略了.例如,如果第二行替换为/usr/bin/printf"\ a"
,则不会发出哔声.我尝试了许多其他Shell脚本,但似乎无济于事.
我该如何解决这个问题?
我很仓促.类文档显示如果应用程序是沙盒,那么脚本必须位于 applicationScriptsDirectory
文件夹中."现在,我想知道如何在安装时复制脚本文件.
我手动将 myscript
复制到〜/Library/Applications Scripts/com.mydomain.myApp/
,然后更改了以上Swift代码的第一行(如果让scriptUrl ...
)
让scriptFolderUrl =试试!FileManager.default.url(用于:.applicationScriptsDirectory,于:.userDomainMask,适当于:无,创建:true)如果让scriptUrl = URL(string:"myscript",relativeTo:scriptFolderURL){
脚本运行.现在,我需要弄清楚在首次安装或运行应用程序时如何将脚本复制到scripts文件夹.
pkamp 请求了正确的答案.:)
正如类文档所述,脚本必须位于applicationScriptsDirectory
文件夹. vadian 指出,沙盒应用程序无法写入此文件夹.脚本( myscript
)必须手动复制到 applicationScriptDirectory
文件夹(例如,〜/Library/Applications Scripts/com.mydomain.myApp/
).
完成此操作后,将第一行更改为以下内容:
//--------------------------------------------------------让scriptFolderUrl =试试!FileManager.default.url(用于:.applicationScriptsDirectory,于:.userDomainMask,适当于:无,创建:true)如果让scriptUrl = URL(string:"myscript",relativeTo:scriptFolderURL){//--------------------------------------------------------//下面相同做 {让任务=尝试NSUserUnixTask(URL:scriptUrl)task.execute(withArguments:["hello"])debugPrint(确定")}捕获让错误{debugPrint(错误)}} 别的 {debugPrint(找不到脚本")}
我受益于 This gives no error messages and "OK" is correctly displayed. But the The test (The is only a test script; the real one calls How can I solve this issue? EDIT: I was hasty. The class document says "If the application is sandboxed, then the script must be in the EDIT 2: I manually copied The script runs. Now I need to figure out how to copy the script to the scripts folder when installing or running the app the first time. pkamp requested a proper answer. :) As the class document says, the script must be in the After this done, change the first line to the following: I benefited from this link for the first line. 这篇关于NSUserUnixTask静默忽略脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!myscript
script seems to be completely ignored. When I run myscript
from Terminal
instead, it runs as intended.myscript
file looks like:#!/bin/sh
/usr/bin/osascript -e "tell app \"System Events\" to display dialog \"Message: $1\""
emacsclient
.) The shell script file is set executable (permission 755). Not only this shell script but also any other shell script seems ignored. For example, if the second line is replaced with /usr/bin/printf "\a"
, it does not beep. I tried many other shell scripts but nothing seems to work.applicationScriptsDirectory
folder." Now I want to know how to copy the script file upon installation.myscript
to ~/Library/Applications Scripts/com.mydomain.myApp/
and then changed the first line of the above Swift codes (if let scriptUrl ...
) tolet scriptFolderUrl = try! FileManager.default.url(for: .applicationScriptsDirectory,
in: .userDomainMask, appropriateFor: nil, create: true)
if let scriptUrl = URL(string: "myscript", relativeTo: scriptFolderURL) {
applicationScriptsDirectory
folder. This folder is not writable by a sandboxed application, as vadian pointed out. The script (myscript
) must be copied manually to the applicationScriptDirectory
folder (e.g., ~/Library/Applications Scripts/com.mydomain.myApp/
).// --------------------------------------------------------
let scriptFolderUrl = try! FileManager.default.url(for: .applicationScriptsDirectory,
in: .userDomainMask, appropriateFor: nil, create: true)
if let scriptUrl = URL(string: "myscript", relativeTo: scriptFolderURL) {
// --------------------------------------------------------
// same below
do {
let task = try NSUserUnixTask(url: scriptUrl)
task.execute(withArguments: [ "hello" ])
debugPrint("OK")
} catch let error {
debugPrint(error)
}
} else {
debugPrint("Script not found")
}