| 1 |
#!/usr/bin/perl |
| 2 |
#----------------------------------------------------------------------------- |
| 3 |
# $Id : odf_set_title 0.3 2010-01-27 JMG$ |
| 4 |
#----------------------------------------------------------------------------- |
| 5 |
|
| 6 |
=head1 NAME |
| 7 |
|
| 8 |
odf_set_title - Set the title of a document using the first heading of the content |
| 9 |
|
| 10 |
=head1 USAGE |
| 11 |
|
| 12 |
odf_set_title <filename> <new title> |
| 13 |
|
| 14 |
=head1 SYNOPSIS |
| 15 |
|
| 16 |
This sample script outputs the current title of an ODF file, then it replaces |
| 17 |
it using a command line argument as the new title. If the command line doesn't |
| 18 |
provide a new title, then the program uses the first heading text in the |
| 19 |
document body. If the document doesn't contain any heading element, nothing |
| 20 |
is changed. |
| 21 |
|
| 22 |
=cut |
| 23 |
|
| 24 |
use OpenOffice::OODoc 2.112; |
| 25 |
|
| 26 |
my $new_title = undef; |
| 27 |
|
| 28 |
# create an ODF file object |
| 29 |
# using the 1st command line argument as filename |
| 30 |
my $oofile = odfContainer($ARGV[0]); |
| 31 |
# exit if $filename isn't available or can't be open |
| 32 |
# as a regular ODF file |
| 33 |
die "Unavailable file $ARGV[0]\n" unless $oofile; |
| 34 |
|
| 35 |
# create a metadata-aware object, linked to the File object |
| 36 |
my $doc_meta = odfMeta(container => $oofile); |
| 37 |
|
| 38 |
# extract the title form the metadata object |
| 39 |
# (without argument, the 'title' method is a 'get' accessor) |
| 40 |
my $title = $doc_meta->title; |
| 41 |
|
| 42 |
# if the title is defined, display it and exit |
| 43 |
if (defined $title) |
| 44 |
{ |
| 45 |
print "The existing title is \"$title\"\n"; |
| 46 |
} |
| 47 |
|
| 48 |
if (defined $ARGV[1]) |
| 49 |
{ |
| 50 |
$new_title = $ARGV[1]; |
| 51 |
} |
| 52 |
elsif (! defined $title) |
| 53 |
{ |
| 54 |
# use the 1st heading of the content |
| 55 |
# create a content-aware object linked to the same container |
| 56 |
my $doc_text = odfDocument |
| 57 |
( |
| 58 |
container => $oofile, |
| 59 |
read_only => 'true' |
| 60 |
); |
| 61 |
# get the text of the first heading element |
| 62 |
my $text = $doc_text->getHeadingText(0); |
| 63 |
if ($text) |
| 64 |
{ |
| 65 |
# use this text, if defined, as the title |
| 66 |
$new_title = $text; |
| 67 |
} |
| 68 |
else |
| 69 |
{ |
| 70 |
# there was no heading in the document, |
| 71 |
# so nothing is done |
| 72 |
warn "No heading text in the document.\n" . |
| 73 |
"Nothing is changed.\n"; |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
# the new title (if any) is saved |
| 78 |
if (defined $new_title) |
| 79 |
{ |
| 80 |
# set the new title |
| 81 |
print "The new title is \"$new_title\"\n"; |
| 82 |
$doc_meta->title($new_title); |
| 83 |
# update the revision number |
| 84 |
$doc_meta->increment_editing_cycles; |
| 85 |
# commit the update |
| 86 |
$oofile->save; |
| 87 |
} |
| 88 |
|
| 89 |
exit; |