Silly script: Manipulate Ooo properties variables

[[Thanks to Reza too!]]

I like the


File > Properties > Custom properties ...

option in Openoffice.org (and LibreOffice) where I can specify different parameters/variables in my documents. This could be revision numbers, repo indicators, and what have you.

Now, since the Ooo actually stores the documents in a zipped xml format, the variables can be accessed through the linux domain with for example:


#!/bin/sh
...
oooFile=/some/path/to/your/file/fileName.odt
KEY="Document number"
unzip -pa $oooFile meta.xml | tr -s "<" "\n" | grep "$KEY" | tr -s ">" "\n" | tail -n 1

(Yes, there is a flaw, since if there would be $KEY’s that are substrings of each other, it could fail, I’ll leave to the happy hacker to fix that and post here in that case. I’ll solve it by choosing my property names.).

The Ooo file is unzipped, but only the meta.xml is extracted. We then replace the “<" with carriage returns to split the XML tags from their contents. We also strip at the end of the value of the property.

Now, encapsulating this in a function, for example daisyGetVarOoo.sh is easy as a cake. And multiple variables can be extracted on another level, for example:


KEY="Document repo"
docRepo=`daisyGetVarOoo.sh $oooFile "$KEY"`
KEY="Document number"
docNo=`daisyGetVarOoo.sh $oooFile "$KEY"`
KEY="Document type"
docType=`daisyGetVarOoo.sh $oooFile "$KEY"`
KEY="Document tag"
docTag=`daisyGetVarOoo.sh $oooFile "$KEY"`
KEY="Document revision"
docRev=`daisyGetVarOoo.sh $oooFile "$KEY"`
echo "${docRepo}_${docNo}_${docType}_${docTag}_${docRev}"

Using the same approach, it is also easy to modify the existing property and replace the meta.xml file in the zipped archive. Through linux we can therefore create and change the variables. This is quite handy in a documention versioning system – or if you want to bulk change names or sanity check variables settings in large batch of files.

Advertisements

One thought on “Silly script: Manipulate Ooo properties variables

  1. Nice!

    For the manipulation part of variables, something like this could be used (might need some nice formatting to be readable…):

    #!/bin/bash

    #usage: oooreplace.sh tag value file.odt
    #replaces all occurances of xxx with value in file.odt
    unzip -pa $3 meta.xml | sed “s/\(\)\(.*\)\(\)/\1$2\3/g” > /tmp/meta.xml

    zip -u -j $3 /tmp/meta.xml

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s