Reputation: 10329
I have an app that needs to restart the dock application. I have tried this with both Apple Script:
var errorDict: NSDictionary? = nil
let appleScript = NSAppleScript(source: "tell application \"Dock\" to quit")
var error = appleScript?.executeAndReturnError(&errorDict)
if let errorDict = errorDict {
println("An error occured: \(errorDict)")
}
... and NSTask
:
let task = NSTask()
task.launchPath = "/usr/bin/killall"
task.arguments = ["Dock"]
task.launch()
... and another NSTask
:
func restartFinder () {
let task = NSTask()
task.launchPath = "/bin/bash"
task.arguments = ["killall Dock"]
task.launch()
}
However, it seems my app is not allowed to restart it. I'd like to release my app to the AppStore, but how can I restart the dock?
Error when using Apple Script:
An error occured: {
NSAppleScriptErrorAppName = Dock;
NSAppleScriptErrorBriefMessage = "Application isn\U2019t running.";
NSAppleScriptErrorMessage = "Dock got an error: Application isn\U2019t running.";
NSAppleScriptErrorNumber = "-600";
NSAppleScriptErrorRange = "NSRange: {27, 4}";
}
Error when using NSTask
:
killall: warning: kill -TERM 255: Operation not permitted
Update
I have also tried it with STPrivilegedTask, which didn't work for me either. Neither did I get an auth window.
Upvotes: 2
Views: 620
Reputation: 1559
I would try using Applescript, but instead like this:
var errorDict: NSDictionary? = nil
let appleScript = NSAppleScript(source: "do shell script \"killall Dock\" with administrator " +
"privileges")
var error = appleScript?.executeAndReturnError(&errorDict)
if let errorDict = errorDict {
println("An error occured: \(errorDict)")
}
This way, it executes the shell script(as if through Terminal) with admin privileges. The problem is since this is a task normally only done by the system or the user, it requires you to type in the admin password.
Upvotes: 1