Right, well, the script has been furthered since we last all spoke thanks much to the time and expertise of Phil Harvey (ExifTool:
So now the script will interrogate the files to spit out an XMP file with the data from the Text file in the right places:
#!/usr/bin/perl -w
use strict;
my $commentTemplate="<rdf

escription rdf:about='' xmlns:dc='
<dc:description><rdf:Alt><rdf:li xml:lang='x-default'>COMMENT</rdf:li></rdf:Alt></dc:description>
</rdf

escription>
";
my $template="<?xpacket begin='' id=''?>
<x:xmpmeta xmlns:x='adobe:ns:meta/' x:xmptk='XMP toolkit 2.9-9, framework 1.6'>
<rdf:RDF xmlns:rdf='
xmlns:iX='
<rdf

escription rdf:about='' xmlns:Iptc4xmpCore='
<Iptc4xmpCore:SubjectReference>URL</Iptc4xmpCore:SubjectReference>
<Iptc4xmpCore:SubjectCode>
<rdf:Bag>
<rdf:li>URL</rdf:li>
</rdf:Bag>
</Iptc4xmpCore:SubjectCode>
</rdf

escription>
<rdf

escription rdf:about='' xmlns

hotoshop='
<photoshop:Headline>TITLE</photoshop:Headline>
</rdf

escription>
COMMENT
<rdf

escription rdf:about='' xmlns

hotomechanic='
</rdf

escription>
<rdf

escription rdf:about='' xmlns:xap='
<xap:Rating>0</xap:Rating>
</rdf

escription>
</rdf:RDF>
</x:xmpmeta>
<?xpacket end='w'?>
";
foreach my $file (@ARGV) {
open F, $file or die "unable to open $file";
local $/;
my $content=<F>;
print "==== $file\n";
if ($content =~ /Title:\n(.*?)\s+URL:\n(.*?)(\s+Comment:\n(.*?)\s+)?$/) {
my $title=$1;
my $url=$2;
my $comment=$4;
print "Title is $title\n\n";
print "URL is $url\n\n";
if (defined $comment) {
print "Comment is $comment\n\n";
} else {
$comment = '';
}
my $xml = $template;
foreach ($title, $url, $comment) {
s/&/&/g;
s/>/>/g;
s/</</g;
}
$xml =~ s/TITLE/$title/g;
$xml =~ s/URL/$url/g;
$xml =~ s/COMMENT/$commentTemplate/ if length $comment;
$xml =~ s/COMMENT/$comment/g;
my $outfile = $file;
$outfile =~ s/\.[^\\\/]*$//;
open OUTPUT, ">$outfile.xmp" or die "unable to create output $outfile.xmp";
print OUTPUT $xml;
close OUTPUT;
} else {
print "Unexpected format\n";
}
close F;
}
The code *does* work for me but a problem I've found, on occasion, is that the files sometimes do not always have the "Comment" field filled out and this will stop the script from working as it should i.e. only "Title" and "URL" are present.
Apparently the following line is of concern:
if ($content =~ /Title:\n(.*?)\s+URL:\n(.*?)(\s+Comment:\n(.*?)\s+)?$/) {
I thought it might be that Comments wasn't case sensitive but it is, I've had a look and how the code bounces from place to place and I can't tell why it cannot skip the Comment search when it can't find it in a text file.
There is a section to this which makes sure that the overriding extension to a file is always going to be outputted as XMP, this is because a picture file is accompanied with a Text file (like a BMP file which has no ability to contain metadata) and so looks like "filename.bmp" and so the textfile looks like "filename.bmp.txt" and if the script had of converted it it would then read as "filename.bmp.txt.xmp" and so when importing into Aperture it would not work, it must end in XMP and be the only extension to the same file name as the picture.
Apparently the problem lies in the "$content" expression, when the script tests for it and it doesn't match. I have a link about how expressions work but it's as long as my Santa list and it's full of stuff that I don't understand so I was hoping somebody else might be able to help.
link:
txt with "Title", "URL" and "Comment":
txt with just "Title and "URL":
Thanks!