Version 0 of parsing Mac OS X preferences using the 'defaults' command

Updated 2011-03-25 16:39:24 by _hc

On Mac OS X, you can easily read and write to the preference .plist files using the 'defaults' command. I didn't find anything on parsing the results in Tcl, so I quickly wrote up a script to parse an array of filenames like you'd use to store a "Recent Files" list.

set domain "recentfilestext"
set key "recentfiles"

# this generates the test data
catch {exec defaults write $domain $key -array patch.pd another.pd "file with spaces.txt" file,with,commas.pd oneword anotherpatch.pd}

# this parses into a Tcl list
if {![catch {exec defaults read $domain $key} arr]} {
    puts "arr $arr"
    set filelist $arr
    regsub -all -- {("?),\s+("?)} $filelist {\1 \2} filelist
    regsub -all -- {\n} $filelist {} filelist
    regsub -all -- {^\(} $filelist {} filelist 
    regsub -all -- {\)$} $filelist {} filelist
    puts "filelist: $filelist"
    foreach file $filelist {
        set filename [regsub -- {,$} $file {}]
        puts "file: $filename"
    }
}