-
Notifications
You must be signed in to change notification settings - Fork 4
/
plist_example.py
executable file
·60 lines (50 loc) · 2.19 KB
/
plist_example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/python
# The Following bits were borrowed from Greg Neagle's FoundationPlist:
# https://code.google.com/p/munki/source/browse/code/client/munkilib/FoundationPlist.py
from Foundation import NSData, \
NSPropertyListSerialization, \
NSPropertyListMutableContainers, \
NSPropertyListXMLFormat_v1_0
class FoundationPlistException(Exception):
pass
class NSPropertyListSerializationException(FoundationPlistException):
pass
class NSPropertyListWriteException(FoundationPlistException):
pass
def readPlist(filepath):
"""
Read a .plist file from filepath. Return the unpacked root object
(which is usually a dictionary).
"""
plistData = NSData.dataWithContentsOfFile_(filepath)
dataObject, plistFormat, error = \
NSPropertyListSerialization.propertyListFromData_mutabilityOption_format_errorDescription_(
plistData, NSPropertyListMutableContainers, None, None)
if error:
error = error.encode('ascii', 'ignore')
errmsg = "%s in file %s" % (error, filepath)
raise NSPropertyListSerializationException(errmsg)
else:
return dataObject
def writePlist(dataObject, filepath):
'''
Write 'rootObject' as a plist to filepath.
'''
plistData, error = \
NSPropertyListSerialization.dataFromPropertyList_format_errorDescription_(
dataObject, NSPropertyListXMLFormat_v1_0, None)
if error:
error = error.encode('ascii', 'ignore')
raise NSPropertyListSerializationException(error)
else:
if plistData.writeToFile_atomically_(filepath, True):
return
else:
raise NSPropertyListWriteException(
"Failed to write plist data to %s" % filepath)
# Set the plist_path object to the path of the plist you wish to edit
plist_path="/Users/nate/Library/Preferences/com.apple.Safari.plist"
# Get the contents of the plist specified in plist_path and assign them to plist_contents
plist_contents = readPlist(plist_path)
# Print out the contents of the 'DomainsToNeverSetUp' key found in plist_contents
print plist_contents["DomainsToNeverSetUp"]