Conditionally write to XMP file based on type of source file

Started by Joanna Carter, May 30, 2021, 08:24:54 AM

Previous topic - Next topic

Joanna Carter

It's me again  ;)

In trying to optimise the number of commands I am executing, I'm trying to enumerate a list of urls, which can be either image files or xmp files, using the same code.

So, I need to detect whether the source file is a RAW file or not. I'm using this command to test...

exiftool -if '$fileType eq "NEF"' -tagsfromfile JNA_0052.NEF "-format<mimetype" JNA_0052.xmp

It always fails, no matter what file extension I put in the condition. Any ideas please?

StarGeek

You're processing an XMP file.  That Filetype will never equal NEF and always be Filetype eq "XMP".
"It didn't work" isn't helpful. What was the exact command used and the output.
Read FAQ #3 and use that cmd
Please use the Code button for exiftool output

Please include your OS/Exiftool version/filetype

Joanna Carter

Even if I replace NEF with XMP, it still fails the condition.

I have an array of files which can be either RAW or XMP. What I am trying to do is to transfer two tags from the NEF (or other RAW file) to the XMP file, but only if the "source" file is a RAW file.

Can you help with the correct syntax?

Joanna Carter

So, can anyone help with this?

To clarify what I'm after...

1. I have a list of files that can be either NEF or XMP.
2. I want to process them in such a way that, if the file is NEF, an XMP file with the same name should be either created and/or written to with two tags copied from the NEF file.

Helllppp!!!  :o ::)

Phil Harvey

See Example 13 here, and add as many -ext options as you want for the various raw files you want to process.

- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

Joanna Carter

Quote from: Phil Harvey on May 31, 2021, 09:03:51 PM
See Example 13 here, and add as many -ext options as you want for the various raw files you want to process.
Thank you for that lead but I don't want to process a whole directory, just a list of files that I am generating in code.

Is that possible with one command per file?

Phil Harvey

You just put whatever files/directories you want to process on the command line.

Doing one command per file is much slower.  You can put as many file names on the command line as you want.  (There is a command-line-length limitation, and if you are getting close to that you should use the -@ option and put the file names in an argfile.)

- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

Joanna Carter

Thank you once again. Most of the time I am only dealing with writing to one file at a time; it just depends on how many images the user selects in the software but it would be rare to select a whole directory.

So, for unconditional code, am I right in saying that my code should be creating these (or more) "arguments"?


-xmp:subject=Didier JNA_0052.NEF
-tagsfromfile JNA_0052.NEF "-xmp:subject<xmp:subject" "-SidecarForExtension<filetype" "-format<mimetype" JNA_0052.xmp
-api compact=onedesc


What I now want to do is execute the second two lines only if the first line applies to a RAW file, hence my idea of the -if '$filetype eq "NEF"' to test the file type of the source file but that seems to test the target file instead.

Joanna Carter

My software searches for keywords, which could be found either in a standalone RAW file or in a related sidecar. If the search returns the URL of an XMP file, I then need to get back to the RAW file in order to show its thumbnail instead of an icon for the RAW file.

This whole effort is all about the faint possibility of having more than one RAW file type, with the same name, in the same directory and trying to avoid writing to an XMP file for the wrong RAW file.

I know it is not normal to have more than one RAW file with the same name because of just such confusion. But it is still possible that someone could add a second RAW file after the XMP has been written for the first.



It might seem unlikely but I feel I should cover the eventuality.

Alan Clifford

Quote from: Joanna Carter on June 01, 2021, 01:18:32 PM

This whole effort is all about the faint possibility of having more than one RAW file type, with the same name, in the same directory and trying to avoid writing to an XMP file for the wrong RAW file.


Can't you name your xmp files as

myphotosname.nef.xmp
myphotosname.nrw.xmp



Joanna Carter

Quote from: Alan Clifford on June 01, 2021, 02:40:26 PM
Can't you name your xmp files as

myphotosname.nef.xmp
myphotosname.nrw.xmp
If they were my files, yes. But this app is for photographers who already have their EXIF data in XMP files. It us my job to be provide a search facility that looks into their files, whether they be RAW or XMP and, if they are XMP files find the RAW file that it is based on

Phil Harvey

#11
Quote from: Joanna Carter on June 01, 2021, 10:38:05 AM
So, for unconditional code, am I right in saying that my code should be creating these (or more) "arguments"?


-xmp:subject=Didier JNA_0052.NEF
-tagsfromfile JNA_0052.NEF "-xmp:subject<xmp:subject" "-SidecarForExtension<filetype" "-format<mimetype" JNA_0052.xmp
-api compact=onedesc


What I now want to do is execute the second two lines only if the first line applies to a RAW file, hence my idea of the -if '$filetype eq "NEF"' to test the file type of the source file but that seems to test the target file instead.

Try this.  (I list the arguments in -@ argfile format):

-xmp:subject=Didier
JNA_0052.NEF
-ext
nef
-ext
cr2
-ext
arw
... (all the raw extensions you support)
-execute
-if
$ok
-tagsfromfile
JNA_0052.NEF
-xmp:subject<xmp:subject
-SidecarForExtension<filetype
-format<mimetype
JNA_0052.xmp
-api
compact=onedesc


- Phil

Edit: forgot newline after -api
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

Joanna Carter

Wow! Almost  :D

Since your reply, I have realised that I really shouldn't be writing anything to the RAW file if the user has chosen to use XMP files.

So, now I end up with...


-xmp:subject=Didier JNA_0052.xmp
-tagsfromfile JNA_0052.NEF "-SidecarForExtension<filetype" "-format<mimetype" JNA_0052.xmp
-api compact=onedesc


... to write the subject to the XMP file followed by copying the tags from the RAW to the XMP file.

But I realise that this is actually two commands. Since I am now only concerned with writing to the XMP file, how do I best combine the two?

Joanna Carter

Aaaarrrgghhh!!!

Why do I have to think of the answer after posting an obvious error?

I now have...


-xmp:subject=Didier
-tagsfromfile JNA_0052.NEF "-SidecarForExtension<filetype" "-format<mimetype" JNA_0052.xmp
-api compact=onedesc


Which is exactly what I wanted, once I realised what I thought I wanted wasn't what I really wanted  :o ::) ???

Phil, thank you so much for taking the time and effort to help me figure this stuff out.

Joanna Carter

Well, thanks to your help, I now have the code I need for writing to an XMP file without needing to write to the RAW file as well, and setting the tags from the RAW file that I will need for searching.


    -preserve
    -ignoreMinorErrors
    -overwrite_original_in_place
    -mwg:keywords=Didier
    -tagsFromFile /Users/joannacarter/Pictures/JNA_0052.NEF '-SidecarForExtension<filetype'
    /Users/joannacarter/Pictures/JNA_0052.xmp


But, even though everything works, I am getting a minor warning peeking through...


Warning: No writable tags set from /Users/joannacarter/Pictures/JNA_0052.xmp


This doesn't happen from the command line

Any ideas?