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

时间:2018-10-01 21:56:23

标签: xml tags applescript adobe-indesign

我有一个奇怪的问题。我的简短Applescript有点奏效。我想用XML标记“图像”标记所有矩形(这是Adobe库中称为图像框架的矩形)。

for(i in 1:ncol(obs)){
  colname <- names(obs)[i]
  write.csv(obs[,i], paste0(colname, ".csv"))
}

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

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

2 个答案:

答案 0 :(得分:2)

您当前的条件:

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

问题中的以下代码行如下:

set imageList to every rectangle

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


推荐标准:

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

  

graphic 以任何图形文件格式(包括矢量和位图格式)导入的图形。


解决方案:

下面的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元素的名称(即“图片”)分配给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}
    

    执行图像的实际标记。

答案 1 :(得分:-1)

感谢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
相关问题