Skip to content

Instantly share code, notes, and snippets.

@gregneagle
Created January 26, 2015 20:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gregneagle/55ef3a0c0232074a1f58 to your computer and use it in GitHub Desktop.
Save gregneagle/55ef3a0c0232074a1f58 to your computer and use it in GitHub Desktop.
Python scripts for MacTech MacEnterprise column "Managing Complex Preferences"
import CoreFoundation
from Foundation import NSDate
# read Safari's current ManagedPlugInPolicies
policy = CoreFoundation.CFPreferencesCopyAppValue(
'ManagedPlugInPolicies', 'com.apple.Safari')
if not 'com.oracle.java.JavaAppletPlugin' in policy:
# create an empty dict
policy['com.oracle.java.JavaAppletPlugin'] = {}
if not 'PlugInHostnamePolicies' in policy[
'com.oracle.java.JavaAppletPlugin']:
# create an empty array
policy['com.oracle.java.JavaAppletPlugin'][
'PlugInHostnamePolicies'] = []
found_redacted_vpn = False
# iterate through dicts in
# com.oracle.java.JavaAppletPlugin:PlugInHostnamePolicies
# looking for one containing our desired PlugInHostname
for dict_item in policy[
'com.oracle.java.JavaAppletPlugin'][
'PlugInHostnamePolicies']:
if dict_item.get('PlugInHostname') == 'vpn.redacted.com':
found_redacted_vpn = True
if not found_redacted_vpn:
# we didn't find one, so create one
redacted_vpn = {
'PlugInPageURL':
'https://vpn.redacted.com/index.cgi',
'PlugInHostname': 'vpn.redacted.com',
'PlugInRunUnsandboxed': True,
'PlugInPolicy':
'PlugInPolicyAllowNoSecurityRestrictions',
'PlugInLastVisitedDate': NSDate.date()
}
# add our new policy to the array
policy['com.oracle.java.JavaAppletPlugin'][
'PlugInHostnamePolicies'].append(redacted_vpn)
# save the changed preference
CoreFoundation.CFPreferencesSetAppValue(
'ManagedPlugInPolicies', policy, 'com.apple.Safari')
CoreFoundation.CFPreferencesAppSynchronize(
'com.apple.Safari')
import CoreFoundation
from Foundation import NSDate
from Foundation import NSMutableArray, NSMutableDictionary
# read the current ManagedPlugInPolicies
policy = CoreFoundation.CFPreferencesCopyAppValue(
'ManagedPlugInPolicies', 'com.apple.Safari')
if policy:
# policy is an immutable dict,
# so we have to make a mutable copy
my_policy = NSMutableDictionary.alloc(
).initWithDictionary_copyItems_(policy, True)
else:
# create an empty dict
my_policy = {}
if 'com.oracle.java.JavaAppletPlugin' in my_policy:
# make a mutable copy of the dict
current_dict = my_policy[
'com.oracle.java.JavaAppletPlugin']
my_policy['com.oracle.java.JavaAppletPlugin'] = (
NSMutableDictionary.alloc(
).initWithDictionary_copyItems_(
current_dict, True))
else:
# create an empty dict
my_policy['com.oracle.java.JavaAppletPlugin'] = {}
if 'PlugInHostnamePolicies' in my_policy[
'com.oracle.java.JavaAppletPlugin']:
# make a mutable copy of the array
current_array = my_policy[
'com.oracle.java.JavaAppletPlugin'][
'PlugInHostnamePolicies']
my_policy['com.oracle.java.JavaAppletPlugin'][
'PlugInHostnamePolicies'] = NSMutableArray.alloc(
).initWithArray_(current_array)
else:
# create an empty array
my_policy['com.oracle.java.JavaAppletPlugin'][
'PlugInHostnamePolicies'] = []
found_redacted_vpn = False
# iterate through dicts in
# com.oracle.java.JavaAppletPlugin:PlugInHostnamePolicies
# looking for one containing our desired PlugInHostname
for dict_item in my_policy[
'com.oracle.java.JavaAppletPlugin'][
'PlugInHostnamePolicies']:
if dict_item.get(
'PlugInHostname') == 'vpn.redacted.com':
found_redacted_vpn = True
if not found_redacted_vpn:
redacted_vpn = {
'PlugInPageURL':
'https://vpn.redacted.com/index.cgi',
'PlugInHostname': 'vpn.redacted.com',
'PlugInRunUnsandboxed': True,
'PlugInPolicy':
'PlugInPolicyAllowNoSecurityRestrictions',
'PlugInLastVisitedDate': NSDate.date()
}
# add our new policy to the array
my_policy['com.oracle.java.JavaAppletPlugin'][
'PlugInHostnamePolicies'].append(redacted_vpn)
# save the changed preference
CoreFoundation.CFPreferencesSetAppValue(
'ManagedPlugInPolicies', my_policy, 'com.apple.Safari')
CoreFoundation.CFPreferencesAppSynchronize(
'com.apple.Safari')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment