Applescript 将 XML 标签分配给 InDesign 文档中的所有图像

Applescript to assign an XML tag to all images in an InDesign document

我有一个奇怪的问题。我的简短 applescript 有点工作。我想用 XML 标签 "Image".

标记所有矩形(这些是 Adob​​e 库中的图像框架)
set tagName to "Image"
set imageList to {}

tell application "Adobe InDesign CC 2018"
tell active document

    set x to make XML element of first XML element with properties {markup tag:tagName}

    set imageList to every rectangle //there are 4 rectangles
    repeat with i from 1 to number of items in imageList
        tell item i of imageList to markup using x

    end repeat

end tell

end tell

日志显示它正在标记每个矩形。但是当我检查文档时,只有 imageList 中的最后一个矩形实际上显示应用了 XML 标签。

而且我可以取消或停止脚本,取消前的最后一张图片会得到标签。 (也就是说,如果我在处理矩形 2 时取消,则矩形 2 会获取图像,但矩形 1、3 和 4 不会。

您当前的标准:

首先,您当前用于推断 图像 的标准容易出错。 typerectangle 的页面项目不一定等于 image。例如;图像可以放在圆圈内,在这种情况下,它的 type 将是 oval(不是 rectangle)。

您问题中的以下代码行内容如下:

set imageList to every rectangle

还将包括使用 矩形框架工具 创建的任何页面项目 - 在许多 .indd 文件中不会是图像。


推荐标准:

要推断图像,我建议使用 all graphics 而不是 rectangles 来获取列表图像。 InDesign 的 Applescript 词典将 graphic 描述为:

graphic An imported graphic in any graphic file format (including vector and bitmap formats.)


解决方案:

下面的 AppleScript 要点演示了一种使用名为 "Images" 的 XML 标签自动标记所有图像的方法。每个生成的标记图像将添加为文档根 XML 元素的子 XML 元素。

set tagName to "Image"

tell application "Adobe InDesign CC 2018"
  tell active document
    if (count of page items) = 0 then return

    set locked of every layer to false
    set locked of every page item to false

    repeat with currentImage in all graphics
      if associated XML element of currentImage is not equal to nothing then
        untag associated XML element of currentImage
      end if
      make XML element at XML element 1 with properties {markup tag:tagName, XML content:currentImage}
    end repeat

  end tell
end tell

说明

  1. set tagName to "Image" 将 XML 元素的名称(即 "Image")分配给 tagName 变量。

  2. 一行显示:

    if (count of page items) = 0 then return
    

    确保我们在文档不包含页面项目时提前退出脚本。

  3. 行:

    set locked of every layer to false
    set locked of every page item to false
    

    确保解锁所有文档图层和页面项目。如果图像被锁定,则无法对其进行标记。

  4. 行阅读:

    if associated XML element of currentImage is not equal to nothing then
      untag associated XML element of currentImage
    end if
    

    取消标签 图像可能具有的任何现有标签,因为它可能不正确。

  5. 行读:

    make XML element at XML element 1 with properties {markup tag:tagName, XML content:currentImage}
    

    执行图像的实际标记。

感谢 Mark Anthony 在 MacScripter.net 上的建议。它适用于我的需要。我正在处理的所有文档中只有图像(没有其他图形、框架等)

set tagName to "Image"
set imageList to {}

tell application "Adobe InDesign CC 2018"
tell active document

    set x to make XML element of first XML element with properties {markup tag:tagName}

    set imageList to every rectangle
    repeat with rect in imageList
        make XML element at XML element 1 with properties {markup tag:"Image", 
XML content:rect}
    end repeat

end tell

end tell