首页 > 解决方案 > 使用 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 保护。

标签: swiftcocoaapplescript

解决方案


该错误与代码无关。在 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}

推荐阅读