[TxMt] export property list keys as BASH variables

Hans-Joerg Bibiko bibiko at eva.mpg.de
Sun Nov 19 19:20:32 UTC 2006


Hi,

of course I forgot something.

In order to be able to have access from a perl, python, ruby, etc.  
script I export these BASH variables now.

On the other hand there is still one question:

I set the value to an empty string if the given key is not set as key  
witin the plist. Is there a better way? I believe it is ok and the  
user has to catch this by itself.

Bye,

Hans

----------------------------------------------------------------
This message was sent using IMP, the Internet Messaging Program.

-------------- next part --------------
#################
#
# 19 Nov 2006 - written by Hans-Jörg Bibiko   bibiko at eva.mpg.de
#
#   exportpl set BASH variables according to the property list <key>=<value>
#
#    if <value> is an array tag a BASH array will be returned
#    if <value> 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_<key>
#
#   Usage:
#
#     exportpl <data|file> {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 <data|file> {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(" ", at arr).")";
			}
			$aval =~ s/(.*?)( )(.*?)( )(.*)/$1T$3Z$5/ if ( $val->isKindOfClass_( NSDate->class ) );
			$aval = "\"$aval\"" if ( $val->isKindOfClass_( NSString->class ) );
			print "export TMD_$key=$aval\n";
		} else {
			print "export 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(" ", at arr).")";
		}
		$aval =~ s/(.*?)( )(.*?)( )(.*)/$1T$3Z$5/ if ( $val->isKindOfClass_( NSDate->class ) );
		$aval = "\"$aval\"" if ( $val->isKindOfClass_( NSString->class ) );
		print "export 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"


More information about the textmate mailing list