Author Topic: Set/add to an image's Keywords tag from a file  (Read 5286 times)

agill

  • Jr. Member
  • **
  • Posts: 28
Set/add to an image's Keywords tag from a file
« on: February 28, 2014, 07:42:36 PM »
I'm using exiftool on a Mac (Snow Leopard OS X 10.6.8 ). Successfully downloaded/installed and managed to set up Automator apps to do some of what I need, e.g. I've used the following from Run Shell Script to add Keywords to an image file, where the exiftool command is sandwiched between the "do" and "done", and the "$f" on the end of it is what brings in the file I'm pointing at in the prior Ask For Finder Items section of the workflow - it works fine (the Shell is the default /bin/bash) -

for f in "$@"
do
   exiftool -Keywords+=newkeyw "$f"
done

Based on this I thought I could set up that same automator app to take in what I want to add to the keywords tag from a file. BUT - I can't seem to work out the syntax to do this as I keep getting errors. I've now tried formatting the exiftool command SO many different ways I'm fairly well lost, so I'm asking what the exact format to do this should be, e.g. I know I need to use quotes, but is that single or double quotes, and where exactly are they to be located, e.g. around the filename as well, or only after the =, or ...? Assuming I'm correct with the automator Run Shell Script then could someone help me with the exiftool command, e.g. I've tried variations of:

exiftool '-Keywords+<=filename' "$f" where the file named filename is in the directory I'm pointing at for the photos

I've also tried using an absolute path such as
exiftool '-Keywords+<=' /Users/mymac/filename "$f"

and much else, and only succeed in getting errors.

For all of these I've tried single/double quotes, varying the position of the quotes, varying the file location, name, extension (even removing that) and so much else it's hard to remember.

I figure I must be missing something, e.g. do I need the %d, %f as well as the actual names, or ...? Maybe even the type of file I need is important - for now I've just used a standard text file, but don't know if that's correct.

Any guidance would be greatly appreciated.
Thanks
« Last Edit: February 28, 2014, 08:18:54 PM by agill »

Phil Harvey

  • ExifTool Author
  • Administrator
  • ExifTool Freak
  • *****
  • Posts: 14896
    • ExifTool Home Page
Re: Set/add to an image's Keywords tag from a file
« Reply #1 on: February 28, 2014, 08:20:12 PM »
for f in "$@"
do
   exiftool -Keywords+=newkeyw "$f"
done

Why do people insist on calling exiftool once for each file in a shell script?  This is very inefficient, and probably harder to script.  Instead, just pass all the file names to exiftool.  Perhaps nobody reads the documentation:

FILE is one or more source file names, directory names, or "-" for the standard input.

Quote
exiftool '-Keywords+<=filename' "$f" where the file named filename is in the directory I'm pointing at for the photos

I've also tried using an absolute path such as
exiftool '-Keywords+<=' /Users/mymac/filename "$f"

and much else, and only succeed in getting errors.\

Knowing the actual errors might help.

Quote
For all of these I've tried single/double quotes, varying the position of the quotes, varying the file location, name, extension (even removing that) and so much else it's hard to remember.

Unfortunately I don't have any experience with the automator, so I don't know how you need to quote commands in automator scripts, or anything about how the working directory is set.

Quote
I figure I must be missing something, e.g. do I need the %d, %f as well as the actual names, or ...?

I suggest getting the command working from the Terminal before moving to an automator script.  %d represents the directory of the original image file.  So you would only want to use this if your keywords file is in the same directory as the image.  Otherwise a full path is probably advisable.

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

agill

  • Jr. Member
  • **
  • Posts: 28
Re: Set/add to an image's Keywords tag from a file
« Reply #2 on: February 28, 2014, 08:33:27 PM »
Thanks for the prompt responses. Okay, I'll try it on the command line before I set it up in Automator.

FYI, in the prior Ask For Finder Items, I'd set up so it can take in multiple files/folders meaning that the "$f" will be able to handle that and pass them onto exiftool.

agill

  • Jr. Member
  • **
  • Posts: 28
Re: Set/add to an image's Keywords tag from a file
« Reply #3 on: February 28, 2014, 08:44:12 PM »
Seems I'm pointing at the wrong place. I tried it on the command line as follows:

exiftool '-Keywords+<=' \Users\agill\0Temp\keyw.txt SL100D_20140126_131137_0004.JPG
Warning: No value to add or delete in IPTC:Keywords
Nothing to do.

with the warning shown. I did a read of the meta data on the image, and there are already keywords there. I guess it's not in the IPTC however, maybe the EXIF?

I guess my questions now needs to be how do I point to the right Keywords tag? Or is it that I have the command format incorrect?

Thanks,
Adrian

StarGeek

  • Global Moderator
  • ExifTool Freak
  • *****
  • Posts: 2569
Re: Set/add to an image's Keywords tag from a file
« Reply #4 on: February 28, 2014, 09:40:56 PM »
Seems I'm pointing at the wrong place. I tried it on the command line as follows:

exiftool '-Keywords+<=' \Users\agill\0Temp\keyw.txt SL100D_20140126_131137_0004.JPG
Warning: No value to add or delete in IPTC:Keywords
Nothing to do.

Try
exiftool '-Keywords+<=\Users\agill\0Temp\keyw.txt' SL100D_20140126_131137_0004.JPG
though I don't think that will quite work.  You may need to add the -Sep option as well if there is more than one keyword in the keyw.txt file.  Otherwise, ExifTool will add the entire contents of the keyw.txt file into one keyword.  If the keyw.txt file is one keyword per line, for example, then you need to set -Sep to a newline, which I'm not sure how to do.  It might be possible on the mac (see FAQ 21) but I was unable to do it on my windows machine.
Troubleshooting hints:
* When posting, include your OS, Exiftool version, and type of file you're processing (MP4, JPG, etc).
* Double all percent signs (%) in a Windows batch file.
* If your GPS coords are negative, make sure and set the GpsLatitudeRef and GpsLongitudeRef tags correctly.

agill

  • Jr. Member
  • **
  • Posts: 28
Re: Set/add to an image's Keywords tag from a file
« Reply #5 on: February 28, 2014, 09:52:33 PM »
Thanks for responding StarGeek. I tried your advised format as follows;
exiftool '-Keywords+<=\Users\agill\0Temp\keyw.txt' SL100D_20140126_131137_0004.JPG
Error opening file \Users\agill\0Temp\keyw.txt
Nothing to do.

With the error you can see.
Seems like it's maybe now pointing to the file, but perhaps doesn't like the format of it. It's just a standard text file with one entry and no extra lines. I would think it such would be okay, but it seems not to be the case. For now it seems -sep may not be needed as I'm not past the first hurdle, which is to get the command to work.

Any further advice is much appreciated.
Thanks,
Adrian

StarGeek

  • Global Moderator
  • ExifTool Freak
  • *****
  • Posts: 2569
Re: Set/add to an image's Keywords tag from a file
« Reply #6 on: February 28, 2014, 10:30:44 PM »
Wait, shouldn't it be /Users/agill/0Temp/keyw.txt on the Mac?  I have no experience with the Mac command line, so I don't know how flexible it is.    And is that the exact path to the keyw.txt file (is it case-sensitive?)
Troubleshooting hints:
* When posting, include your OS, Exiftool version, and type of file you're processing (MP4, JPG, etc).
* Double all percent signs (%) in a Windows batch file.
* If your GPS coords are negative, make sure and set the GpsLatitudeRef and GpsLongitudeRef tags correctly.

agill

  • Jr. Member
  • **
  • Posts: 28
Re: Set/add to an image's Keywords tag from a file
« Reply #7 on: February 28, 2014, 10:32:57 PM »
Some success with moving the keyw.txt file to the same folder as the image, then typing
adrian-mac:201401 agill$ exiftool '-Keywords+<=%dkeyw.txt' SL100D_20140126_131137_0004.JPG
    1 image files updated

From the meta data read I did following that, the new keyword has been added to the existing ones.

It would be nice to know how to correctly formulate an absolute path however, so I didn't always have to try and move the file around, but at least this is a start.

Thanks for the suggestions so far, and any more are appreciated,
Adrian

agill

  • Jr. Member
  • **
  • Posts: 28
Re: Set/add to an image's Keywords tag from a file
« Reply #8 on: February 28, 2014, 10:40:58 PM »
StarGeek you're right! I had the slashes the wrong way round - kind of confusing as when you change directories in Mac's Unix via Terminal using the cd command, it expects the slash I'd put, yet when defining the path, it expects the other slash.

Anyway, you spotted the problem, I tried it, and can now update the tag accordingly from the file, defining the absolute path.

Once again, many thanks for your help, and I should now be able to put it all into an Automator app - great!

Phil Harvey

  • ExifTool Author
  • Administrator
  • ExifTool Freak
  • *****
  • Posts: 14896
    • ExifTool Home Page
Re: Set/add to an image's Keywords tag from a file
« Reply #9 on: March 01, 2014, 06:22:22 AM »
Thanks StarGeek.

'cd' should also accept forward slashes.

The easy thing to do is just drag and drop the file onto the Terminal window instead of typing the full path.

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

agill

  • Jr. Member
  • **
  • Posts: 28
Re: Set/add to an image's Keywords tag from a file
« Reply #10 on: March 01, 2014, 11:39:17 PM »
Many thanks to both StarGeek and Phil.

FYO, terminal on the Mac (at least under 10.6.8 SL which is what I have) does NOT accept the cd command with the "/" - that returns "No such file or directory". Only the "\" is acceptable, yet when defining the path it's necessary to use the "/".

Thanks for the tip about dragging the file to the Terminal window - that would make it a lot easier. But it's still great to know how to define the path as I'm now building it all into an Automator app which requires it. Maybe when I've done that and run it successfully in various forms (so far it's working well) I could put it in the Developers section. Though not sure if just using Automator counts as running exiftool from within another app - the main point is if it would be helpful to someone, I'd be happy to post details.

Phil Harvey

  • ExifTool Author
  • Administrator
  • ExifTool Freak
  • *****
  • Posts: 14896
    • ExifTool Home Page
Re: Set/add to an image's Keywords tag from a file
« Reply #11 on: March 02, 2014, 07:25:34 AM »
FYO, terminal on the Mac (at least under 10.6.8 SL which is what I have) does NOT accept the cd command with the "/" - that returns "No such file or directory". Only the "\" is acceptable, yet when defining the path it's necessary to use the "/".

An example is required.  The forward slash "/" is the separator that must be used between directory names.  The backward slash "\" is the escape character that is used to escape special characters within a directory name (which isn't necessary if the directory name is properly quoted).

Quote
Maybe when I've done that and run it successfully in various forms (so far it's working well) I could put it in the Developers section.

That would be appreciated.

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

agill

  • Jr. Member
  • **
  • Posts: 28
Re: Set/add to an image's Keywords tag from a file
« Reply #12 on: March 04, 2014, 11:29:58 PM »
An example of the (possibly unnecessary) use of backslash with directories is the change directory command - cd.

Assuming the current focus is a directory called User, which has various subdirectories, including one called Photos, in Terminal on a Mac typing "cd \Photos" (without the quotes), results in moving from the directory it's in (User) to the sub-directory Photos. The same effect can be achieved by typing "cd Photos". If you type "cd /Photos" it won't change directory, but will return the message "No such file or directory". Note there needs to be a space between cd and whatever follows.

To declare the path using the cd command, i.e. to go to lower levels of the directory structure, you'd need to type the following "cd \Photos/2014/201401", which would move to the 201401 subdirectory of 2014, which is itself a subdirectory of Photos. The same can be achieved by typing "cd Photos/2014/201401".

So the cd command in Terminal on the first instance of moving to a new subdirectory requires either \ or no slash, and will not accept /. However, to declare a full path involving more than one subdirectory, the / must be used in all instances of the directory name after the first, so "cd /Photos/2014/201401" will not work, but "cd \Photos/2014/201401" or "cd Photos/2014/201401" will work.

When I first used -TAG<=FMT in exiftool, I incorrectly used \ to declare the path when it should have been /. Please note that / should be used from within exiftool even for the first directory, e.g. exiftool '-Keywords+<=/Users/Myusername/Temp/keyw.txt' - that will direct exiftool to add to the keywords tag in a file the contents of the file keyw.txt. I'm not sure if it would work to just put a space and no / between the = and Users as I didn't try that.

I've put together some Automator apps that successfully read tags, edit tags, use separators and so on via exiftool. Before putting them onto the developers site however, I want to be sure they're fully operational so will try them out in all variations first. When I do put them up I hope they prove helpful.
I understand there are already GUIs available to purchase that run under Mac OS X and use exiftool to carry out the work, avoiding the need to use the command line interface. From what I've seen however these are for versions later than I have, e.g. Lion (OS X 10.7) onwards, while I'm using Snow Leopard 10.6.8. Automator is also freely available with Mac OS X, and should span different versions of the OS if upgrades are made.

Hope this is both clear and helpful,
Adrian

Phil Harvey

  • ExifTool Author
  • Administrator
  • ExifTool Freak
  • *****
  • Posts: 14896
    • ExifTool Home Page
Re: Set/add to an image's Keywords tag from a file
« Reply #13 on: March 05, 2014, 07:36:53 AM »
Hi Adrian,

Assuming the current focus is a directory called User, which has various subdirectories, including one called Photos, in Terminal on a Mac typing "cd \Photos" (without the quotes), results in moving from the directory it's in (User) to the sub-directory Photos. The same effect can be achieved by typing "cd Photos". If you type "cd /Photos" it won't change directory, but will return the message "No such file or directory". Note there needs to be a space between cd and whatever follows.

Yes.  A directory specification that begins with a slash is an absolute path.  In this example, the absolute path is /Users/YOU/Photos.  If a directory does not start with a slash, then it is relative to the current directory.  The current directory may be obtained by the "pwd" command.  In your example, the current working directory is "/Users/YOU", and when you type just "Photos" you will be referring to the "/Users/YOU/Photos" directory.

The backslash in your example is ignored since it escapes a character that doesn't need escaping.

I hope this makes sense.

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

agill

  • Jr. Member
  • **
  • Posts: 28
Re: Set/add to an image's Keywords tag from a file
« Reply #14 on: March 06, 2014, 09:30:58 PM »
It does make sense, thanks Phil.

I've uploaded a walkthrough and attachment to show how I got exiftool to run within Automator. It's only for certain, limited aspects of exiftool, but I think should be enough for others to work out their own.

Thanks again,
Adrian