Search code examples
swiftmacosapplescript

Enable touchid in the Apple Script in Swift


NSAppleScript(source: "do shell script \"sudo chmod 777 '\(appPath.path)'\" with administrator " + "privileges")!.executeAndReturnError(nil)

When I run this code in Xcode, the app gives a alert

enter image description here

But if I run same code in the Apple Scrip app, the app will give a touch id alert.

How can I show a touch id alert when I run this code in the Xcode?


Solution

  • Finally, I found it is impossible to do this. But there is an alternative way.s

    guard let shBundlePath = Bundle.main.path(forResource: "chmodHelper", ofType: "sh") else { fatalError("Cannot get the sh file path") }
            guard let installerHelperPath = Bundle.main.path(forResource: "installHelper", ofType: "sh") else { fatalError("Cannot get the sh file path") }
            let helperToolURL = URL.documents.universalappending(path: "chmodHelper.sh")
            if FileManager.default.fileExists(atPath: helperToolURL.universalPath()) {
                try FileManager.default.removeItem(at: helperToolURL)
            }
            try FileManager.default.copyItem(atPath: shBundlePath, toPath: helperToolURL.universalPath())
            try setInstallerContent(path: helperToolURL.universalPath())
            NSAppleScript(source: "do shell script \"chmod +x '\(installerHelperPath)' && sudo '\(installerHelperPath)'\" with administrator " + "privileges")!.executeAndReturnError(nil)
    

    Run this when the app starts.

    And then change the content of the shell file. Then use apple scrip to run it. So you can avoid the alert