gpt4 book ai didi

swift/AppleScript : Cannot run apple script from Swift code

转载 作者:行者123 更新时间:2023-11-28 07:35:19 25 4
gpt4 key购买 nike

我尝试像这样从 Swift 代码运行 AppleScript 命令:

 var appleScriptCmd = "tell application \"System Events\" to make login item at end with properties {path:\"" + appPath +  "\", hidden:false, name:\"Some App\"}";

var appleScriptCmd2 = "tell application \"System Events\" to set visible of process \"Safari\" to false";

然后我都试过了:

let script = NSAppleScript(source: appleScriptCmd2)!;
var errorDict : NSDictionary?
script.executeAndReturnError(&errorDict)
if errorDict != nil { print(errorDict!) }

或较旧的方法:

Process.launchedProcess(launchPath: "/usr/bin/osascript", arguments: ["-e", appleScriptCmd])

两者都不起作用,同时我尝试过的两个命令都在使用 osascript -e "some command"工具的终端程序中工作。

最佳答案

由于您的应用是沙盒化的(项目设置 > 功能打开应用沙盒),您有以下三种选择:

  • 添加temporary entitlements用于您要使用的应用程序。
  • 将您的脚本放在~/Library/Application Scripts/ 中的适当目录中并使用NSUserAppleScriptTask .
  • 实现 AppleScriptObjC 桥并从 ASOC 框架运行 AppleScript 代码(还需要 Objective-C 桥接头文件)。

在沙盒应用程序中 NSAppleScript 拒绝工作。

关于 swift/AppleScript : Cannot run apple script from Swift code,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53291754/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com