def write_keys(path, keys, test=False):
    """
    Write key structure and its values to the given property list.
    If a key does not exist in the target plist, it is created as a dictionary by default.

    path
        An absolute path to a property list (.plist) file, including the extension

    keys
        A dict describing a structure and value(s) that should exist inside the target plist

    test
        If test is true, no changes will be written, but you will receive a dict containing the changes that
        would have been performed.
    """
    dataObject = _read_plist(path)

    if dataObject is None:
        dataObject = NSMutableDictionary()

    changed = {}
    _set_objects_for_keys(dataObject, keys, changed)

    if test == False:
        _write_plist(dataObject, path)

    return changed
def _object_for_key_list(dict, keys, create=False):
    '''
    Get an object inside a nested NSDictionary structure, using a list of keys to traverse.

    If create is true, then missing elements are automatically created as NSMutableDictionary objects.
    '''
    key = keys.pop(0)

    # User accidentally supplies zero length key
    if len(key) == 0:
        return dict

    if len(keys) > 0:
        value = dict.objectForKey_(key)
        if value is None:
            if create:
                created = NSMutableDictionary()
                dict.setObject_forKey_(created, key)
                return _object_for_key_list(created, keys, create)
            else:  # Just return None if path was not found
                log.debug('No key found in Property List: {0}'.format(key))
                return None
        else:
            return _object_for_key_list(dict.objectForKey_(key), keys, create)
    else:
        # TODO: special case for array index notation [x] if current object is NSArray
        # if dict.isKindOfClass_(NSArray.class_()):
        # return
        return dict.objectForKey_(key)
def _set_objects_for_keys(dict, keys, changed=None):
    """Set plist values using a given dict.

    Recursively finds or creates keys given and sets their values. This can be used to maintain a partial or
    complete override of any given property list file.

        Args:
            dict (NSMutableDictionary): The current dictionary being operated on. For a non existent file this will be
            blank.

            keys: A dict representing a hierarchy with leaf node values.
    """
    if changed is None:
        changed = dict()

    for key, value in keys.items():
        existing_value = dict.objectForKey_(key)

        if type(value) is dict:
            # Value unavailable, so create structure
            if existing_value is None:
                child = NSMutableDictionary()
                dict.setObject_forKey_(child, key)

            changed[key] = {}
            _set_objects_for_keys(child, value, changed[key])
        else:
            if existing_value != value:
                dict.setObject_forKey_(value, key)
                changed[key] = value
def write_key(path, key, nstype, value):
    '''
    Write the value of a key contained within the Property List file specified.
    If the property list file does not exist, the default behaviour is to create it with the keys/values given.

    path
        An absolute path to a property list (.plist) file, including the extension

    key
        The path specification for the key to modify. A list of keys separated by a colon.

    nstype
        The value type to write, one of 'string', 'int', 'float', 'bool'

    value
        The property value. If not specified it will be set to an empty value.

    CLI Example:

    .. code-block:: bash

        salt '*' plist.write <path> <key> <nstype> [value]
    '''
    dataObject = _readPlist(path)

    keys = key.split(':')
    if type(keys) is str:
        keys = list(keys)

    if dataObject is None:
        dataObject = NSMutableDictionary()

    _setObjectForKeyList(dataObject, keys, _valueToNSObject(value, nstype))
    _writePlist(dataObject, path)
def write_key(path, key, nstype, value):
    '''
    Write the value of a key contained within the Property List file specified.
    If the property list file does not exist, the default behaviour is to create it with the keys/values given.

    path
        An absolute path to a property list (.plist) file, including the extension

    key
        The path specification for the key to modify. A list of keys separated by a colon.

    nstype
        The value type to write, one of 'string', 'int', 'float', 'bool', 'data'

    value
        The property value. If not specified it will be set to an empty value.

    CLI Example:

    .. code-block:: bash

        salt '*' plist.write <path> <key> <nstype> [value]
    '''
    log.debug('Reading original plist for modification at path: %s' % path)
    dataObject = _read_plist(path)

    log.debug('Deriving key hierarchy from colon separated string')
    keys = key.split(':')
    if type(keys) is str:
        keys = list(keys)

    if dataObject is None:
        dataObject = NSMutableDictionary()

    log.debug('Performing string to NSObject conversion')
    nsval = _value_to_nsobject(value, nstype)
    log.debug('Setting object value in hierarchy')
    _set_object_for_key_list(dataObject, keys, nsval)
    log.debug('Writing out plist to original path')
    _write_plist(dataObject, path)
Beispiel #6
0
    NSURLBookmarkCreationMinimalBookmark, [], None, None)

# check if the file exists already
ls_prefs = os.path.join(
    NSHomeDirectory(),
    u'Library/Preferences/com.apple.LaunchServices/com.apple.LaunchServices')
ls_prefs_plist = ls_prefs + u'.plist'

if os.path.isfile(ls_prefs_plist):
    # read it in
    current_prefs = CFPreferencesCopyMultiple(None, ls_prefs,
                                              kCFPreferencesAnyUser,
                                              kCFPreferencesCurrentHost)
else:
    # make a new dictionary
    current_prefs = NSMutableDictionary()

# Get any existing key or a new blank dict if not present
magnified = current_prefs.get(u'LSHighResolutionModeIsMagnified',
                              NSMutableDictionary())
magnified_editable = NSMutableDictionary.dictionaryWithDictionary_(magnified)
# Build our values
options = NSMutableArray.alloc().init()
options.append(bookmark)
# A value of 3 = enabled, value of 2 = disabled
options.append(3)
magnified_editable[lowres_app_id] = options

# Update the setting
update_dict = NSMutableDictionary()
update_dict[u'LSHighResolutionModeIsMagnified'] = magnified_editable