Saturday, July 18, 2015 InDesign-IDML,groovy

Count up how many textframes in an IDML file

Read Spreads/*.xml in an IDML file, and count up textframe elements.

idml

@Grab(group='net.lingala.zip4j', module='zip4j', version='1.3.2')
import net.lingala.zip4j.core.ZipFile
import net.lingala.zip4j.exception.ZipException
import net.lingala.zip4j.model.FileHeader

@Grab(group='org.jdom', module='jdom2', version='2.0.5')
import org.jdom2.*
import org.jdom2.input.*
import org.jdom2.output.*


def NS = Namespace.getNamespace('idPkg', 'http://ns.adobe.com/AdobeInDesign/idml/1.0/packaging')

def filter = { (it.fileName =~ /Spreads\//).find() }

def inputIdmlFile = new File(args[0])
def zipFile = new ZipFile( inputIdmlFile )

def textFrameCount = 0
zipFile.getFileHeaders().findAll( filter ).each { FileHeader fileHeader->
    println fileHeader.fileName

    def is = zipFile.getInputStream(fileHeader)
    def reader = new BufferedReader( new InputStreamReader(is,'UTF-8') )

    def document = new SAXBuilder().build( reader )

    //<TextFrame>要素を数える.
    textFrameCount = textFrameCount + document.getDescendants().findAll( { it instanceof Element && it.name=='TextFrame' } ).size()

    reader.close()
}

println textFrameCount

usage

groovy main input.idml