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

Updated 2011-03-25 19:19:17 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"

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

if {![catch {exec defaults read $domain $key} arr]} {
    puts "arr $arr"
    set filelist $arr
    regsub -all -- {",\s+"} $filelist {" "} 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"
    }
}