Last active
July 7, 2017 00:40
-
-
Save jprichards/71a26c1640d12ae1c9c497cf53e4283c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# encoding utf-8 | |
# | |
# Requires an MCX profile with the following (example) format: | |
# | |
# <key>PayloadType</key> | |
# <string>com.apple.ManagedClient.preferences</string> | |
# <key>PayloadContent</key> | |
# <dict> | |
# <key>CustomConfig</key> | |
# <dict> | |
# <key>Forced</key> | |
# <array> | |
# <dict> | |
# <key>mcx_preference_settings</key> | |
# <dict> | |
# <key>config_arguments</key> | |
# <array> | |
# <string>/Applications/Utilities/Yo.app/Contents/MacOS/yo</string> | |
# <string>-t</string> | |
# <string>test title</string> | |
# <string>-s</string> | |
# <string>subtitle</string> | |
# <string>-b</string> | |
# <string>OK</string> | |
# </array> | |
# </dict> | |
# </dict> | |
# </array> | |
# </dict> | |
# </dict> | |
from Foundation import CFPreferencesCopyAppValue, NSDistributedNotificationCenter | |
from AppKit import * | |
from PyObjCTools import AppHelper | |
import subprocess | |
import textwrap | |
class ConfigHelper(NSObject): | |
previous_config = [] | |
def getConfig_(self, config): | |
config_details = {} | |
bundleID = 'CustomConfig' | |
pref_name = 'config_arguments' | |
info = config.userInfo() | |
config_details = dict(zip(info.keys(), info.values())) | |
if str(config_details["com.apple.MCX.changedDomains"]).find(bundleID): | |
raw_args = CFPreferencesCopyAppValue(pref_name, bundleID) | |
if raw_args is not None: | |
raw_args = str(raw_args) | |
parsed_args = textwrap.dedent(raw_args.strip("()"). | |
replace("\"", ""). | |
replace(",", "")).splitlines() | |
cleaned_args = filter(lambda x: x != '', parsed_args) | |
if cleaned_args == self.previous_config: | |
print "no change" | |
else: | |
print "Running command: " + ' '.join(map(str, cleaned_args)) | |
runCommand(cleaned_args) | |
self.previous_config = cleaned_args | |
def runCommand(cmd): | |
try: | |
proc = subprocess.Popen(cmd, shell=False, bufsize=-1, | |
stdin=subprocess.PIPE, | |
stdout=subprocess.PIPE, | |
stderr=subprocess.PIPE) | |
output = proc.communicate() | |
return output | |
except Exception: | |
pass | |
nc = NSDistributedNotificationCenter.defaultCenter() | |
ConfigHelper = ConfigHelper.new() | |
nc.addObserver_selector_name_object_( | |
ConfigHelper, | |
'getConfig:', | |
'com.apple.MCX._managementStatusChangedForDomains', | |
'com.apple.MCX' | |
) | |
NSLog("Listening for new configs...") | |
AppHelper.runConsoleEventLoop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment