Debian Codegraph @

examples/odf_set_fields

raw
1 #!/usr/bin/perl
2 #-----------------------------------------------------------------------------
3 # $Id : odf_set_fields 0.4 2010-01-27 JMG$
4 #-----------------------------------------------------------------------------
5
6 =head1 NAME
7
8 odf_set_fields - Set names & values for user-defined fields
9
10 =head1 USAGE
11
12 odf_set_fields <filename> -options
13
14 =head1 SYNOPSIS
15
16 Sample script updating or creating 4 user-defined fields of an ODF
17 file and adding new keywords. Existing keywords are preserved. Old user-defined
18 fields names and values are deleted and replaced. The revision number of the
19 document is incremented by 1.
20
21 The keywords must be passed as a comma-separated list through the -keywords
22 option.
23
24 The user-defined fields/options are :
25 -contact -organization -status -diffusion
26
27 Examples:
28
29 odf_set_fields foo.odt -contact "Donald Duck" -organization "Genicorp"
30
31 odf_set_fields foo.odt -status "Complete" -keywords "software, office"
32
33 =cut
34
35 use OpenOffice::OODoc::Meta;
36 use Getopt::Long;
37
38 # default values for the user-defined fields (examples)
39 my $contact = 'Corporate Editor';
40 my $organization = 'Foo Unlimited';
41 my $status = 'Draft';
42 my $diffusion = 'Public/Unclassified';
43 my $keywords = undef;
44
45 # get the command line options
46 GetOptions
47 (
48 'contact=s' => \$contact,
49 'organization=s' => \$organization,
50 'status=s' => \$status,
51 'diffusion=s' => \$diffusion,
52 'keywords=s' => \$keywords
53 );
54
55 # get the command line argument as filename
56 my $filename = $ARGV[0]
57 or die "usage : odf_set_fields filename [-options]\n";
58
59 # create a meta-data object linked to the file
60 my $doc = OpenOffice::OODoc::Meta->new(file => $filename)
61 or die "I can't open $filename as an ODF document\n";
62
63 # set the user-defined fields using a list of name/value pairs
64 # (the names are hard-coded in my example but they could be
65 # dynamically defined just like the values)
66 $doc->user_defined
67 (
68 Contact => $contact,
69 Organization => $organization,
70 Status => $status,
71 Diffusion => $diffusion
72 );
73
74 # add the new keyword list (got from the -keyword command line option
75 # if any). The 'keywords' method needs a list, so we have to split the
76 # content of the -keywords option, assuming separator=comma
77 # There is no risk to introduce some keyword redundancy here, because
78 # the 'keywords' method from OpenOffice::OODoc::Meta adds only non-existing
79 # keywords
80 $doc->keywords(split(",", $keywords)) if $keywords;
81
82 # because I'm sometimes a perfectionist, I want to
83 # put the program signature in the 'generator' field (unavailable for
84 # the end-user, but readable/updateable for OpenOffice::OODoc::Meta just as for
85 # the OpenOffice.org software); useful to allow any other program to know
86 # if this version comes directly from the StarOffice/OpenOffice suite or
87 # has been generated by my program
88 $doc->generator('odf_set_fields Version 0.4');
89
90 # with the same kind of idea, I want to
91 # increment the editing cycles count (just to mimic the OpenOffice.org
92 # software each time the file is edited, and maybe
93 # to help some other workflow/versioning management program)
94 $doc->increment_editing_cycles;
95
96 # commit all and leave
97 $doc->save;
98 exit;