How can I reload com.apple.systemuiserver settings in a SystemUIServer application?

In my Mac OSX application, I have a feature that removes the system clock in the upper right corner of the screen when a button is clicked. Settings that control which system menus are displayed (including the system clock) are stored in ~ / Library / Preferences / com.apple.systemuiserver.plist. I was able to update the corresponding settings in this file to remove the system clock. However, the SystemUIServer application must be restarted so that the new settings can be reloaded and the clock can be deleted. Here is the code I used to restart SystemUIServer.

NSTask *killSystemUITask = [[NSTask alloc] init]; NSMutableArray *args = [NSMutableArray array]; [args addObject:@"SystemUIServer"]; [killSystemUITask setLaunchPath:@"/usr/bin/killall"]; [killSystemUITask setArguments:args]; [killSystemUITask launch]; 

When SystemUIServer is killed, OSX automatically restarts it. The above code works, but removing the system clock is not as clean as we would like. The entire system panel is removed for a second. Can anyone think of a better way to reload settings in SystemUIServer? Ideally, I would like the system clock to be as clean as the clock display function in the date and time settings of the System Preferences application.

+4
source share
1 answer

Do you consider trying to control your own panel using AppleEvents / OSAScript? Although the Network Prefix panel is apparently the only one with fully integrated scripting capabilities, you can control any user interface using system events. It was easy enough to prototype in AppleScript. Here is what I came up with:

 tell application "System Preferences" reveal pane "Date & Time" reveal anchor "ClockPref" of pane "Date & Time" tell application "System Events" tell tab group 1 of window 1 of process "System Preferences" repeat with cbIndex from 0 to count of checkboxes tell checkbox cbIndex if title contains "menu bar" then click exit repeat end if end tell end repeat end tell end tell quit end tell 

You can use it as is using the NSAppleScript object, or if you feel masochistic, you can dive into the little things to figure out how to properly send AppleEvents. (I recommend the former approach for sanity, but the latter will run faster at runtime.)

Pros: Easy.

Cons: launches system settings (which, as you can see, bounce in the dock), you need to activate "Enable access for assistive devices" (like many others)

It's hard to say if it is visually better or worse than killing SystemUIServer, but it is almost certainly kind to any other components that can interact with SystemUIServer.

To get rid of the rollback of the dock, this question here mentions how to get things to start without the dock icon. To this I can add that in the past I argued with this problem, and the solution that I came up with was at a high level:

  • Get a unique / secure temp directory
  • Mirror only links to the entire application suite ( pax -rwl will help with this)
  • Replace the tightly bound Info.plist with a real copy of Info.plist
  • Edit the copy in the directions in another question. (Note: there are other options here than setting NSUIElement = true , but I leave them as an exercise for the reader and google.)
  • Use the application from the temp directory
  • Delete the temp directory.

This approach turned out to be quite reliable for me when trying to use third-party applications. I assume that in the future you may have problems with system applications that are signed / isolated. (i.e. changing their Info.plist changes the signature, they may refuse to run.) In addition, it is natural that any isolated application will require a certain right or exception to send AppleEvents in general, but I would suggest that this also applies to killing system processes (if at all possible to make an isolated application at all.)

Finally, you should write a bug report to Apple , requesting a top-notch API or scripting for this if you find this important.

+2
source

Source: https://habr.com/ru/post/1341652/


All Articles