################# # # 19 Nov 2006 - written by Hans-Jörg Bibiko bibiko@eva.mpg.de # # exportpl set BASH variables according to the property list = # # if is an array tag a BASH array will be returned # if is a date tag output format YYYY-MM-DDTHH:MM:SSZGMTshift # # all variable values are UTF-8 encoded # # if a given key is not specified in plist an empty string will be returned # # each variable is named $TMD_ # # Usage: # # exportpl {key1 key2 key3 ... keyn} # # data := string containing valid plist data # file := plist file [.plist extension is not necessary] # key1...keyn := valid key(s) for plist # # # Examples: (output is an array with 8 items) # # a) # KEY=("returnCode" "output") # . "$TM_BUNDLE_SUPPORT"/bin/exportpl.sh "$TM_BUNDLE_SUPPORT"/bin/test.plist ${KEY[@]} # echo $TMD_returnCode # echo ${TMD_output[0]} # # # # b) # . "$TM_BUNDLE_SUPPORT"/bin/exportpl.sh "$TM_BUNDLE_SUPPORT"/bin/test returnCode output # echo $TMD_returnCode # echo ${TMD_output[5]} # # c) # DIA=$( cat "$TM_BUNDLE_SUPPORT"/bin/test.plist | tm_dialog -m test ) # . "$TM_BUNDLE_SUPPORT"/bin/exportpl.sh "$DIA" # #all key(s) for $DIA are exported # ############### if [ ${#1} -eq 0 ]; then echo -e "\n \n exportpl Usage:\n\n exportpl {key1 key2 key3 ... keyn} \n \n" exit_show_tool_tip fi # if $1 is a path to plist file or plist data if [ -e "$1" ]; then PLIST=$1 elif [ -e "$1.plist" ]; then PLIST="$1.plist" else # create tmp plist file for data tmp=${TMPDIR-/tmp} tfile=TextMatePLutf8DATA.$RANDOM.$RANDOM echo "$1" > "$tmp/$tfile.plist" PLIST="$tmp/$tfile.plist" fi # check plist for syntax PLERR=$(plutil -lint -s "$PLIST") if [ ${#PLERR} -gt 0 ]; then echo -e "exportpl Error:\n Data not valid or could not open plist file!" exit_show_tool_tip fi # check if key(s) are specified if [ $# -gt 1 ]; then # export only given keys shift # shift file INP="" # specified key(s) while [ "$#" -gt "0" ]; do INP="$INP$1\n" shift done RES=$(echo -e "$PLIST\n$INP" | perl -e ' use Foundation; $file = <>; chomp($file); @keys = <>; chomp(@keys); pop(@keys); $plist = NSDictionary->dictionaryWithContentsOfFile_( $file ); if ( $plist and $$plist) { foreach $key (@keys){ $val = $plist->objectForKey_( $key ); if($val and $$val) { $aval = $val->description()->UTF8String(); if ( $val->isKindOfClass_( NSArray->class ) ) { @arr = (); for($i=0; $i<$val->count(); $i++) { push(@arr, "\"".$val->objectAtIndex_( $i )->description()->UTF8String()."\""); } $aval = "(".join(" ",@arr).")"; } $aval =~ s/(.*?)( )(.*?)( )(.*)/$1T$3Z$5/ if ( $val->isKindOfClass_( NSDate->class ) ); $aval = "\"$aval\"" if ( $val->isKindOfClass_( NSString->class ) ); print "TMD_$key=$aval\n"; } else { print "TMD_$key=\"\"\n"; } } } else { print "echo -e \"exportpl Error:\n Could not open plist file!\"\n exit_show_tool_tip"; } ') else # export all key(s) from plist RES=$(echo "$PLIST" | perl -e ' use Foundation; $file = <>; chomp($file); $plist = NSDictionary->dictionaryWithContentsOfFile_( $file ); if ( $plist and $$plist) { $enum = $plist->keyEnumerator(); while($key = $enum->nextObject() and $$key){ $val = $plist->objectForKey_( $key ); $aval = $val->description()->UTF8String(); if ( $val->isKindOfClass_( NSArray->class ) ) { @arr = (); for($i=0; $i<$val->count(); $i++) { push(@arr, "\"".$val->objectAtIndex_( $i )->description()->UTF8String()."\""); } $aval = "(".join(" ",@arr).")"; } $aval =~ s/(.*?)( )(.*?)( )(.*)/$1T$3Z$5/ if ( $val->isKindOfClass_( NSDate->class ) ); $aval = "\"$aval\"" if ( $val->isKindOfClass_( NSString->class ) ); print "TMD_".$key->description()->UTF8String()."=$aval\n"; } } else { print "echo -e \"exportpl Error:\n Could not open plist file!\"\n exit_show_tool_tip"; } ') fi # delete tmp plist if needed if [ -e "$tmp/$tfile".plist ]; then rm "$tmp/$tfile".plist fi # set plist variables with prefix TMD_ eval "$RES"