ExifTool Forum

ExifTool => Newbies => Topic started by: j99mac on July 17, 2014, 08:32:45 AM

Title: add tag if
Post by: j99mac on July 17, 2014, 08:32:45 AM
Is there a way to write a command to add a tac if the tag dose not match.
I am looking to only add the Artist Tag if it is not all ready added with my custom artist name.
I found -if. How to I use it with exiftool -artist="$Artist" "$imagepath"  ?
Title: Re: add tag if
Post by: Phil Harvey on July 17, 2014, 09:50:40 AM
The command could look like this:

exiftool "-artist=Some Name" -if "not $artist or $artist !~ /Some Name/" FILE

(use single quotes instead of double if you are on Mac or Linux)

- Phil
Title: Re: add tag if
Post by: j99mac on July 17, 2014, 11:34:01 AM
This is what I try


artist="artist name"
echo "Enter the file directory of Images Directory "
   read input_variable
exiftool "-artist=artist name" -if "not $artist or $artist !~ /Some Name/" "$input_variable"
exiftool "$input_variable" -r -artist

What is ~ /Some Name/ ? I want to run this on all files in a directory and if it fails then add he artist
Title: Re: add tag if
Post by: Hayo Baan on July 17, 2014, 11:40:51 AM
Hi,

!~/Some Name/ is a perl regular expression for not matching Some Name. If you like to know more about perl regular expressions, google perldoc perlre. Or, if you are on a Mac, Unix, or Linux system, type perldoc perlre on the command line.

Hope this helps,
Hayo
Title: Re: add tag if
Post by: j99mac on July 17, 2014, 11:49:13 AM
This what I tryed

exiftool "-artist=Some Name" -if "not $artist or $artist !~ /Some Name/" "$input_variable" -r
How do I get the script to add the artist tag f it is not all ready in the file
Title: Re: add tag if
Post by: Phil Harvey on July 17, 2014, 12:10:29 PM
I think your problem is with your batch file variables.  If you type the command I gave at the command line, then it should add the Artist "Some Name" if it doesn't already exist.

In my command, "FILE" represents one or more file and/or directory names.

- Phil
Title: Re: add tag if
Post by: j99mac on July 17, 2014, 12:33:51 PM
This is what I get when I entered the command in terminal

exiftool "-artist=Some Name" -if "not $artist or $artist !~ /Some Name/" ~/Desktop/images

exiftool "-artist=Some Name" -if "not $artist or $artist ~/Library/Preferences/com.apple.Terminal.plist /Some Name/" ~/Desktop/images
    1 directories scanned
    1 files failed condition
    0 image files read
Title: Re: add tag if
Post by: Phil Harvey on July 17, 2014, 12:49:49 PM
Your first command should work, but there is something terribly wrong with your second command.

This is what I get:

> exiftool -artist a.jpg
> exiftool "-artist=Some Name" -if "not $artist or $artist !~ /Some Name/" a.jpg
    1 image files updated
> exiftool -artist a.jpg
Artist                          : Some Name
> exiftool "-artist=Some Name" -if "not $artist or $artist !~ /Some Name/" a.jpg
    1 files failed condition


- Phil
Title: Re: add tag if
Post by: j99mac on July 17, 2014, 12:54:15 PM
I want to run the script on a directory of images
Title: Re: add tag if
Post by: Phil Harvey on July 17, 2014, 01:03:07 PM
Yes.  So instead of "a.jpg" in my commands, use the directory name.
Title: Re: add tag if
Post by: j99mac on July 17, 2014, 02:10:52 PM
This is what I get when I changed the jpg to my file path

exiftool "-artist=Some Name" -if "not $artist or $artist !~ /Some Name/" ~/Desktop/Images
exiftool "-artist=Some Name" -if "not $artist or $artist ~/Library/Preferences/com.apple.Terminal.plist /Some Name/" ~/Desktop/Images
    1 directories scanned
    0 image files read
Title: Re: add tag if
Post by: Hayo Baan on July 17, 2014, 03:09:20 PM
You're on a mac, so try changing the quotes to single quotes. This should solve your problem (within double quotes, the shell still tries to do variable expansion and other stuff like interpreting the ! as a shell history reference, that's what's causing the funny path).

Cheers,
Hayo
Title: Re: add tag if
Post by: Phil Harvey on July 17, 2014, 03:23:30 PM
Ah, I get it now.  The "~" in your command was expanded by the shell because you used the wrong quotes.  That is why it looked like you had 2 commands.

Hmm.  But if I try this command on my Mac, in a C-shell I get "artist: Undefined variable.", and in a Bourne shell I get "bash: !~: event not found".  Oh well.  So maybe I don't understand after all.

But using the proper quotes as Hayo and I mentioned will likely solve the problem.

- Phil

Title: Re: add tag if
Post by: Hayo Baan on July 17, 2014, 03:37:46 PM
Quote from: Phil Harvey on July 17, 2014, 03:23:30 PM
Hmm.  But if I try this command on my Mac, in a C-shell I get "artist: Undefined variable.", and in a Bourne shell I get "bash: !~: event not found".  Oh well.  So maybe I don't understand after all.

Hi Phill, on your machine it doesn't find !~ because you didn't have a shell command starting with a ~. I did and it gave the same result as for j99mac (albeit with a different expansion of !~). I'm running the bash shell (the default), and there non-existing vars don't give me an error either (they just expand to an empty string). Interesting that you do get an error. What version of the OS are you on? I'm on 10.9.4.

(Oh, I guess you must have set -o nounset active, which I don't. Hmm, perhaps a smart thing to do...)
Title: Re: add tag if
Post by: Phil Harvey on July 17, 2014, 03:44:32 PM
I'm on 10.8.5 here, but I have set my default shell to tcsh.

- Phil
Title: Re: add tag if
Post by: Hayo Baan on July 17, 2014, 04:19:11 PM
Quote from: Phil Harvey on July 17, 2014, 03:44:32 PM
I'm on 10.8.5 here, but I have set my default shell to tcsh.

Ah, interesting. Any specific reason you switched to tcsh? In my UNIX developer days, I remember using the tcsh myself too; it was just so much better than sh (or bash). But as far as I can tell now, the advantages are mostly gone since bash has evolved so much in the meantime (we're talking 15~20 years ago...). However, please enlighten me if there's something I might be missing out on ;)

Cheers,
Hayo
Title: Re: add tag if
Post by: Phil Harvey on July 18, 2014, 07:12:35 AM
Hi Hayo,

Bash is better if you are used to it, but I am more familiar with tcsh so it is easier for me.  tcsh has one main disadvantage that you can't pipe to stderr separately.  But other than that, I don't have any reason to switch.

- Phil
Title: Re: add tag if
Post by: Hayo Baan on July 18, 2014, 08:39:55 AM
Quote from: Phil Harvey on July 18, 2014, 07:12:35 AM
Bash is better if you are used to it, but I am more familiar with tcsh so it is easier for me.  tcsh has one main disadvantage that you can't pipe to stderr separately.  But other than that, I don't have any reason to switch.

Hi Phil, thanks for the info. So absolutely no reason for me to switch at all. And to me it seems one can live without the fancy redirecting bash offers (for the most time). True, it is nice to separately pipe stdout and stderr as in, exiftool xxxx 2> >(grep -v "errors I'm not interested in" 1>&2) | grep "Output I'm interested in". And still get the (filtered) errors. But when have you really needed this?
Title: Re: add tag if
Post by: Phil Harvey on July 18, 2014, 09:18:35 AM
I only need to use the fancy redirecting occasionally, and I just do a quick "sh" if I need to do this.

- Phil
Title: Re: add tag if
Post by: j99mac on July 18, 2014, 04:22:51 PM
I am doing it on a mac running OS X 10.6
Title: Re: add tag if
Post by: Hayo Baan on July 18, 2014, 06:18:23 PM
Quote from: j99mac on July 18, 2014, 04:22:51 PM
I am doing it on a mac running OS X 10.6

Ok, then changing the quotes to single quotes (') should definitely work for you. Have you already tried that?

Cheers,
Hayo