3

我的最终解决方案:

将以下脚本(从 damienfrancois 答案修改)保存到文件,例如“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

不要忘记通过执行以下操作使脚本可执行

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

安装“JDBerry 的标签”并安装“Phil Harvey 的 ExifTool”。使用终端转到选择的目录。该目录必须只有“.jpg”、“.JPG”、“.tif”和“.JPEG”文件,脚本将递归遍历根目录但不会更改其他文件类型。成功的输出应该是这样的:

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

该脚本会将原始文件的副本保留为“img.jpg_original”。所有 Apple 标签将从最终文件“img.jpg”中删除。请记住在确定一切正常后删除“_original”文件(我使用了 Spotlight)。

我原来的问题:

我经常在 OS X 上使用终端来执行 rysnc、ssh 等任务,但我仍然是 bash 脚本的完全菜鸟。客户拥有大量使用 OS X 标签标记的图像。我需要将这些标签附加到 IPTC 元数据中。

到目前为止,我已经能够使用“JDBerry 标记”执行以下操作

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

我还能够使用 Phil Harvey 的 ExifTool 执行以下操作

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

是否有任何 Bash 脚本专家愿意并且能够帮助我?我正在考虑以下内容(用伪代码编写):

$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;"
        } 
    }
}
4

3 回答 3

2

这是一个未经测试的解决方案,应该适合您。不过它可能需要抛光。

for file in /path/to/img/*.jpg # 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
  end
end

您可能可以将内部for循环和read命令合并为一个while循环,但在我看来会牺牲可读性。

于 2015-06-05T14:30:08.203 回答
2

自动化工作流程

你好,我试图用它制作一个自动化工作流程

小心:

默认情况下,Apple 在 Mavericks 中启用了智能引号,这意味着默认情况下它会自动更正和崩溃您的代码

在 Automator 中,您必须右键单击“运行 shell 脚本”文本框。

/上下文菜单/替换选项/智能引号
并在每个新文档/重复项中禁用它。

*更新:

您还可以通过在 系统偏好设置/键盘/文本/SmartQuotes(下拉菜单中的最低项目)中将自动更正设置为“直引号”来禁用它,然后可以选择禁用自动更正。这将永久更改 automator 中的默认设置。

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

首先,我制作了一个“询问查找器项目”框:

类型:文件夹允许多选

然后我添加了一个“运行 shell 脚本”框:

外壳:bin/bash
传递输入:作为参数

并在选项中:启用工作流运行时显示此操作。

这是代码的另一个迭代:

    #!/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

我是新手,所以要小心处理。

如果您找到更好的解决方案,请随时修复。我很高兴听到你的方法。

感谢所有之前的演讲者发帖。

于 2017-03-03T14:47:59.630 回答
0

这已在上面的答案中进行了修改

好的,这就是我如何让它为那些想要详细信息的 Mac 用户工作的。

将以下脚本(从 damienfrancois 答案修改)保存到文件,例如“photos.sh”。

for file in *.jpg # 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

不要忘记通过执行以下操作使脚本可执行

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

从https://github.com/jdberry/tag/安装 Tag,并从http://www.sno.phy.queensu.ca/~phil/exiftool/安装 ExifTool 。使用终端转到选择的目录。该目录必须只有“.jpg”文件,脚本不会递归迭代或更改其他文件类型。成功的输出应该是这样的:

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

该脚本会将原始文件的副本保留为“img.jpg_original”。所有 Apple 标签将从最终文件“img.jpg”中删除。请记住在确定一切正常后删除“原始”文件(我使用了 Spotlight)。

于 2015-06-08T09:33:31.460 回答