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

查看:471
本文介绍了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某些命令"工具在终端程序中都无法使用,同时我尝试过的两个命令也都无法使用.

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天全站免登陆