Never been to CodeSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

List & delete Safari history

# for PlistBuddy see: http://codesnippets.joyent.com/posts/show/1484

# list Safari history
function lsh() {
   /usr/libexec/PlistBuddy -c Print ~/Library/Safari/History.plist | \
      /usr/bin/awk '/^[[:space:]]+= /{sub( /^[[:space:]]+=[[:space:]]+/,""); print}' | \
      sed -E -n -e 's/^(.{1,100}).*$/\1/' -e '1!G;h;$p' | /usr/bin/nl
      #/usr/bin/sed -n '1!G;h;$p' | /usr/bin/nl
      #/usr/bin/awk '!x[$0]++' | /usr/bin/sed -n '1!G;h;$p' | /usr/bin/nl
      #/usr/bin/sort -u | /usr/bin/nl
   return 0
}

lsh


# delete Safari history except ...
# ... for those domains specified by an egrep regex

function dshe() {

   declare array_indices historyplistfile index regex

   regex="${1:-$'\777'}"   # delete entire history if there is no argument $1; for \777 see man ruby
   historyplistfile="${HOME}/Library/Safari/History.plist"

   array_indices="$(/usr/libexec/PlistBuddy -c print "${historyplistfile}" | /usr/bin/awk '/^[[:space:]]+= /{x++;print x-1,$0}' | \
       /usr/bin/egrep -iv "${regex}" | /usr/bin/awk '{print $1}' | /usr/bin/sort -rn)"

:<<-'COMMENT'
   # test
   for index in ${array_indices}; do 
      echo $index; 
      #/usr/libexec/PlistBuddy -c "Print :WebHistoryDates:'${index}'" "${historyplistfile}" 
      /usr/libexec/PlistBuddy -c "Print :WebHistoryDates:'${index}':title" "${historyplistfile}" 
   done
COMMENT

   for index in ${array_indices}; do 
      /usr/libexec/PlistBuddy -c "Delete :WebHistoryDates:'${index}'" "${historyplistfile}" 
   done

   /usr/libexec/PlistBuddy -c save "${historyplistfile}" &>/dev/null

   return 0

}


lsh
dshe unix
lsh
dshe
lsh

List & delete Safari cookies

# for PlistBuddy see: http://codesnippets.joyent.com/posts/show/1484

# list Safari cookies
function lsc() {
   /usr/libexec/PlistBuddy -c print ~/Library/Cookies/Cookies.plist | \
      /usr/bin/awk '/^[[:space:]]+Domain = /{sub( /^[[:space:]]+Domain =[[:space:]]+/,""); print}' | \
      /usr/bin/sort -u | /usr/bin/nl
      #/usr/bin/awk '!x[$0]++' | /usr/bin/sed -n '1!G;h;$p' | /usr/bin/nl
   return 0
}

lsc


# delete Safari cookies except ...
# ... for those domains specified by an egrep regex

/usr/libexec/PlistBuddy -c Print ~/Library/Cookies/Cookies.plist
/usr/libexec/PlistBuddy -c 'Print :0' ~/Library/Cookies/Cookies.plist
/usr/libexec/PlistBuddy -c 'Print :1' ~/Library/Cookies/Cookies.plist


function dsce() {

   declare array_indices cookiesplistfile index regex

   regex="${1:-$'\777'}"   # delete all cookies if there is no argument $1; for \777 see man ruby
   cookiesplistfile="${HOME}/Library/Cookies/Cookies.plist"

   array_indices="$(/usr/libexec/PlistBuddy -c print "${cookiesplistfile}" | /usr/bin/awk '/Domain = /{x++;print x-1,$0}' | \
       /usr/bin/egrep -iv "${regex}" | /usr/bin/awk '{print $1}' | /usr/bin/sort -rn)"

:<<-'COMMENT'
   # test
   for index in ${array_indices}; do 
      echo $index; 
      #/usr/libexec/PlistBuddy -c "Print :'${index}'" "${cookiesplistfile}" 
      /usr/libexec/PlistBuddy -c "Print :'${index}':Domain" "${cookiesplistfile}" 
   done
COMMENT

   for index in ${array_indices}; do 
      /usr/libexec/PlistBuddy -c "Delete :'${index}'" "${cookiesplistfile}" 
   done

   /usr/libexec/PlistBuddy -c save "${cookiesplistfile}" &>/dev/null

   return 0

}


lsc

dsce 'xxx.com|xxx.org'

lsc

app2dock

An exercise in using PlistBuddy.

dockplistfile="${HOME}/Library/Preferences/com.apple.dock.plist"

cp -ip "${dockplistfile}" "${dockplistfile}.orig"

plutil -convert xml1 -o /dev/fd/1 "${dockplistfile}" | /usr/bin/pl
plutil -convert xml1 -o ~/Desktop/com.apple.dock.plist "${dockplistfile}"
open -e ~/Desktop/com.apple.dock.plist


# get an example entry from $dockplistfile (last application in the Dock)
# cf. http://codesnippets.joyent.com/posts/show/1814

num_apps_dock=$(/usr/libexec/PlistBuddy -c "Print :persistent-apps" "${dockplistfile}" | awk -F '=' '/CFURLString = /{print $2}' | wc -l)
echo ${num_apps_dock}

/usr/libexec/PlistBuddy -c "Print :persistent-apps:'${num_apps_dock}'" "${dockplistfile}" 
#/usr/libexec/PlistBuddy -c "Print :persistent-apps:${num_apps_dock}" "${dockplistfile}" 

/usr/libexec/PlistBuddy -c "Print :persistent-apps:'${num_apps_dock}':tile-data:file-data:_CFURLString" "${dockplistfile}"


open /bin/bash


# we will first create an example entry step by step
# note: persistent-apps is an array that stores dictionaries

dockplistfile="${HOME}/Library/Preferences/com.apple.dock.plist"

# get a free array index number (that is, the last + 1 array index)
free_num_apps_dock=$(( 1 + $(/usr/libexec/PlistBuddy -c "Print :persistent-apps" "${dockplistfile}" | awk -F '=' '/CFURLString = /{print $2}' | wc -l) ))
echo ${free_num_apps_dock}

# add a dictionary to the persistent-apps array at array index ${free_num_apps_dock}
/usr/libexec/PlistBuddy -c "Add :persistent-apps:'${free_num_apps_dock}' dict" "${dockplistfile}" 

# print the dictionay in the persistent-apps array at array index ${free_num_apps_dock}
/usr/libexec/PlistBuddy -c "Print :persistent-apps:'${free_num_apps_dock}'" "${dockplistfile}" 

# add a dictionary named "tile-data" to the previously added dictionary 
/usr/libexec/PlistBuddy -c "Add :persistent-apps:'${free_num_apps_dock}':tile-data dict" "${dockplistfile}" 

/usr/libexec/PlistBuddy -c "Print :persistent-apps:'${free_num_apps_dock}'" "${dockplistfile}" 

# add the dictionary 'file-data' to the dictionary 'tile-data'
/usr/libexec/PlistBuddy -c "Add :persistent-apps:'${free_num_apps_dock}':tile-data:file-data dict" "${dockplistfile}" 

/usr/libexec/PlistBuddy -c "Print :persistent-apps:'${free_num_apps_dock}'" "${dockplistfile}" 

# add key - value pairs to the file-data dictionary
/usr/libexec/PlistBuddy -c "Add :persistent-apps:'${free_num_apps_dock}':tile-data:file-data:thisIsAKey string 'this is a string value'" "${dockplistfile}" 
/usr/libexec/PlistBuddy -c "Add :persistent-apps:'${free_num_apps_dock}':tile-data:file-data:_CFURLString string '/path/to/app'" "${dockplistfile}" 
/usr/libexec/PlistBuddy -c "Add :persistent-apps:'${free_num_apps_dock}':tile-data:file-data:_CFURLStringType integer 0" "${dockplistfile}" 

/usr/libexec/PlistBuddy -c "Print :persistent-apps:'${free_num_apps_dock}'" "${dockplistfile}" 

# delete the dictionay in the persistent-apps array at array index ${free_num_apps_dock}
/usr/libexec/PlistBuddy -c "Delete :persistent-apps:'${free_num_apps_dock}'" "${dockplistfile}" 

# print all dictionaries of the persistent-apps array
/usr/libexec/PlistBuddy -c "Print :persistent-apps" "${dockplistfile}" 


# short version
dockplistfile="${HOME}/Library/Preferences/com.apple.dock.plist"
free_num_apps_dock=$(( 1 + $(/usr/libexec/PlistBuddy -c "Print :persistent-apps" "${dockplistfile}" | awk -F '=' '/CFURLString = /{print $2}' | wc -l) ))
echo ${free_num_apps_dock}
/usr/libexec/PlistBuddy -c "Add :persistent-apps:'${free_num_apps_dock}':tile-data:file-data:thisIsAKey string 'this is a string value'" "${dockplistfile}" 
/usr/libexec/PlistBuddy -c "Add :persistent-apps:'${free_num_apps_dock}':tile-data:file-data:_CFURLString string '/path/to/app'" "${dockplistfile}" 
/usr/libexec/PlistBuddy -c "Add :persistent-apps:'${free_num_apps_dock}':tile-data:file-data:_CFURLStringType integer 0" "${dockplistfile}" 
/usr/libexec/PlistBuddy -c "Print :persistent-apps:'${free_num_apps_dock}'" "${dockplistfile}" 
/usr/libexec/PlistBuddy -c "Delete :persistent-apps:'${free_num_apps_dock}'" "${dockplistfile}" 
/usr/libexec/PlistBuddy -c "Print :persistent-apps" "${dockplistfile}" 


:<<-'COMMENT'
# example entry
Dict {
    tile-data = Dict {
        file-data = Dict {
            thisIsAKey = this is a string value
            _CFURLString = /path/to/app
            _CFURLStringType = 0
        }
    }
}

COMMENT



#-----------------------------------------------------------------------------



# app2dock
# allows case insensitive name of application as input
# uses lsregister & egrep -i

# lsregister
/usr/bin/sudo /bin/ln -is $(/usr/bin/locate *lsregister | /usr/bin/head -n 1) /bin/lsregister 

function app2dock() {
   declare appname dockplistfile free_num_apps_dock path
   dockplistfile="${HOME}/Library/Preferences/com.apple.dock.plist"

   for appname in "${@}"; do
      appname="${appname%/}"
      path="$(/bin/lsregister -dump | /usr/bin/egrep -i -m 1 "path: +.*\/[^\/]*${appname}[^\/]*\.app$" | \
                /usr/bin/sed -E 's/^[[:space:]]+path:[[:space:]]+//')"
      if [[ -z "${path}" ]]; then echo "No such application: ${appname}"; continue; fi
      path="${path%/}"
      if [[ ! -d "${path}" ]] || [[ "${path}" == "${path%.app}" ]]; then echo "Argument error: ${path}"; continue; fi
      free_num_apps_dock=$(( 1 + $(/usr/libexec/PlistBuddy -c "Print :persistent-apps" "${dockplistfile}" | \
             /usr/bin/awk -F '=' '/CFURLString = /{print $2}' | /usr/bin/wc -l) ))
      #echo ${free_num_apps_dock}
      /usr/libexec/PlistBuddy -c "Add :persistent-apps:'${free_num_apps_dock}':tile-data:file-data:_CFURLString string '${path}'" "${dockplistfile}" 
      /usr/libexec/PlistBuddy -c "Add :persistent-apps:'${free_num_apps_dock}':tile-data:file-data:_CFURLStringType integer 0" "${dockplistfile}" 
      #/usr/libexec/PlistBuddy -c "Print :persistent-apps:'${free_num_apps_dock}'" "${dockplistfile}" 
      /usr/libexec/PlistBuddy -c save "${dockplistfile}" &>/dev/null
   done

   /usr/bin/killall -HUP Dock
   return 0
}



# app2dock_fp
# only allows full file path to application as input

function app2dock_fp() {
   declare dockplistfile free_num_apps_dock path
   dockplistfile="${HOME}/Library/Preferences/com.apple.dock.plist"

   for path in "${@}"; do
      path="${path%/}"
      if [[ ! -d "${path}" ]] || [[ "${path}" == "${path%.app}" ]]; then echo "Argument error: ${path}"; continue; fi
      free_num_apps_dock=$(( 1 + $(/usr/libexec/PlistBuddy -c "Print :persistent-apps" "${dockplistfile}" | \
             /usr/bin/awk -F '=' '/CFURLString = /{print $2}' | /usr/bin/wc -l) ))
      #echo ${free_num_apps_dock}
      /usr/libexec/PlistBuddy -c "Add :persistent-apps:'${free_num_apps_dock}':tile-data:file-data:_CFURLString string '${path}'" "${dockplistfile}" 
      /usr/libexec/PlistBuddy -c "Add :persistent-apps:'${free_num_apps_dock}':tile-data:file-data:_CFURLStringType integer 0" "${dockplistfile}" 
      #/usr/libexec/PlistBuddy -c "Print :persistent-apps:'${free_num_apps_dock}'" "${dockplistfile}" 
      /usr/libexec/PlistBuddy -c save "${dockplistfile}" &>/dev/null

   done

   /usr/bin/killall -HUP Dock
   return 0
}



# remove_app_from_dock
# allows case insensitive name of application as input

function remove_app_from_dock() {

   declare appname dockplistfile nums=""

   dockplistfile="${HOME}/Library/Preferences/com.apple.dock.plist"

   for appname in "${@}"; do

      appname="${appname%/}"

      nums=""
      nums=$(/usr/libexec/PlistBuddy -c "Print :persistent-apps" "${dockplistfile}" | /usr/bin/awk -F '=' '/CFURLString = /{print $2}' | \
            /usr/bin/nl | /usr/bin/egrep -i "${appname}" | /usr/bin/awk '{print $1}' | /usr/bin/sort -rn)

      if [[ -z "${nums}" ]]; then echo "No application: ${appname} found in the Dock."; continue; fi

      for num in ${nums}; do
         /usr/libexec/PlistBuddy -c "Delete :persistent-apps:'${num}'" "${dockplistfile}" 
      done

      # test
      #for num in ${nums}; do
      #   /usr/libexec/PlistBuddy -c "Print :persistent-apps:'${num}'" "${dockplistfile}" 
      #done

      /usr/libexec/PlistBuddy -c save "${dockplistfile}" &>/dev/null

   done

   /usr/bin/killall -HUP Dock

   return 0

}


app2dock_fp /Applications/Chess.app /Applications/Chess.app

app2dock chess grapher

remove_app_from_dock chess grapher

Listing Dock items with PlistBuddy


# for PlistBuddy see: http://codesnippets.joyent.com/posts/show/1484
# for updating Bash to get in-process regular expression matching see: http://codesnippets.joyent.com/posts/show/1714

# for a command line Dock management tool see: 
# dockutil, http://patternbuffer.wordpress.com/category/dockutil/


# the awk solution should work except when a quotation mark (") is part of a file path, etc.

# applications
defaults read com.apple.dock persistent-apps | awk -F '"' '/CFURLString" = [^[:digit:]]/{print $4}' | nl

# files, folders, URLs
defaults read com.apple.dock persistent-others | awk -F '"' '/CFURLString" = [^[:digit:]]/{print $4}' | nl


# alternative with PlistBuddy & awk
dockplistfile="${HOME}/Library/Preferences/com.apple.dock.plist"
/usr/libexec/PlistBuddy -c "Print persistent-apps" "${dockplistfile}" | awk '/CFURLString = /{print $0}' | nl
/usr/libexec/PlistBuddy -c "Print persistent-apps" "${dockplistfile}" | awk -F '=' '/CFURLString = /{print $2}' | nl
/usr/libexec/PlistBuddy -c "Print persistent-others" "${dockplistfile}" | awk -F '=' '/CFURLString = /{print $2}' | nl


# applications

count=1
dockplistfile="${HOME}/Library/Preferences/com.apple.dock.plist"

# list first application
/usr/libexec/PlistBuddy -c "Print :persistent-apps:1:tile-data:file-data:_CFURLString" "${dockplistfile}"

until [[ "$(/usr/libexec/PlistBuddy -c "Print :persistent-apps:${count}" "${dockplistfile}")" =~ 'Does Not Exist' ]]; do
   /usr/libexec/PlistBuddy -c "Print :persistent-apps:${count}:tile-data:file-data:_CFURLString" "${dockplistfile}"
   let count++
done | /usr/bin/egrep -v 'Does Not Exist'


apps="$(
count=1
dockplistfile="${HOME}/Library/Preferences/com.apple.dock.plist"
until [[ "$(/usr/libexec/PlistBuddy -c "Print :persistent-apps:${count}" "${dockplistfile}")" =~ 'Does Not Exist' ]]; do
   /usr/libexec/PlistBuddy -c "Print :persistent-apps:${count}:tile-data:file-data:_CFURLString" "${dockplistfile}"
   let count++
done | /usr/bin/egrep -v 'Does Not Exist'
)"

printf "%s\n" "${#apps[@]}"
printf "%s\n" "${apps[@]}" | sed 's|/$||'



# files, folders, URLs

count=1
dockplistfile="${HOME}/Library/Preferences/com.apple.dock.plist"

until [[ "$(/usr/libexec/PlistBuddy -c "Print :persistent-others:${count}" "${dockplistfile}" 2>/dev/null)" =~ 'Does Not Exist' ]]; do
   /usr/libexec/PlistBuddy -c "Print :persistent-others:${count}:tile-data:file-data:_CFURLString" "${dockplistfile}" 2>/dev/null
   /usr/libexec/PlistBuddy -c "Print :persistent-others:${count}:tile-data:url:_CFURLString" "${dockplistfile}" 2>/dev/null
   let count++
done | /usr/bin/egrep -v 'Does Not Exist'



docs="$(
count=1
dockplistfile="${HOME}/Library/Preferences/com.apple.dock.plist"
until [[ "$(/usr/libexec/PlistBuddy -c "Print :persistent-others:${count}" "${dockplistfile}" 2>/dev/null)" =~ 'Does Not Exist' ]]; do
   /usr/libexec/PlistBuddy -c "Print :persistent-others:${count}:tile-data:file-data:_CFURLString" "${dockplistfile}" 2>/dev/null
   /usr/libexec/PlistBuddy -c "Print :persistent-others:${count}:tile-data:url:_CFURLString" "${dockplistfile}" 2>/dev/null
   let count++
done | /usr/bin/egrep -v 'Does Not Exist'
)"


printf "%s\n" "${#docs[@]}"
printf "%s\n" "${docs[@]}" | sed 's|/$||'

Using PlistBuddy to customize syslogd


# Inspired by: http://www.maciverse.com/the-case-of-the-slow-mac-and-how-to-fix-it.html

# cf. man syslog, man syslogd and http://developer.apple.com/documentation/Darwin/Reference/ManPages/man8/syslogd.8.html
/usr/bin/open -a Console   # system.log
/usr/bin/tail -n 50 /private/var/log/system.log

help declare
declare -x sudo=/usr/bin/sudo plistbuddy=/usr/libexec/PlistBuddy defaults=/usr/bin/defaults
/usr/bin/env | /usr/bin/grep -E 'sudo=|plistbuddy=|defaults='

# create a symbolic link (on Mac OS X 10.4)
help test
/usr/bin/locate PlistBuddy
if [[ ! -e /usr/libexec/PlistBuddy ]]; then $sudo /bin/ln -s "/Library/Receipts/AdditionalEssentials.pkg/Contents/Resources/PlistBuddy" /usr/libexec/PlistBuddy; fi

# make a backup
$sudo /bin/cp -p /System/Library/LaunchDaemons/com.apple.syslogd.plist /System/Library/LaunchDaemons/com.apple.syslogd.plist.orig

# cf. http://developer.apple.com/documentation/Darwin/Reference/ManPages/man8/PlistBuddy.8.html
$plistbuddy -h   

$plistbuddy -c Print /System/Library/LaunchDaemons/com.apple.syslogd.plist
$plistbuddy -c "Print :ProgramArguments" /System/Library/LaunchDaemons/com.apple.syslogd.plist

# compare
$defaults read /System/Library/LaunchDaemons/com.apple.syslogd
$defaults read /System/Library/LaunchDaemons/com.apple.syslogd ProgramArguments

$sudo /usr/bin/nano /System/Library/LaunchDaemons/com.apple.syslogd.plist

$sudo $plistbuddy -c "Delete :ProgramArguments" /System/Library/LaunchDaemons/com.apple.syslogd.plist
$sudo $plistbuddy -c "Add :ProgramArguments array" /System/Library/LaunchDaemons/com.apple.syslogd.plist
$sudo $plistbuddy -c "Add :ProgramArguments:0 string '/usr/sbin/syslogd'" /System/Library/LaunchDaemons/com.apple.syslogd.plist
$sudo $plistbuddy -c "Add :ProgramArguments:1 string '-c'" /System/Library/LaunchDaemons/com.apple.syslogd.plist
$sudo $plistbuddy -c "Add :ProgramArguments:2 integer 3" /System/Library/LaunchDaemons/com.apple.syslogd.plist

# Mac OS X 10.5
#$sudo $plistbuddy -c "Add :ProgramArguments:3 string '-a'" /System/Library/LaunchDaemons/com.apple.syslogd.plist
#$sudo $plistbuddy -c "Add :ProgramArguments:4 string '-db_max'" /System/Library/LaunchDaemons/com.apple.syslogd.plist
#$sudo $plistbuddy -c "Add :ProgramArguments:5 integer 5000000" /System/Library/LaunchDaemons/com.apple.syslogd.plist

$plistbuddy -c "Print :ProgramArguments" /System/Library/LaunchDaemons/com.apple.syslogd.plist
$defaults read /System/Library/LaunchDaemons/com.apple.syslogd ProgramArguments
$sudo /usr/bin/nano /System/Library/LaunchDaemons/com.apple.syslogd.plist

# reset com.apple.syslogd.plist
#if [[ -n $(/usr/bin/sw_vers -productVersion | /usr/bin/grep '^10.5') ]]; then exit; fi   # exit if on Mac OS X 10.5.x
$sudo /bin/rm -f /System/Library/LaunchDaemons/com.apple.syslogd.plist
$sudo /bin/cp -p /System/Library/LaunchDaemons/com.apple.syslogd.plist.orig /System/Library/LaunchDaemons/com.apple.syslogd.plist

$sudo /usr/bin/nano /System/Library/LaunchDaemons/com.apple.syslogd.plist

# restart syslogd
#$sudo /bin/launchctl unload -w /System/Library/LaunchDaemons/com.apple.syslogd.plist 2>/dev/null
#/bin/sleep 3
#$sudo /bin/launchctl load -w /System/Library/LaunchDaemons/com.apple.syslogd.plist 2>/dev/null
#$sudo /usr/bin/killall -HUP syslogd   # alternative