Class: OsxSub::PlistBuddy

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/osxsub/plist_buddy.rb

Constant Summary collapse

PLIST_BUDDY =
'/usr/libexec/PlistBuddy'
GLOBAL_PREFERENCES_PLIST =
File.expand_path('~/Library/Preferences/.GlobalPreferences.plist')
NSUSER_REPLACEMENT_ITEMS =
'NSUserDictionaryReplacementItems'

Instance Method Summary collapse

Instance Method Details

#addObject



25
26
27
# File 'lib/osxsub/plist_buddy.rb', line 25

def add
  execute plist_buddy(:add, "array")
end

#deleteObject



21
22
23
# File 'lib/osxsub/plist_buddy.rb', line 21

def delete
  execute plist_buddy(:delete)
end

#execute(command) ⇒ Object

Raises:



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/osxsub/plist_buddy.rb', line 37

def execute(command)
  require 'stringio'
  require 'open3'

  out, err = ::StringIO.new, ::StringIO.new
  ::Open3.popen3(command) do |stdin, stdout, stderr|
    out.write stdout.read
    err.write stderr.read
  end
  raise PlistBuddyError, err.string unless err.string.empty?
  out.string
end

#merge(path) ⇒ Object



29
30
31
# File 'lib/osxsub/plist_buddy.rb', line 29

def merge(path)
  execute plist_buddy("Merge #{path}")
end

#plist_buddy(command, *extras) ⇒ Object



33
34
35
# File 'lib/osxsub/plist_buddy.rb', line 33

def plist_buddy(command, *extras)
  %Q[#{PLIST_BUDDY} -x -c "#{command.to_s.capitalize} #{NSUSER_REPLACEMENT_ITEMS} #{extras.join(' ')}" #{GLOBAL_PREFERENCES_PLIST}]
end


17
18
19
# File 'lib/osxsub/plist_buddy.rb', line 17

def print
  execute plist_buddy(:print)
end