Method: RunLoop::PlistBuddy#plist_set

Defined in:
lib/run_loop/plist_buddy.rb

#plist_set(key, type, value, file, opts = {}) ⇒ Boolean

Replaces or creates the value of key in the file.

Parameters:

  • key (String)

    the key to set (may not be nil or empty)

  • type (String)

    the plist type (used only when adding a value)

  • value (String)

    the new value

  • file (String)

    the plist to read

  • opts (Hash) (defaults to: {})

    options for controlling execution

Options Hash (opts):

  • :verbose (Boolean) — default: false

    controls log level

Returns:

  • (Boolean)

    true if the operation was successful

Raises:

  • (ArgumentError)

    if nil or empty key



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/run_loop/plist_buddy.rb', line 55

def plist_set(key, type, value, file, opts={})
  default_opts = {:verbose => false}
  merged = default_opts.merge(opts)

  if key.nil? or key.length == 0
    raise(ArgumentError, "key '#{key}' must not be nil or empty")
  end

  cmd_args = {:key => key,
              :type => type,
              :value => value}

  if plist_key_exists?(key, file, merged)
    cmd = build_plist_cmd(:set, cmd_args, file)
  else
    cmd = build_plist_cmd(:add, cmd_args, file)
  end

  success, output = execute_plist_cmd(cmd, file, merged)
  if !success
    raise RuntimeError, %Q[
Encountered an error performing operation on plist:

#{plist_buddy} -c "#{cmd}" #{file}
=> #{output}
]
  end
  success
end