将 Apple 标签(Mavericks 标签)导出到 IPTC "Keywords"元数据的 Bash 脚本(使用 'Tag' 和 'ExifTool')

标签 bash tags metadata photos iptc

我的最终解决方案:

将以下脚本(根据 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 的 Tag”并安装“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 元数据中。

到目前为止,我已经能够使用“Tag by 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;"
        } 
    }
}

最佳答案

自动化工作流程

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

注意:

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

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

/上下文菜单/替换选项/引号
并在每个新文档/副本中禁用它。

*更新:

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

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

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

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

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

关于将 Apple 标签(Mavericks 标签)导出到 IPTC "Keywords"元数据的 Bash 脚本(使用 'Tag' 和 'ExifTool'),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30667174/

相关文章:

ios - 元数据被拒绝,因为我没有给他们测试帐户,但这不是我能做的?

bash - 如何跳过 .csv 文件中的重复条目

linux - Inotify linux 监视子目录

java - 在java代码中实例化jsp 2.0的标签

c# - 使用 LINQ 过滤列表

url - 网站元文件的新 url 路径前缀的名称是什么?

bash - 如何指定多行shell变量?

bash - external-downloader 选项在 youtube-dl 中未按预期工作

python - 有没有一种优雅的方法可以在 python 中使用 lxml 来计算 xml 文件中的标记元素?

metadata - EventStore 基础 - 事件元数据/元数据和事件数据有什么区别?