简体   繁体   English

NSUserUnixTask 悄悄地忽略脚本

[英]NSUserUnixTask ignores script quietly

I'm writing a sandboxed app in Swift 4 on macOS High Sierra using Xcode 9. My app has a shell script file (say, myscript ) copied to the Bundle.main.resourceURL directory when installing.我正在使用 Xcode 9 在 macOS High Sierra 上的 Swift 4 中编写沙盒应用程序。我的应用程序在安装时将一个 shell 脚本文件(例如myscript )复制到Bundle.main.resourceURL目录。 I want to run this shell script using NSUserUnixTask as follows:我想使用NSUserUnixTask运行这个 shell 脚本,如下所示:

if let scriptUrl = URL(string: "myscript", relativeTo: Bundle.main.resourceURL) {
    do {
        let task = try NSUserUnixTask(url: scriptUrl)
        task.execute(withArguments: [ "hello" ])
        debugPrint("OK")
    } catch let error {
        debugPrint(error)
    }
} else {
    debugPrint("Script not found")
}

// output: "OK"

This gives no error messages and "OK" is correctly displayed.这不会给出错误消息,并且正确显示“OK”。 But the myscript script seems to be completely ignored.但是myscript脚本似乎被完全忽略了。 When I run myscript from Terminal instead, it runs as intended.当我从Terminal运行myscript ,它按预期运行。

The test myscript file looks like:测试myscript文件如下所示:

#!/bin/sh
/usr/bin/osascript -e "tell app \"System Events\" to display dialog \"Message: $1\""

(The is only a test script; the real one calls emacsclient .) The shell script file is set executable (permission 755). (这只是一个测试脚本;真正的调用emacsclient 。)shell 脚本文件设置为可执行(权限 755)。 Not only this shell script but also any other shell script seems ignored.不仅这个 shell 脚本而且任何其他 shell 脚本似乎都被忽略了。 For example, if the second line is replaced with /usr/bin/printf "\\a" , it does not beep.例如,如果将第二行替换为/usr/bin/printf "\\a" ,则不会发出哔声。 I tried many other shell scripts but nothing seems to work.我尝试了许多其他 shell 脚本,但似乎没有任何效果。

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 applicationScriptsDirectory folder."类文档说“如果应用程序是沙盒的,那么脚本必须在applicationScriptsDirectory文件夹中。” Now I want to know how to copy the script file upon installation.现在我想知道如何在安装时复制脚本文件。

EDIT 2:编辑2:

I manually copied myscript to ~/Library/Applications Scripts/com.mydomain.myApp/ and then changed the first line of the above Swift codes ( if let scriptUrl ... ) to我手动将myscript复制到~/Library/Applications Scripts/com.mydomain.myApp/然后将上述 Swift 代码的第一行( if let scriptUrl ... )更改为

let scriptFolderUrl = try! FileManager.default.url(for: .applicationScriptsDirectory, 
    in: .userDomainMask, appropriateFor: nil, create: true)
if let scriptUrl = URL(string: "myscript", relativeTo: scriptFolderURL) {

https://tutel.me/c/programming/questions/43741325/swift+3++sandbox++how+to+create+applicationscriptsdirectory+if+it+doesn39t+exist https://tutel.me/c/programming/questions/43741325/swift+3++sandbox++how+to+create+applicationscriptsdirectory+if+it+doesn39t+exist

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. pkamp要求一个正确的答案。 :) :)

As the class document says, the script must be in the applicationScriptsDirectory folder.正如类文档所说,脚本必须在applicationScriptsDirectory文件夹中。 This folder is not writable by a sandboxed application, as vadian pointed out.正如vadian指出的那样,这个文件夹不能被沙盒应用程序写入。 The script ( myscript ) must be copied manually to the applicationScriptDirectory folder (eg, ~/Library/Applications Scripts/com.mydomain.myApp/ ).脚本 ( myscript ) 必须手动复制到applicationScriptDirectory文件夹(例如, ~/Library/Applications Scripts/com.mydomain.myApp/ )。

After this done, change the first line to the following:完成后,将第一行更改为以下内容:

// --------------------------------------------------------
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")
}

I benefited from this link for the first line.我从第一行的这个链接中受益。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM