ExifTool Forum

ExifTool => The "exiftool" Application => Topic started by: Alan Clifford on May 17, 2013, 09:50:41 AM

Title: conditional in an arg file?
Post by: Alan Clifford on May 17, 2013, 09:50:41 AM
What I want to do is,
  if xmp:location doesn't exist, add mwg:location
  if xmp:location exists, copy it to mwg:location

Can I put a conditional in an arg file?  So far, I can't  :(

-overwrite_original_in_place
-countrycode=BB
-event=Arriving in Barbados
-mwg:country=Barbados
-mwg:location=Over Barbados
-if not defined $xmp:location
-mwg:keywords-=Barbados
-mwg:keywords+=Barbados


Title: Re: conditional in an arg file?
Post by: Phil Harvey on May 17, 2013, 09:53:50 AM
Hi Alan,

There are 2 things wrong with what you have tried:

1) it is one argument (not one option) per line of the argfile

2) the -if condition applies to the entire command

So let me understand:  You want to add an MWG keyword only if XMP:location is not defined?

If this is correct, then you would need to do this in a separate command:

-overwrite_original_in_place
-countrycode=BB
-event=Arriving in Barbados
-mwg:country=Barbados
-mwg:location=Over Barbados
FILE
-execute
-overwrite_original_in_place
-if
not defined $xmp:location
-mwg:keywords-=Barbados
-mwg:keywords+=Barbados
FILE


- Phil
Title: Re: conditional in an arg file?
Post by: Alan Clifford on May 17, 2013, 10:28:22 AM
Quote from: Phil Harvey on May 17, 2013, 09:53:50 AM

So let me understand:  You want to add an MWG keyword only if XMP:location is not defined?


Thanks for a very quick reply.

I run my script which will put in the mwg:location, say Barbados.  I then use Nikonview to modify stuff, say put in "Bathsheba, Barbados".  Nikonview doesn't do all the mwg stuff.  So I want to run the script again.  Or maybe even put in the location first with Nikon view.  So when I run the script again, photos with no xmp:location would get the default from the argfile whilst photos that had the location input manually with Nikonview would get the mwg stuff updated.

In your example, you've used FILE.  I got a bit of an "eeek" moment there.  I take it that is not literally the word "FILE" but should be the name of the photo, something like %f?
Title: Re: conditional in an arg file?
Post by: Phil Harvey on May 17, 2013, 11:08:30 AM
Hi Alan,

If you are are launching exiftool each time this argfile is used, then I would drop the -overwrite_original_in_place and FILE (the file and/or directory names) from the argfile, and do this instead:

exiftool -@ ARGFILE -common_args -overwrite_original_in_place FILE

I hope this makes sense.

- Phil
Title: Re: conditional in an arg file?
Post by: Alan Clifford on May 17, 2013, 12:06:38 PM
Thanks Phil, I think I'm there

exiftool  -m -P -@ "${ARGFILE}" -common_args -overwrite_original_in_place  "${xmpphoto}"  >&2

with argfile

-mwg:location<$xmp:location
-if
defined $xmp:location
-execute
-mwg:location=Over Barbados
-if
not defined $xmp:location
-execute
-countrycode=BB
-event=Arriving in Barbados
-mwg:country=Barbados
-mwg:keywords-=Barbados
-mwg:keywords+=Barbados


I think I shall have to do the same for the keywords in order to update the IPTCDigest if I add keywords using Nikonview.
Title: Re: conditional in an arg file?
Post by: Phil Harvey on May 17, 2013, 12:09:18 PM
Hi Alan,

Looks good, but I think you should probably move the -m and -P to the common arguments.  Otherwise they will only be part of the first command.

- Phil
Title: Re: conditional in an arg file?
Post by: Alan Clifford on May 20, 2013, 08:20:21 AM
I'm happy with this (see below) but I've got a mixture of code and data that is a bit awkward.  So what I was thinking was that I could create some user defined fields in a config file and then put them all at the top of the arg file, for example,
mylocation=Over Barbados
at the top of the arg file, put
-mwg:location<$mylocation
in the code section then all the data would be at the top and I wouldn't have to touch the code bits.  Presumably, I could tidy up by removing the user defined fields at the end.

Would this be a good way of doing it?



# Title, event and location will not be changed if they exist
# Additional keywords added with another program will not be deleted
#
# Used by addstufftojpg.sh
# use exiftool -@ keywordsandstuff.args -common_args -m -P -overwrite_original_in_place photo.jpg
# instead of photo.jpg, use eg *,jpg,  -ext jpg . or  *.mov.xml
#
# validfrom: 2012:10:25 18:43:00+00:00
# validto:   2012:10:25 19:02:00+00:00
#
# Don't touch this rule
-mwg:location<$xmp:location
-if
defined $xmp:location
-execute
#
# default location if not defined already
-mwg:location=Over Barbados
-if
not defined $xmp:location
-execute
#
# default event if not already defined
-event=Arriving in Barbados
-if
not defined $event
-execute
#
# default title if not already defined
-title=Flying into Barbados
-if
not defined $title
-execute
#
-countrycode=BB
-mwg:country=Barbados
-mwg:keywords-=Barbados
-mwg:keywords+=Barbados
Title: Re: conditional in an arg file?
Post by: Phil Harvey on May 20, 2013, 09:19:13 AM
You can't define something like $mylocation in an argfile.  But you can put the location in a separate file (mylocation.txt) and do something like this.  Also, I realized you can do this all with a single command since copying from XMP:Location will only occur if it exists:

# set MWG location to XMP:Location if it exists, otherwise mylocation
-mwg:location<=mylocation.txt
-mwg:location<xmp:location
# default event if not already defined
-event-=
-event=Arriving in Barbados
# default title if not already defined
-title-=
-title=Flying into Barbados
# other stuff
-countrycode=BB
-mwg:country=Barbados
-mwg:keywords-=Barbados
-mwg:keywords+=Barbados


- Phil
Title: Re: conditional in an arg file?
Post by: Alan Clifford on May 20, 2013, 09:57:34 AM

Thanks for all that Phil.

I thought I could put it in a config file rather than an arfgile.  But with the argfile you've suggested, I don't think I'd need it as it's much clearer.  But I need to read your documentation to work out why this works

# default event if not already defined
-event-=
-event=Arriving in Barbados
Title: Re: conditional in an arg file?
Post by: Alan Clifford on June 10, 2013, 12:19:54 PM
Hi Phil

# default location if not defined already
-mwg:location=Mannheim, Germany
# Don't touch this rule
-mwg:location<$xmp:location
#

What I'm getting with this, if there is no existing location, is an empty location

alan@egremont:jpegs$ exiftool -a -G -location -sub-location  temp1.jpg
[XMP]           Location                        :
[IPTC]          Sub-location                    :

Title: Re: conditional in an arg file?
Post by: Phil Harvey on June 10, 2013, 02:26:30 PM
Right.  That's the work of the -m option.  From the -p option documentation:

            If a specified tag does not exist, a minor warning is issued and
            the line with the missing tag is not printed [or the tag is not
            copied in the context of -tagsFromFile]
.  However, the -f
            option may be used to set the value of missing tags to '-' (but
            this may be configured via the MissingTagValue API option), or the
            -m option may be used to ignore minor warnings and leave the
            missing values empty.

But the good news is you don't need to use the format-string feature at all:  Just remove the "$" symbol to copy the tag directly (instead of interpolating inside a format string), and this problem goes away:

-mwg:location<xmp:location

- Phil
Title: Re: conditional in an arg file?
Post by: Alan Clifford on June 10, 2013, 02:34:31 PM
Hi Phil, thanks for the reply.  As I've had a glass of wine or two, I won't be absorbing that information until tomorrow.

Alan
Title: Re: conditional in an arg file?
Post by: Phil Harvey on June 10, 2013, 02:40:46 PM
Good idea.  Time for a glass of wine here too. :)

- Phil
Title: Re: conditional in an arg file?
Post by: Alan Clifford on June 11, 2013, 09:45:07 AM
I thought I understood the series of events but I am having difficulty here.
# default title if not already defined
-title-=
-title=By the Rhine
-execute   
-objectname<title


I thought that if I put in the -execute, there would be something in title to put into objectname.  But it doesn't create objectname.  It does create it if I run my script again.  Or could this be a buffer problem?


... later

hmm, it works from a command line but not from the bash script.  I'll investigate more.


Title: Re: conditional in an arg file?
Post by: Alan Clifford on June 11, 2013, 10:33:10 AM
It seems to be OK now.  Grrr what have I done.  And I haven't had any wine.

I'm getting an warning message though and I don't know why, but no warning if I run it again

alan@egremont:jpegs$ exiftool -@ keywordsandstuff_rhine1.args -common_args -m -P -overwrite_original_in_place temp2.jpg
Warning: No writable tags set from temp2.jpg
    1 image files updated
    1 image files updated
alan@egremont:jpegs$ exiftool -@ keywordsandstuff_rhine1.args -common_args -m -P -overwrite_original_in_place temp2.jpg
    1 image files updated
    1 image files updated



The argfile is now
# Title, event and location will not be changed if they exist
# Additional keywords added with another program will not be deleted
#
# Used by addstufftojpg.sh
# use exiftool -@ keywordsandstuff.args -common_args -m -P -overwrite_original_in_place photo.jpg
# instead of photo.jpg, use eg *.jpg,  -ext jpg . or  *.mov.xml
#
# validfrom: 2013:05:27 13:09:47+02:00
# validto:   2013:05:27 13:10:21+02:00
#
#
# default location if not defined already
-mwg:location=Mannheim, Germany
# Don't touch this rule
-mwg:location<xmp:location
#
#
# # Don't touch this rule
# -mwg:location<$xmp:location
# -if
# defined $xmp:location
# -execute
# #
# #
# # default location if not defined already
# -mwg:location=Mannheim, Germany
# -if
# not defined $xmp:location
# -execute
# #
#
# default event if not already defined
-event-=
-event=Cycling by the Rhine
#
#
# default title if not already defined
-title-=
-title=By the Rhine
-execute
-objectname<title
#
#
-countrycode=DE
-mwg:country=Germany
-mwg:keywords-=Germany
-mwg:keywords+=Germany
-mwg:keywords-=Rhine
-mwg:keywords+=Rhine


Title: Re: conditional in an arg file?
Post by: Phil Harvey on June 11, 2013, 10:51:17 AM
You will get the warning for your first command if XMP:Location doesn't exist in the file.  No warning if you run again because you have written XMP:Location.

I think that reading FAQ 22 (https://exiftool.org/faq.html#Q22) may help you to understand what is happening.

- Phil