Skip to content

Instantly share code, notes, and snippets.

@username0x0a
Created October 27, 2023 23:49
Show Gist options
  • Save username0x0a/8c7e95cea5c19b87efeda2f7cc7d271f to your computer and use it in GitHub Desktop.
Save username0x0a/8c7e95cea5c19b87efeda2f7cc7d271f to your computer and use it in GitHub Desktop.
Simple Ruby script listening to `~/Library/Preferences` & printing the changes as UserDefaults change
#!/usr/bin/env ruby
require 'diffy' # gem install diffy
require 'listen' # gem install listen
class String
def colorize(color_code)
"\e[#{color_code}m#{self}\e[0m"
end
def red; colorize(31); end
def green; colorize(32); end
def yellow; colorize(33); end
def blue; colorize(34); end
def pink; colorize(35); end
end
$files = { } # path => string
def plist_file_to_text path
`plutil -p #{path}`.strip
end
def load_files
list = `find ~/Library/Preferences -name "*.plist"`.split("\n")
list.each{|path|
$files[path] = plist_file_to_text path
}
end
def cache_file path, content
$files[path] = content
end
def process_change path
return if path.index /(ContextStoreAgent\.plist)/
old_content = $files[path]
new_content = plist_file_to_text path
cache_file path, new_content
diff = Diffy::Diff.new old_content, new_content, :diff => '-Naur'
diff = diff.to_s
.split("\n")
.filter{|line| line.start_with? /[-+]/ }
.map{|line| line.start_with?('+') ? line.green : line.red }
.join("\n")
return if diff.strip.length < 2
puts '–––––––––––––––––––––'
puts 'Path: ' + path
puts '–––––––––––––––––––––'
puts diff
puts
end
load_files
listener = Listen.to(File.expand_path('~/Library/Preferences')) do |modified, added, removed|
modified.each{|path|
process_change path
}
end
listener.start
sleep
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment