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!)

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

List the names & version numbers of applications

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


# first rebuild the Launch Services database
lsregister -kill -r -f -all system,local,user     # Mac OS X 10.5
lsregister -kill -r -f -domain local -domain system -domain user


# print the lines between :path ... version: ...
time -p lsregister -dump | sed -E -n -e '/^.+path: .+\.app$/{N;N;N;p;}' | nl
time -p lsregister -dump | sed -E -n -e '/\.app$/{N;N;N;p;}' | nl

time -p lsregister -dump | sed -E -n -e '/path: .+\.app$/,/version:/p' | nl
time -p lsregister -dump | sed -E -n -e '/\.app$/,/version:/p' | nl

time -p lsregister -dump | egrep -A 3 -i "path: +\/.+\\.app$" | nl


# list application paths with lsregister
time -p lsregister -dump | sed -E -n -e '/path: .+\.app$/,/version:/s/^.+path: +(.+\.app)$/\1/p' | nl
time -p lsregister -dump | sed -E -n -e '/\.app$/,/version:/s/^.+path: +(.+\.app)$/\1/p' | nl

# list application names with lsregister
time -p lsregister -dump | sed -E -n -e '/path: .+\.app$/,/version:/s/^.+path: +\/?.*\/([^\/]+\.app)$/\1/p' | nl
time -p lsregister -dump | sed -E -n -e '/\.app$/,/version:/s/^.+path: +\/?.*\/([^\/]+\.app)$/\1/p' | nl

# list version numbers of applications with lsregister
time -p lsregister -dump | sed -E -n -e '/path: +.+\.app$/,/version/s/^.+version: +(.*)$/\1/p' | nl
time -p lsregister -dump | sed -E -n -e '/\.app$/,/version/s/^.+version: +(.*)$/\1/p' | nl


# list the names & version numbers of applications based on lsregister

function lsvers() {

   if [[ "$1" == '-fp' ]]; then     # full path option

      while read -d $'\n' file; do

         path="${file% -*}"
         lsregister_version="${file##*- }"

         mdls_version="$(/usr/bin/mdls -name kMDItemVersion "${path}" 2>/dev/null | \
            /usr/bin/awk -F '"' '/kMDItemVersion/ {print $2}' 2>/dev/null)"
         #mdls_version="$(/usr/bin/mdls -name kMDItemVersion "${path}" 2>/dev/null | \
         #   /usr/bin/awk -F '"' 'END {print $2}' 2>/dev/null)"

         if [[ -n "$mdls_version" ]]; then
            printf "%s\n" "${path}   --   ${mdls_version}"
         else
            printf "%s\n" "${path}   --   ${lsregister_version}"
         fi

done < <(
/bin/lsregister -dump | /usr/bin/egrep -A 3 '^[[:space:]]+path:[[:space:]]+([^[:space:]].*\.app)$' | \
/usr/bin/sed -E -n -e 's/^[[:space:]]+version:[[:space:]]+(.*)$/\1/p' -e 's/^[[:space:]]+path:[[:space:]]+(\/.*\.app)$/\1/p' | \
/usr/bin/sed 's/ /\\ /g' | /usr/bin/xargs -n 2 printf "%s - %s\n"
)

   else

      while read -d $'\n' file; do

         path="${file% -*}"
         bname="$(/usr/bin/basename "${path}")"
         lsregister_version="${file##*- }"

         mdls_version="$(/usr/bin/mdls -name kMDItemVersion "${path}" 2>/dev/null | \
            /usr/bin/awk -F '"' '/kMDItemVersion/ {print $2}' 2>/dev/null)"

         if [[ -n "$mdls_version" ]]; then
            printf "%s\n" "${bname}   --   ${mdls_version}"
         else
            printf "%s\n" "${bname}   --   ${lsregister_version}"
         fi

done < <(
/bin/lsregister -dump | /usr/bin/egrep -A 3 '^[[:space:]]+path:[[:space:]]+([^[:space:]].*\.app)$' | \
/usr/bin/sed -E -n -e 's/^[[:space:]]+version:[[:space:]]+(.*)$/\1/p' -e 's/^[[:space:]]+path:[[:space:]]+(\/.*\.app)$/\1/p' | \
/usr/bin/sed 's/ /\\ /g' | /usr/bin/xargs -n 2 printf "%s - %s\n"
)

   fi


: <<-'COMMENT'

time -p /bin/lsregister -dump | /usr/bin/egrep -A 3 '^[[:space:]]+path:[[:space:]]+([^[:space:]].*\.app)$' | \
/usr/bin/sed -E -n -e 's/^[[:space:]]+version:[[:space:]]+(.*)$/\1/p' -e 's/^[[:space:]]+path:[[:space:]]+(\/.*\.app)$/\1/p' | \
/usr/bin/sed 's/ /\\ /g' | /usr/bin/xargs -n 2 printf "%s - %s\n" | nl

# alternatives with sed pattern matching across several lines (here matching the lines containing: path: ... :version ...)

time -p /bin/lsregister -dump | /usr/bin/sed -E -n -e '/path:.+\.app$/,/version:/s/^.+path: +(\/.+\.app)$|^.+version: +(.+)$/\1\2/p' | \
/usr/bin/sed 's/ /\\ /g' | /usr/bin/xargs -n 2 printf "%s - %s\n" | nl

time -p /bin/lsregister -dump | /usr/bin/sed -E -n -e '/path:.+\.app$/,/version:/s/path: +(\/.+\.app)$|version: +(.+)$/\1\2/p' | \
/usr/bin/sed 's/ /\\ /g' | /usr/bin/xargs -n 2 printf "%s - %s\n" | nl

time -p /bin/lsregister -dump | /usr/bin/sed -E -n -e '/\.app$/,/version:/s/path: +(\/.+\.app)$|version: +(.+)$/\1\2/p' | \
/usr/bin/sed 's/ /\\ /g' | /usr/bin/xargs -n 2 printf "%s - %s\n" | nl

COMMENT


return 0

}


lsvers | nl
lsvers | egrep -i system | nl
lsvers -fp | egrep -i system | nl     # print full paths to applications
lsvers -fp | egrep -i '\/[^\/]*system[^\/]*$' | nl     # print full paths to applications


# check
app="VerifiedDownloadAgent.app"
app="Crash Reporter.app"
app="SyncServer.app"
app="SecurityAgent.app"
app="SystemUIServer.app"

lsregister -dump | egrep -A 3 "${app}$"

path="$(lsregister -dump | grep -A 3 "${app}$" | sed -E -n -e 's/^[[:space:]]+path:[[:space:]]+(\/.*\.app)$/\1/p')"
echo "$path"
mdls -name kMDItemVersion "$path"


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


# list the names and version numbers of specified applications using lsregister
function appversion() {

/bin/lsregister -dump | /usr/bin/egrep -i -A 3 "^[[:space:]]+path:[[:space:]]+\/?.*\/([^\/]*${@}[^\/]*\\.app)$" | \
/usr/bin/sed -E -n -e 's/^[[:space:]]+version:[[:space:]]+(.*)$/\1/p' -e 's/^[[:space:]]+path:[[:space:]]+\/.*\/([^\/]+\.app)$/\1/p' | \
/usr/bin/sed 's/ /\\ /g' | /usr/bin/xargs -n 2 printf "%s   --   %s\n"


: <<-'COMMENT'

set -- System
echo "${@}"

time -p /bin/lsregister -dump | /usr/bin/egrep -i -A 3 "^[[:space:]]+path:[[:space:]]+\/?.*\/[^\/]*${@}[^\/]*\\.app$" | \
/usr/bin/sed -E -n -e 's/^[[:space:]]+version:[[:space:]]+(.*)$/\1/p' -e 's/^[[:space:]]+path:[[:space:]]+\/.*\/([^\/]+\.app)$/\1/p' | \
/usr/bin/sed 's/ /\\ /g' | /usr/bin/xargs -n 2 printf "%s - %s\n" | nl

# alternatives
time -p /bin/lsregister -dump | /usr/bin/egrep -A 3 -i "path: +\/?.*\/[^\/]*${@}[^\/]*\\.app$" | /usr/bin/sed -E -n \
-e 's/version: +(.*)$/\1/p' -e 's/path: +\/?.*\/([^\/]+\.app)$/\1/p' | \
/usr/bin/sed 's/ /\\ /g' | /usr/bin/xargs -n 2 printf "%s - %s\n" | nl

time -p /bin/lsregister -dump | /usr/bin/egrep -A 3 -i " +\/?.*\/[^\/]*${@}[^\/]*\\.app$" | /usr/bin/sed -E -n \
-e 's/version: +(.*)$/\1/p' -e 's/path: +\/?.*\/([^\/]+\.app)$/\1/p' | \
/usr/bin/sed 's/ /\\ /g' | /usr/bin/xargs -n 2 printf "%s - %s\n" | nl


# alternative with sed pattern matching across several lines
# here: matching the lines between: path: ... :version ...

time -p /bin/lsregister -dump | /usr/bin/egrep -A 3 -i "path: +\/?.*\/[^\/]*${@}[^\/]*\\.app$" | /usr/bin/sed -E -n \
-e '/\.app$/,/version:/s/path: +\/?.*\/([^\/]+\.app)$|version: +(.+)$/\1\2/p' | \
/usr/bin/sed 's/ /\\ /g' | /usr/bin/xargs -n 2 printf "%s - %s\n" | nl

COMMENT

return 0
}


appversion mail
appversion finder
appversion safari

appversion system
appversion uiserver
appversion server
appversion window


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


# list names & mdls version numbers of applications (based on mdfind & com.apple.application-bundle)
function lsmdvers() {

   if [[ "$1" == '-fp' ]]; then     # full path option

      while read -d $'\000' file; do

         mdls_version="$(/usr/bin/mdls -name kMDItemVersion "${file}" 2>/dev/null | \
            /usr/bin/awk -F '"' '/kMDItemVersion/ {print $2}' 2>/dev/null)"

         if [[ -n "$mdls_version" ]]; then
            printf "%s\n" "${file}   --   ${mdls_version}"
         else
            printf "%s\x21\n" "${file}   --   No mdls version number specified"
         fi

      done < <(/usr/bin/mdfind -0 'kMDItemContentTypeTree == "com.apple.application-bundle"wc')

   else

      while read -d $'\000' file; do

         bname="$(/usr/bin/basename "${file}")"

         mdls_version="$(/usr/bin/mdls -name kMDItemVersion "${file}" 2>/dev/null | \
            /usr/bin/awk -F '"' '/kMDItemVersion/ {print $2}' 2>/dev/null)"

         if [[ -n "$mdls_version" ]]; then
            printf "%s\n" "${bname}   --   ${mdls_version}"
         else
            printf "%s\x21\n" "${bname}   --   No mdls version number specified"
         fi

      done < <(/usr/bin/mdfind -0 'kMDItemContentTypeTree == "com.apple.application-bundle"wc')

   fi

   return 0

}


lsmdvers | nl
lsmdvers -fp | nl    # print full paths to applications

lsmdvers | grep -i system | nl && echo && lsvers | grep -i system | nl



# list the names and version numbers of specified applications using mdfind & com.apple.application-bundle
function appmdversion() {

   if [[ "$1" == '-fp' ]]; then     # full path option

      while read -d $'\000' file; do

         mdls_version="$(/usr/bin/mdls -name kMDItemVersion "${file}" 2>/dev/null | \
            /usr/bin/awk -F '"' '/kMDItemVersion/ {print $2}' 2>/dev/null)"

         if [[ -n "$mdls_version" ]]; then
            printf "%s\n" "${file}   --   ${mdls_version}"
         else
            printf "%s\x21\n" "${file}   --   No mdls version number specified"
         fi

      done < <(/usr/bin/mdfind -0 "kMDItemContentTypeTree == 'com.apple.application-bundle'wc && kMDItemDisplayName == '*${2:-*}*'wc")

   else

      while read -d $'\000' file; do

         bname="$(/usr/bin/basename "${file}")"

         mdls_version="$(/usr/bin/mdls -name kMDItemVersion "${file}" 2>/dev/null | \
            /usr/bin/awk -F '"' '/kMDItemVersion/ {print $2}' 2>/dev/null)"

         if [[ -n "$mdls_version" ]]; then
            printf "%s\n" "${bname}   --   ${mdls_version}"
         else
            printf "%s\x21\n" "${bname}   --   No mdls version number specified"
         fi

      done < <(/usr/bin/mdfind -0 "kMDItemContentTypeTree == 'com.apple.application-bundle'wc && kMDItemDisplayName == '*${@:-*}*'wc")

   fi

   return 0

}


appmdversion play | nl
appmdversion -fp play | nl   # print full paths to applications

appversion play && echo && appmdversion play


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


# experimental

function cmdversion() {

declare cmd last_modified vers

while [ $# -gt 0 ]; do

   cmd="$(/usr/bin/which ${1})"   # get the full cmd path

   # cmd was not found in $PATH
   if [[ -n "$(echo "$cmd" | /usr/bin/egrep "^no +${1} +in ")" ]]; then echo; echo "${cmd}"; echo; shift; continue; fi
   #if [[ -n "$(echo "$cmd" | /usr/bin/egrep '^no +[^[:space:]]+ +in ')" ]]; then echo; echo "${cmd}"; echo; shift; continue; fi


   # handle special cases such as ls, echo, ...
   if [[ "${1##*/}" == "ls" ]] || [[ "${1##*/}" == "echo" ]] || [[ "${1##*/}" == "getopt" ]]; then
      vers="$(/usr/bin/egrep -ao '(.{0,10}[Vv]ersion:? +"?[\.\_[:digit:]]+"?.{0,15}|.{0,10}[Vv]\.? +"?[\.\_[:digit:]]+"?.{0,15})' "${cmd}")"
      last_modified="$(/usr/bin/stat -f $'last modified:   %Sm\n' "${cmd}")"
      #printf "\e[1m%s\e[m (guess):\n%s\n" "${cmd}" "${vers}"
      printf "\e[1m%s\e[m (guess):\n%s\n%s\n" "${cmd}" "${vers}" "${last_modified}"
      shift
      continue
   fi

   if [[ "${1##*/}" == "python" ]]; then
      vers="$(${cmd} -V 2>&1)"
      printf "\e[1m%s\e[m:\n%s\n" "${cmd}" "${vers}"
      shift
      continue
   fi

   # cases that require sudo
   if [[ "${1##*/}" == "fibreconfig" ]]; then
      vers="$(/usr/bin/sudo ${cmd} --version 2>&1)"
      printf "\e[1m%s\e[m:\n%s\n" "${cmd}" "${vers}"
      shift
      continue
   fi


   if [[ -n "$(${cmd} --version 2>/dev/null)" ]]; then
      vers="$(${cmd} --version)"
      printf "\e[1m%s\e[m:\n%s\n" "${cmd}" "${vers}"

   elif [[ -n "$(${cmd} -version 2>/dev/null)" ]]; then
      vers="$(${cmd} -version)"
      printf "\e[1m%s\e[m:\n%s\n" "${cmd}" "${vers}"

   elif [[ -n "$(${cmd} --version 2>&1 | /usr/bin/egrep -ao '([Vv]ersion:? *"?[\.\_[:digit:]]+"?|[Vv]\.? +"?[\.\_[:digit:]]+"?)')" ]]; then
      vers="$(${cmd} --version 2>&1)"
      printf "\e[1m%s\e[m:\n%s\n" "${cmd}" "${vers}"

   elif [[ -n "$(${cmd} -version 2>&1 | /usr/bin/egrep -ao '([Vv]ersion:? *"?[\.\_[:digit:]]+"?|[Vv]\.? +"?[\.\_[:digit:]]+"?)')" ]]; then
      vers="$(${cmd} -version 2>&1)"
      printf "\e[1m%s\e[m:\n%s\n" "${cmd}" "${vers}"

   elif [[ -n "$(/usr/bin/egrep -ao '([Vv]ersion:? +"?[\.\_[:digit:]]+"?|[Vv]\.? +"?[\.\_[:digit:]]+"?)' "${cmd}" 2>/dev/null) 2>/dev/null)" ]]; then
      vers="$(/usr/bin/egrep -ao '(.{0,10}[Vv]ersion:? +"?[\.\_[:digit:]]+"?.{0,15}|.{0,10}[Vv]\.? +"?[\.\_[:digit:]]+"?.{0,15})' "${cmd}")"
      #vers="$(/usr/bin/egrep -ao '(.{0,30}[Vv]ersion:? +"?[\.\_[:digit:]]+"?.{0,30}|.{0,30}[Vv]\.? +"?[\.\_[:digit:]]+"?.{0,30})' "${cmd}")"
      #vers="$(/usr/bin/egrep -ao '([Vv]ersion:? +"?[\.\_[:digit:]]+"?|[Vv]\.? +"?[\.\_[:digit:]]+"?)' "${cmd}")"

      last_modified="$(/usr/bin/stat -f $'last modified:   %Sm\n' "${cmd}")"

      if [[ -z "${vers}" ]]; then 
         printf "\e[1m%s\e[m:\n%s\n" "${cmd}" "${last_modified}"
         shift
         continue
      fi

      #printf "\e[1m%s\e[m (guess):\n%s\n" "${cmd}" "${vers}"
      printf "\e[1m%s\e[m (guess):\n%s\n%s\n" "${cmd}" "${vers}" "${last_modified}"

   fi

   shift

done

return 0
}


cmdversion bash
cmdversion /bin/bash

cmdversion sh java sed ls echo printf tr

cmdversion rm srm rmdir unlink kill killall

cmdversion read stat chown w tcl tk getopt getopts symlink ln locate

cmdversion chmod cp dd ed ssh

cmdversion bzcat openssl cat open alias uuidgen bc apropos man perl python ruby

cmdversion /sbin/fibreconfig


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


# list the version numbers of dynamically loaded kernel extensions
man kextstat   
/usr/sbin/kextstat | /usr/bin/sed -E -n -e 's/^([[:space:]]+[^[:space:]]+){5}[[:space:]]+([^[:space:]]+)[[:space:]]+\(([^[:space:]]+)\).*$/\2  --  \3/p'

Changing the Finder "Open With" contextual menu from the command line

The command line tool duti (http://duti.sourceforge.net) can be used to change default file-application launching associations as shown by the Finder "Open With" contextual menu. Changing the Finder "Open With" contextual menu means changing the default way we "open" documents on the command line as well.

The "lsregister" command can be used to dump or rebuild the Launch Services database.

export PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin
export IFS=$' \t\n'


# download & install duti, MacVim and TextMate

# duti - set default UTI handlers
# cf. http://duti.sourceforge.net/documentation.php and
# http://developer.apple.com/macosx/uniformtypeidentifiers.html

cd ~/Desktop
curl -L -O http://downloads.sourceforge.net/duti/duti-1.3.0.pkg.tar.gz
tar -xzf duti-1.3.0.pkg.tar.gz
open -a Installer duti-1.3.0.pkg
stat -x /usr/local/bin/duti
duti -h
man duti


# MacVim, http://code.google.com/p/macvim/

cd ~/Desktop
curl -L -O http://macvim.googlecode.com/files/MacVim-7_2-stable-1_2.tbz
tar -xjf MacVim-7_2-stable-1_2.tbz
#open MacVim-7_2-stable-1_2.tbz    # uses BOMArchiveHelper (10.4) or Archive Utility (10.5) respectively
mv -i MacVim-7_2-stable-1_2 MacVim
mv -i ~/Desktop/MacVim /Applications


# TextMate, http://macromates.com

cd ~/Desktop
curl -L -O http://macromates.com/textmate/files/TextMate_1.5.7.dmg
hdiutil mount ~/Desktop/TextMate_1.5.7.dmg
cp -Ri '/Volumes/TextMate 1.5.7/TextMate.app' /Applications
hdiutil unmount '/Volumes/TextMate 1.5.7'
#/usr/bin/sudo /bin/ln -s /Applications/TextMate.app/Contents/Resources/mate /usr/local/bin/mate


# get the path to the "lsregister" command
locate lsregister | head -n 1
find /System/Library/Frameworks -type f -name lsregister -ls
find /System/Library/Frameworks/CoreServices.framework -type f -name lsregister -ls

alias lsregister="/System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/LaunchServices.framework/Versions/A/Support/lsregister"

#/usr/bin/sudo /bin/ln -s `locate lsregister | head -n 1` /bin/lsregister   # alternative

# rebuild Launch Services database
#alias rlsdb='lsregister -kill -r -f -all system,local,user'    # Mac OS X 10.5; cf. http://9stmaryrd.com/2008/05/16/158
alias rlsdb='lsregister -kill -r -f -domain local -domain system -domain user'

rlsdb

lsregister -h

lsregister -dump | grep -i duti
lsregister -dump | grep -i MacVim
lsregister -dump | grep -i TextMate
lsregister -dump | grep -i TextEdit
lsregister -dump | grep -i Safari
lsregister -dump | grep -i helpviewer


lsregister -dump | sed -E -n -e 's/^.*uti:[[:space:]]+(.*)$/  \1/p' | sort | uniq | nl
lsregister -dump | grep '[[:space:]]uti:' | awk '{ print $2 }' | sort | uniq | nl
lsregister -dump | sed -E -n -e 's/^.*identifier:[[:space:]]+([^[:space:]].*)$/  \1/p' | sort | uniq | nl
lsregister -dump | sed -E -n -e 's/^.*path:[[:space:]]+([^[:space:]].*\.app)$/  \1/p' | sort | uniq | nl


lsregister -dump | sed -E -n -e 's/^.*uti:[[:space:]]+(.*)$/  \1/p' | nl | egrep -i 'duti|MacVim|TextMate|TextEdit|Safari|helpviewer'
lsregister -dump | sed -E -n -e 's/^.*identifier:[[:space:]]+([^[:space:]].*)$/  \1/p' | nl | egrep -i 'duti|MacVim|TextMate|TextEdit|Safari|helpviewer'
lsregister -dump | sed -E -n -e 's/^.*path:[[:space:]]+([^[:space:]].*\.app)$/  \1/p' | nl | egrep -i 'duti|MacVim|TextMate|TextEdit|Safari|helpviewer'


lsregister -dump | sed -E -n -e 's/^.*uti:[[:space:]]+(public\..*)$/  \1/p' | sort | uniq | nl
lsregister -dump | sed -E -n -e 's/^.*uti:[[:space:]]+(.*text.*)$/  \1/p' | sort | uniq | nl


alias utis="lsregister -dump | grep '[[:space:]]uti:' | awk '{ print \$2 }' | sort | uniq"
alias utis="lsregister -dump | sed -E -n -e 's/^.*uti:[[:space:]]+(.*)$/\1/p' | sort | uniq"
utis

utis | grep -i 'public\.' | nl

defaults read com.apple.LaunchServices


# ... or just use wsupdate from http://osxutils.sourceforge.net 
# move & copy the same file
unset -f mc
function mc() {
   if [[ $# -gt 1 ]] || [[ ! -e "$@" ]]; then return 1; fi
   tmpfile="${@}.tmp-${RANDOM}"
   printf "%s\n" "$tmpfile"
   /bin/mv "$@" "${tmpfile}"
   /bin/sleep 0.2
   /bin/cp -p "${tmpfile}" "$@"
   /bin/rm "${tmpfile}"
   return 0
}


unset -f mc
function mc() {
   if [[ $# -gt 1 ]] || [[ ! -e "$@" ]]; then return 1; fi
   tmpfile="/tmp/tmpfile.tmp-${RANDOM}"
   printf "%s\n" "$tmpfile"
   /bin/mv "$@" "${tmpfile}"
   /bin/sleep 0.2
   /bin/cp -Rp "${tmpfile}" "$@"
   /bin/rm -Rf "${tmpfile}"
   return 0
}



echo hello world > ~/Desktop/test.txt
echo hello world > ~/Desktop/test.html


# switch between Safari & Help Viewer as the default launching app for html files
open ~/Desktop/test.html


# cf. http://duti.sourceforge.net/documentation.php

printf "%s\n" 'com.apple.helpviewer   public.html   all' | duti     
#mc ~/Desktop/test.html
wsupdate ~/Desktop/test.html
open ~/Desktop/test.html

printf "%s\n" 'com.apple.Safari   public.html   all' | duti
wsupdate ~/Desktop/test.html
open ~/Desktop/test.html

rlsdb


# switch between TextEdit, MacVim & TextMate as the default launching app for plain text files

open ~/Desktop/test.txt

printf "%s\n" 'com.macromates.textmate   public.plain-text   all' | duti
open ~/Desktop/test.txt
wsupdate ~/Desktop/test.txt

printf "%s\n" 'org.vim.MacVim   public.plain-text   all' | duti
open ~/Desktop/test.txt    # type: ":q" or ":x" to quit test.txt
wsupdate ~/Desktop/test.txt

printf "%s\n" 'com.apple.TextEdit   public.plain-text   all' | duti
open ~/Desktop/test.txt
wsupdate ~/Desktop/test.txt



# cf. http://duti.sourceforge.net/documentation.php

/bin/cat > ~/Desktop/test.duti <<-'EOF'
com.apple.helpviewer   public.html   all
com.macromates.textmate   public.plain-text   all
EOF

# remove white space at line beginning
ed -s ~/Desktop/test.duti <<< $',s/^[[:space:]]*//\nw'

open -e ~/Desktop/test.duti

duti -v ~/Desktop/test.duti

wsupdate ~/Desktop/test.html
open ~/Desktop/test.html

wsupdate ~/Desktop/test.txt
open ~/Desktop/test.txt


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


# GUI alternative to switch the default launching app for a single file
[right-click on a file] + [alt]  ->  "Always Open With"

# GUI alternative to change the default launching app for all files of the same kind
[right-click on a file] -> "Get Info" -> "Open with:" -> [select an app] -> "Change All ..."