Swift/AppleScript:无法从 Swift 代码运行 Apple 脚本 [英] Swift/AppleScript: Cannot run apple script from Swift code

查看:38
本文介绍了Swift/AppleScript:无法从 Swift 代码运行 Apple 脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I try to run AppleScript command from Swift code like this:

 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" 工具从终端程序工作的.

neither works and simultaneously both commands I have tried are working from Terminal program using osascript -e "some command" tool.

推荐答案

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

Since your app is sandboxed (Project Settings > Capabilities turn on App Sandbox) you have three options:

  • 添加临时权利用于您要使用的应用程序.
  • 将您的脚本放在 ~/Library/Application Scripts/ 中的适当目录中并使用 NSUserAppleScriptTask.
  • 实现 AppleScriptObjC 桥接并运行 ASOC 框架中的 AppleScript 代码(还需要一个 Objective-C 桥接头文件).
  • Add temporary entitlements for the applications you want to use.
  • Put your scripts in the appropriate directory in ~/Library/Application Scripts/ and use NSUserAppleScriptTask.
  • Implement an AppleScriptObjC bridge and run the AppleScript code from the ASOC framework (requires also an Objective-C bridging header file).

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

In a sandboxed app NSAppleScript refuses to work.

这篇关于Swift/AppleScript:无法从 Swift 代码运行 Apple 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆