swift - 使用 applescript 将我的应用程序添加到登录项,但得到一个奇怪的权利权限问题
问题描述
我正在使用此代码在应用程序启动时将我的应用程序添加到登录项中
if tempVarForAddTologinItemAtLaunch == true{
DispatchQueue.global(qos: .background).async{
var error: NSDictionary?
if let scriptObject = NSAppleScript(source: "tell application \"System Events\" \n if exists login item \"" + Bundle.main.bundleIdentifier! + "\" then \n delete login item \"myApp\" \n end if \n make new login item at end of login items with properties {name:\"" + Bundle.main.bundleIdentifier! + "\", path:\"" + String(Bundle.main.bundleURL.absoluteString).replacingOccurrences(of: "file://", with: "").dropLast().replacingOccurrences(of: "%20", with: " ") + "\", hidden:false} \n end tell \n") {
if let outputString = scriptObject.executeAndReturnError(&error).stringValue {print(outputString)} else if (error != nil) {print("error: ", error!)}
}
}
}
我在控制台中不断收到此错误,如果我在另一个用户的帐户中尝试我的应用程序,代码将不起作用
2019-04-15 20:31:45.474846-0400 myApp[11185:13342142] 跳过脚本添加“/Library/ScriptingAdditions/SASyphonInjector.osax”,因为它不受 SIP 保护。
解决方案
该错误与代码无关。在 10.14 Mojave Apple 不再允许第三方脚本添加。
您可以简化代码,替换
path:\"" + String(Bundle.main.bundleURL.absoluteString).replacingOccurrences(of: "file://", with: "").dropLast().replacingOccurrences(of: "%20", with: " ") + "\", hidden:false}
和
path:\"" + Bundle.main.bundlePath + "\", hidden:false}
推荐阅读
- amazon-web-services - 如何将 AWS Organizations 下的 aws 账户注册到 Control Tower 创建的 OU
- java - 当我想在 Google BigQuery 中创建 StandardSQLTypeName.ARRAY 类型的字段时接收 NPE
- api - 如何更改 API 平台中的基本 URL
- python - 实例字典的行为类似于 Python 中的类字典
- css - 自托管下载字体
- xamarin.forms - xamarin 表单中是否有一种方法,例如向导多步进度
- flutter - Flutter InAppWebView 在发布模式下显示空白页
- ios - Charles Proxy iOS 坚持安装配置文件
- java - CameraX:动画缩放
- javascript - TypeError:即使定义了方法,“x”也不是函数