Tuesday, July 14, 2015 InDesign-IDML,groovy

Count up how many spreads in an IDML file

Read designmap.xml in an IDML file, and count up spread elements.

idml

//
// count up how many spreads in an IDML file.
//

@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 =~ /designmap.xml/).find() }

def inputIdmlFile = new File(args[0])
def zipFile = new ZipFile( inputIdmlFile )
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 )

    //<idPkg:Spread>要素を数える.
    println document.rootElement.getChildren('Spread',NS).size()

    reader.close()
}

usage

groovy main input.idml