Dear all,
here comes my first approach for a BASH function to export <key>=<value> out from a property list.
# # 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> (with prefix TMD_) # # 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 #
What should I add/change/etc. ?
I used the objectC2Perl bridge to parse the plist data. The only thing I don't know yet whether the perl library 'Foundation' is installed as default. Tomorrow I will check this at a fresh Mac installation without 'Developer Toolkit'.
Cheers,
Hans-Jörg
---------------------------------------------------------------- This message was sent using IMP, the Internet Messaging Program.
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.
Hi,
I just figured out that in BASH you cannot export an array to make it visible for subshells.
Any suggestions how to deal with this?
One chance would be instead of making a BASH array to create a string variable which is certain structure. Then you have to split this variable in BASH, perl, ruby, etc by hand. An other chance would be to create n+1 variables, i.e if you have an array TMD_output=("A" "b" "c and d") you could generate TMD_output_0=3 # how many items TMD_output_1="A" TMD_output_2="b" TMD_output_3="c and d"
Greetings
Hans-Jörg
On 19 Nov 2006, at 20:20, Hans-Joerg Bibiko wrote:
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.
Next problem:
bash$ echo "ouch!" bash: !": event not found bash$ csh -f % echo "ouch!" ouch! % exit bash$ tcsh -f : echo "ouch!" ouch! : exit bash$
It seems that BASH has a problem with '!' if it occurs in double quoted strings. I can write to avoid the error
bash$ echo "ouch!" ouch!
but then I have an escaped '!' (???)
any suggestions
Hans
OK
to avoid the ! error you can use '' instead of "" but then it complicates a bit the BASH function
Hans
On 20 Nov 2006, at 10:52, Hans-Joerg Bibiko wrote:
Next problem:
bash$ echo "ouch!" bash: !": event not found bash$ csh -f % echo "ouch!" ouch! % exit bash$ tcsh -f : echo "ouch!" ouch! : exit bash$
It seems that BASH has a problem with '!' if it occurs in double quoted strings. I can write to avoid the error
bash$ echo "ouch!" ouch!
but then I have an escaped '!' (???)
any suggestions
On 20. Nov 2006, at 11:10, Hans-Joerg Bibiko wrote:
to avoid the ! error you can use '' instead of "" but then it complicates a bit the BASH function
! refers to a history event, i.e. something you previously typed, e.g. !! is previous line, !$ is last argument of previous line, !# is current line, !#:1 is first argument of first line, etc. These are actually quite handy, here are some examples:
? 43*37.34*7.45 # do some calculation eur `!!` # convert the result of last line from euro # (both ? and eur are custom functions)
chmod a+w /path/to/some/file # make file word writeable ls -l !$ # check if the file has proper mode
cp some_filename.txt backup_!#:1 # make backup, re-using the name
History substitution can do much more, like even search backwards, but the above are the most common use-cases for me.
Anyway, in double quoted strings, bash does expand various things, like $VARIABLES, `code` and these history events. So you definitely want to use single quoted strings, where the only thing you need to take care of, is apostrophes.
Why you can’t escape it in a double-quoted string is weird, one solution though would be to escape it by first closing the string, i.e.: echo "foo"!" bar".
As for arrays and sub-shells, I think you mean sub-processes, i.e. the array cannot be exported to new processes you launch from the shell (since arrays are bash-only), but would that matter? If you start a new process, chances are that process is a scripting language that can parse property lists.
On 20 Nov 2006, at 11:58, Allan Odgaard wrote:
On 20. Nov 2006, at 11:10, Hans-Joerg Bibiko wrote:
to avoid the ! error you can use '' instead of "" but then it complicates a bit the BASH function
... Why you can’t escape it in a double-quoted string is weird, one solution though would be to escape it by first closing the string, i.e.: echo "foo"!" bar".
As for arrays and sub-shells, I think you mean sub-processes, i.e. the array cannot be exported to new processes you launch from the shell (since arrays are bash-only), but would that matter? If you start a new process, chances are that process is a scripting language that can parse property lists.
Well, that's right. I guess each script language can parse plist by itself, but I find it quite convient to export also an BASH array and you will simplify your script, e.g. if you want to write an bash inline script command à la perl -e ''.
Nevertheless I will add an option to exportpl to export an array as a string which will be exported. On the other hand I just check for escaping some characters for bash.
The new version will come soon.
Best,
Hans
Hi,
here comes the revised version.
Changes: -better escaping for values (hopefully I caught all; "foo"!" bar" works fine) -improved error handling (also checking if variable name is not valid for bash) -errors are outputted as tooltips -added the option '-a' to export a plist array as a single string delimited by TABs for 'export' -fixed the problem to delete the temporary file if <data> syntax is not valid
More details at the beginning of the attached file 'exportpl'
Question: Is there any case that the plist coming from tm_dialog contains NSBoolean or NSData? If yes, what is the best way to handle these?
Have a nice day,
and comments are welcome, as always
Hans
On 20. Nov 2006, at 17:25, Hans-Joerg Bibiko wrote:
Question: Is there any case that the plist coming from tm_dialog contains NSBoolean or NSData? If yes, what is the best way to handle these?
Booleans would come as <true/> or <false/> -- if the user binds to a checkbox, then I am quite sure it will export as such.
NSData would only be if the user used the NSUnarchveDataTransformer (or whatever it is called), I don’t think this is likely.
Quoting Allan Odgaard throw-away-1@macromates.com:
On 20. Nov 2006, at 17:25, Hans-Joerg Bibiko wrote:
Question: Is there any case that the plist coming from tm_dialog contains NSBoolean or NSData? If yes, what is the best way to handle these?
Booleans would come as <true/> or <false/> -- if the user binds to a checkbox, then I am quite sure it will export as such.
I tried it out but if I specify a checkbox (see attached picture from IB) I only get this in my plist
<key>regExp</key> <integer>1</integer>
Or how should you bind this to get Boolean?
Cheers,
Hans
---------------------------------------------------------------- This message was sent using IMP, the Internet Messaging Program.
Hi,
Changes: -for safety now the function ignores <data> and nested <dict> tags -<boolean> tag -> true := 1 false := 0 -all variables used within the function (TM1_ prefix) will be unset at the end -simplified the code a bit
Now it should work ;)
Best, Hans
---------------------------------------------------------------- This message was sent using IMP, the Internet Messaging Program.
On 21. Nov 2006, at 07:23, Hans-Joerg Bibiko wrote:
Quoting Allan Odgaard throw-away-1@macromates.com:
On 20. Nov 2006, at 17:25, Hans-Joerg Bibiko wrote:
Question: Is there any case that the plist coming from tm_dialog contains NSBoolean or NSData? If yes, what is the best way to handle these?
Booleans would come as <true/> or <false/> -- if the user binds to a checkbox, then I am quite sure it will export as such.
I tried it out but if I specify a checkbox (see attached picture from IB) I only get this in my plist
<key>regExp</key> <integer>1</integer>
Or how should you bind this to get Boolean?
I never actually tested it, I just assumed ;)