Skip to content

Instantly share code, notes, and snippets.

@rockpapergoat
Created June 29, 2012 19:57
Show Gist options
  • Save rockpapergoat/3020305 to your computer and use it in GitHub Desktop.
Save rockpapergoat/3020305 to your computer and use it in GitHub Desktop.
simulate casper's "fill each user" function
#!/usr/bin/env ruby
require 'FileUtils'
require 'pathname'
def get_homedir(user)
begin
Etc.getpwnam("#{user}")["dir"].chomp
rescue Exception => e
end
end
def get_all_over500_users
user_hash = {}
`dscl . -list /users UniqueID | sort -k2 -n`.split("\n").each do |pair|
pair.split(" ").collect! { user_hash[pair.split(" ")[0]] = pair.split(" ")[1].to_i}
end
user_hash.delete_if {|k,v| v.to_i < 500 }
# can also do it like this, but it reports all directory entries, not just local
# Etc.passwd { |d| user_hash[d.name] = d.uid if d.uid > 500 }
end
def stash_user_file(file,path)
users = get_all_over500_users
users.each_key do |u|
puts "copying files to #{u}\'s home at #{path}."
system "ditto -V #{file} #{get_homedir(u)}/#{path}/#{File.basename(file)}"
FileUtils.chown_R("#{u}", nil, "#{get_homedir(u)}/#{path}/#{File.basename(file)}")
end
end
stash_user_file("/Library/Preferences/blah","/Library/Preferences")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment