bashtagsmetadataphotosiptc

Bash Script that exports Apple Tags (Mavericks Tags) to IPTC "Keywords" metadata (using 'Tag' and 'ExifTool')


My Final Solution:

Save the following script (modified from damienfrancois answer) to a file such as "photos.sh".

IFS=$'\n';
for file in $(find ./ -name '*.jpg' -or -name '*.JPG' -or -name '*.tif' -or -name '*.JPEG'); # iterate over each file
do
  taglist="$(tag --no-name --list "$file")" # get a comma-separated list (string) of tags
  IFS=',' read -ra tagarray <<< "$taglist" # convert that string to an array
  for tag in "${tagarray[@]}" # loop over that array of tags
  do
    exiftool -Keywords+="$tag" "$file" # add tag to file
  done
done

Don't forget to make the script executable by doing the following

chmod 755 /path/to/script/dir/photos.sh

Install "Tag by JDBerry" and also install "ExifTool by Phil Harvey". Use the Terminal to go to directory of choice. This directory must only have ".jpg", ".JPG", ".tif" and ".JPEG" files inside, the script will iterate recursively through the root directory but won't change other file types. Successful output should look something like this:

~ > cd /path/to/images/dir/
/path/to/images/dir/ > /path/to/script/dir/photos.sh
    1 image files updated
    1 image files updated

The script will keep a copy of the original file as "img.jpg_original". All Apple Tags will be removed from the final file "img.jpg". Remember to delete the "_original" files after you're sure everything worked (I used Spotlight).

My Original Question:

I frequently use the terminal on OS X for tasks like rysnc, ssh etc, but am still a complete noob at bash scripting. A client has a ton of images that they have been tagged using OS X tags. I need to append these tags into the IPTC metadata.

So far I have been able to do the following using "Tag by JDBerry"

~ > tag --no-name --list /path/to/img/example.jpg 
    Orange,Red

I have also been able to do the following with ExifTool by Phil Harvey

~ > exiftool -Keywords+='Orange' /path/to/img/example.jpg
    1 image files updated
~ > exiftool -Keywords+='Red' /path/to/img/example.jpg
    1 image files updated

Are there any Bash Scripting gurus willing and able to help me out? I was thinking of something along the following (written in pseudocode):

$imgDir[] = function that adds all images in directory to array;
foreach ($imgDir as $pathToImg) {
    $tagsArray[] = function that executes "tag --no-name --list $pathToImg" and saves return value;
    $numberOfTags = count($tagsArray);
    if ($numberOfTags != NULL) {
        for ($i = 1; $i <= $numberOfTags; $i++) {
            function that executes "exiftool -Keywords+='$tagsArray[$i-1]' $pathToImg;"
        } 
    }
}

Solution

  • Automator Workflow

    Hello, I tried to make an automator workflow out of it.

    Watch out:

    Apple enabled smart quotes in Mavericks by default, which means it is auto-correcting and crashing your code by default.

    In Automator you have to right click in the "run shell script" text box.

    /contextual menu/ substitution options/smart quotes
    and disable it with every new document/duplicate.

    *update:

    you can also disable it by Setting Auto Correct to "Straight Quotes" in System Preferences/Keyboard/Text/SmartQuotes (the lowest item in the drop down) and optionally disabling Auto Correct afterwards. This permanently changes the default in automator.

    https://derflounder.wordpress.com/2014/02/01/disabling-smart-quotes-in-mavericks/

    first I made a "ask for finder items" box:

    type: folder allow multiple selections

    then I added a "run shell script" box:

    shell: bin/bash
    pass input: as arguments

    and in the options: show this action when the workflow runs enabled.

    and here is another iteration of the code:

        #!/bin/bash
    
    cd "$1" #changes directory to selected folder
    IFS=$'\n';
    for file in $(find ./ -name '*.jpg' -or -name '*.jpeg' -or -name '*.JPG' -or -name '*.pdf' -or -name '*.tiff'); #added all file types i liked.
    do
        taglist="$(/usr/local/bin/tag --no-name --list "$file")" # inserted full file paths /usr/local/bin/tag to make automator find it. these paths can vary. 
        IFS=',' read -ra tagarray <<< "$taglist"
        for tag in "${tagarray[@]}" 
        do
            /usr/local/bin/exiftool -Keywords-="$tag" "$file" -Keywords+="$tag" "$file" -overwrite_original_in_place --a 
    #inserted full file paths /usr/local/bin/exiftool to make automator find it. these paths can vary
    #added -overwrite_original_in_place = updating original image:Can possible DESTROY! your data. Use with caution! 
    #added -Keywords-="$tag" to prevent duplicate keywords: removes allready assigned keywords first.
            done 
    done
    

    I'm a newbie so handle with care.

    If you find a better solution feel free to fix. I would be happy to hear your approach.

    Thanks to all the previous speakers for posting.