PDF rename based on Zotero entry

Here is a tool that will automatically rename the most recently downloaded PDF with the most recently entered Zotero entry. You may call it directly from the browser by setting a link in the toolbar to file:///c:/system/test.000 while setting the file association for 000 in the Content tab to the location of ZoteroRename.exe.

ZoteroRename.zip includes the source perl script including a compiled windows version.

 1:
 2:
 3:
 4:
 5:
 6:
 7:
 8:
 9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
59:
#!/usr/bin/perl
# ZoteroRename.pl links most recently downloaded PDF with most recent Zotero journal entry
# m@wjst.de 17/1/08

my $dbfile = <$ARGV[0]>;
my $dir = <$ARGV[1]>;

#----------------------------------------------------------------------------------------------

use DBI;

$dbh = DBI->connect("dbi:SQLite:$dbfile","","",{ RaiseError => 1 }) || die "Cannot connect: $DBI::errstr";

my $sql = 'SELECT DISTINCT items.itemID
FROM items
WHERE items.itemTypeID=4
ORDER BY items.dateAdded DESC
LIMIT 1';
$res0 = $dbh->selectall_arrayref($sql);
$ID = $res0->[0][0];

my $sql = 'SELECT DISTINCT itemDataValues.value
FROM items LEFT JOIN itemData ON items.itemID = itemData.itemID LEFT JOIN itemDataValues ON itemData.valueID = itemDataValues.valueID
WHERE items.itemID=' . $ID . ' AND itemData.fieldID IN (14,12)';
$res1 = $dbh->selectall_arrayref($sql);
$dt = $res1->[1][0];
$dt =~ s/(^[0-9]{4})(.*)//gi;
$so = $res1->[0][0];
$so =~ s/\s//gi;

my $sql = 'SELECT DISTINCT creators.lastName
FROM items LEFT JOIN itemCreators ON items.itemID = itemCreators.itemID LEFT JOIN creators ON itemCreators.creatorID = creators.creatorID
WHERE items.itemID=' . $ID . ' AND itemCreators.orderIndex=0';
$res2 = $dbh->selectall_arrayref($sql);
$nm = $res2->[0][0];
$newfn = "$nm-$so-$dt.pdf";

$dbh->disconnect;

#----------------------------------------------------------------------------------------------

use DirHandle;

my $dir_handle = new DirHandle $dir or die "unable to open $dir $!";
my %newest;

$newest{mtime} = 0;
while (defined($file = $dir_handle->read)) {
   next if ($file eq '.' or $file eq '..');
   my $mtime = (stat("$dir/$file"))[9];
   $newest{file} = $file and $newest{mtime} = $mtime if $newest{mtime} < $mtime;
}

$oldfn = $newest{file};

#----------------------------------------------------------------------------------------------

rename ("$dir/$oldfn","$dir/$newfn") or die "unable to rename $dir/$oldfn => $dir/$newfn $!";

6 thoughts on “PDF rename based on Zotero entry”

  1. I still do not fully understand how to use this plugin. But it seems to be very useful.
    Can you elaborate a little bit?

  2. Sounds like a really great addition. I am, however, not sure how to use it. Can you please explain in some more detail how I have to set the tool up.

    Thanks!

  3. hello. I think this is absolutely marvelous but I couldn’t get this to work. Could you please leave a hint for us non-perlers how to use this thing?

    I tried reading the code but it doesnt make any sense.

  4. Hint for Windows Users: I did not get the compiled exe to work, but if you have a perl interpreter (eg. activeperl) the perl script works fine from the command line: perl (path to ZoteroRename.pl) (path to Zotero.sqlite) (path to folder with new pdf file). Then the newest file in the folder ist renamed to author-journal-year.pdf.

    -junin

  5. this sounds perfect!

    a few questions:
    1. does it maintain the link in Zotero or do you have to re-associate the file?
    2. can you make it rename ALL the pdfs?
    3. any way to make this into a Zotero extension or a Firefox extension?

Comments are closed.