質問

I've created a script where I select the folder that holds the xml files I want to import, create the document and insert these XML files, but my script ends with the following message, which is not very helpful: "Execution finished. Result: undefined".

Any help will be appreciated.

var myDocument = app.documents.add();

var MyFolderWithFiles = Folder.selectDialog ("Choose a folder");
var myFiles = MyFolderWithFiles.getFiles("*.xml");
for(var i = 0; i < myFiles.length; i++) {
    myDocument.importXML(myFiles[i]);
 }
役に立ちましたか?

解決

This question is a bit old but doesn't have an accepted answer so I'll see if I can help.

Your code for creating the new document looks to me like you're just creating a new blank document. How does the XML you're importing know where to go? One thing that might help is after you've completed the import check the "Structure" of the XML within the document.

var myDocument = app.documents.add();

When I import XML I don't create a new blank document, I create a new document from a template that has a predefined structure so InDesign knows where to place each XML node within your template. Here is a decent reference to help get started setting up your INDT file.

var myDoc = app.open( '//path/to/myTemplate/myTemplate.indt', OpenOption.OPEN_COPY );
// NOTE: I'm running against an InDesign server, if you're running against your ID GUI then you'll need an extra param on the app.open() call like in the following line
var myDoc = app.open( '//path/to/myTemplate/myTemplate.indt', true, OpenOption.OPEN_COPY ); // This is probably what you'll need to use
// The extra param for showingWindow should be true if running against the ID GUI, this feature is NOT available when executing against the ID server

Also I set my XML import preferences -- your's may differ from mine -- so InDesign knows what to do, say in case of an unmatched XML node in contrast to your XML import structure. For example here is one of the xmlImportPreference sets I use from time to time.

with ( myDoc.xmlImportPreferences )
{
    allowTransform = false;
    createLinkToXML = false;
    ignoreUnmatchedIncoming = false;
    ignoreWhitespace = true;
    importCALSTables = false;
    importStyle = XMLImportStyles.mergeImport;
    importTextIntoTables = false;
    importToSelected = false;
    removeUnmatchedExisting = true;
    repeatTextElements = true;
}

Something else to look into with the help of creating your XML structure for your INDT is the use of a DTD (Document Type Definition) file. Here is another good reference for help with InDesign and XML, it also goes into some detail about DTD files. An example of a simple DTD file might be something like this.

<!ELEMENT Root (Root*)>
<!ELEMENT Customer(name, Address*)>
<!ELEMENT name(#PCDATA)>
<!ELEMENT Address(street, city, state, zip)>
<!ELEMENT street(#PCDATA)>
<!ELEMENT city(#PCDATA)>
<!ELEMENT state(#PCDATA)>
<!ELEMENT zip(#PCDATA)>

In XML that would represent something like this:

<Root>
    <Customer>
        <name>Billy Bob</name>
        <Address>
            <street>123 Test Ave</street>
            <city>Testville</city>
            <state>IA</state>
            <zip>12345</zip>
        </Address>
    </Customer>
</Root>

I hope this helps point someone in the right direction struggling with ID and XML. It can be tricky and temperamental at times.

I did ramble a little bit so if someone finds this helpful but still cannot quite get it to work I can elaborate on a specific issue all you have to do is ask! ;) HAPPY CODING!

他のヒント

"Execution finished. Result: undefined"

it means the files are imported succesfully.

and indesign doesn't return any value for this statement...

I second Sulaiman_J. The message indicates everything went fine. The fact you don't have anything imported can be related to the xml import options. You should check with a manual xml import and display xml options. Check if the "Only import elements that match existing structure". Because if this option is checked and as you are working on new documents, there won't be existing structure and the import will not inject any nodes. That's why you may not have injected contents even if InDesign says it did.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top