2

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.

Paul Peelen
  • 10,073
  • 15
  • 85
  • 168
  • You can't do that from a sandboxed application. What are you trying to accomplish? –  Jul 14 '15 at 22:01
  • I am trying to restart the Dock because I changed the key `mcx-expose-disabled` for `com.apple.dock` using `CFPreferencesSetAppValue`. To make the changes take effect I need to restart the Dock. – Paul Peelen Jul 15 '15 at 05:59
  • You can't write to the Dock preferences from a sandboxed application either, as far as I know? –  Jul 15 '15 at 06:41
  • That seems to work just fine, either that or `CFPreferencesSetAppValue` saves the values separately just for my app. After every app restart, it remembers the latest setting. – Paul Peelen Jul 15 '15 at 06:58
  • Try force-quitting the Dock yourself, or running `defaults read com.apple.dock` from the Terminal, to see if anything actually got applied, but I strongly suspect that you're writing the preference to your app sandbox. Allowing a sandboxed app to modify another app's preferences would be a huge security hole. –  Jul 15 '15 at 07:02
  • That's sounds like a valid point. Isn't there some way with entitlements to change an others app settings? If so, how? I'll try your suggestion as soon as I can. – Paul Peelen Jul 15 '15 at 07:33
  • It may be possible by adding entitlements, but your chances of getting that past Apple's app review process are basically nil, particularly because you're trying to set an unsupported default that makes standard OS features stop working. –  Jul 15 '15 at 07:39
  • @duskwuff you are right. It is not being changed. I need to find a way to change `com.apple.dock`, the right way... which Apple will allow. Any suggestions? – Paul Peelen Jul 19 '15 at 08:17

1 Answers1

1

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.

SentientBacon
  • 1,509
  • 3
  • 11
  • 19
  • Thnx. However, this gave me the following error: `NSAppleScriptErrorMessage = "The administrator user name or password was incorrect.";` with `NSAppleScriptErrorRange = "NSRange: {0, 60}";`. – Paul Peelen Jul 15 '15 at 05:44
  • Did it prompt you like, "Myapp requests to make changes." And ask for your password? You may want to try to "remove with administrator privileges" – SentientBacon Jul 15 '15 at 13:52
  • Or, is your password just wrong? Perhaps you do not have administrative privileges? – SentientBacon Jul 15 '15 at 13:55
  • I'm not getting a auth window presented, which I expect to get presented. – Paul Peelen Jul 18 '15 at 19:53
  • So the code you are using is the one I suggested without the administrative privileges part, and instead of giving you the auth window, it throws that error. Seeing https://developer.apple.com/library/mac/technotes/tn2065/_index.html, it seems you need to add a user name parameter as "root" – SentientBacon Jul 19 '15 at 00:19
  • Or if that doesn't work try reverting the code to the version without administrator privileges and adding sudo to force a admin password request – SentientBacon Jul 19 '15 at 00:26
  • Tried the `sudo` as well. I'll see if I can find a way to add the user parameter to it. Thats worth a try. – Paul Peelen Jul 19 '15 at 08:18