//DESCRIPTION:Add Text before or after an XML Element //Contact: Gregor Fellenz - http://www.publishingx.de var px = { projectName:"AddTextToXMLElement.jsx", version:"2017-05-22-v1.0", defaultTagName:"Root", defaultTextBefore:"Prefix", defaultTextAfter:"Suffix", runWithUndo:true, // Verwaltung debug:false } if (app.extractLabel("px:debugID") == "Jp07qcLlW3aDHuCoNpBK_Gregor") { px.debug = true; } main(); function main() { if (app.documents.length == 0) { alert("Kein Dokument geöffnet", "Hinweis"); return; } if (app.layoutWindows.length == 0) { alert("Kein Dokument sichtbar", "Hinweis"); return; } var dok = app.documents[0]; var ial = app.scriptPreferences.userInteractionLevel; var redraw = app.scriptPreferences.enableRedraw; if (px.debug) { if (checkDok(dok)) { if (px.runWithUndo) { app.doScript(processDok, ScriptLanguage.JAVASCRIPT, [dok], UndoModes.ENTIRE_SCRIPT, px.projectName); } else { processDok(dok); } } } else { try { if(dok && dok.isValid) { app.scriptPreferences.userInteractionLevel = UserInteractionLevels.NEVER_INTERACT; app.scriptPreferences.enableRedraw = false; if (checkDok(dok)) { if (px.runWithUndo) { app.doScript(processDok, ScriptLanguage.JAVASCRIPT, [dok], UndoModes.ENTIRE_SCRIPT, px.projectName); } else { processDok(dok); } } } } catch (e) { alert(e); } finally { app.scriptPreferences.userInteractionLevel = ial; app.scriptPreferences.enableRedraw = redraw; app.findGrepPreferences = NothingEnum.NOTHING; app.changeGrepPreferences = NothingEnum.NOTHING; } } } /* Functions with main functionality */ function checkDok(dok) { return true; } function processDok(dok) { if (px.runWithUndo) { dok = dok[0]; } var win = new Window("dialog", "Add Text before or after an XML Element" ); with (win) { win.pConfig = add( "panel", undefined, "Enter text and pick an element"); win.pConfig.preferredSize.width = 400; win.pConfig.alignChildren = ['left', 'top']; win.pConfig.spacing = 10; with (win.pConfig) { win.pConfig.gConfig = add( "group"); win.pConfig.gConfig.margins = [0,10,0,0]; win.pConfig.gConfig.spacing = 10; with (win.pConfig.gConfig) { win.pConfig.gConfig.beforeText = add( "edittext", undefined, px.defaultTextBefore); win.pConfig.gConfig.beforeText.preferredSize.width = 100; win.pConfig.gConfig.tagNameDropdown = add("dropdownlist", undefined, dok.xmlTags.everyItem().name); win.pConfig.gConfig.tagNameDropdown.selection = px.defaultTagName; win.pConfig.gConfig.tagNameDropdown.preferredSize.width = 150; win.pConfig.gConfig.afterText = add( "edittext", undefined, px.defaultTextAfter); win.pConfig.gConfig.afterText.preferredSize.width = 100; } } // Steuerung Ok/Cancel win.groupStart = add("group"); win.groupStart.preferredSize.width = 400; win.groupStart.alignChildren = ['right', 'center']; win.groupStart.margins = 0; with (win.groupStart) { win.groupStart.butOk = add( "button", undefined, "Start"); win.groupStart.butOk.onClick = function() { win.close(1); } win.groupStart.butCancel = add( "button", undefined, "Cancel"); win.groupStart.butCancel.onClick = function(){ win.close(2); } } } win.center(); var res = win.show(); if (res == 2) return; var root = dok.xmlElements[0]; addStandardNameSpace(root); var nodes = root.evaluateXPathExpression("//" + win.pConfig.gConfig.tagNameDropdown.selection.text); var beforeText = win.pConfig.gConfig.beforeText.text.replace(/\\r/, "\r").replace(/\\n/, "\n").replace(/\\t/, "\t"); var afterText = win.pConfig.gConfig.afterText.text.replace(/\\r/, "\r").replace(/\\n/, "\n").replace(/\\t/, "\t"); for (var n = 0; n < nodes.length; n++) { var node = nodes[n]; node.insertTextAsContent (beforeText, XMLElementPosition.ELEMENT_START); node.insertTextAsContent (afterText, XMLElementPosition.ELEMENT_END); } } // Add standard namespaces for using evaluateXPathExpression() on #InDesign #XML subtrees function addStandardNameSpace(xmlContainer) { if( !xmlContainer.xmlAttributes.itemByName("xmlns:xml").isValid ) { xmlContainer.xmlAttributes.add( "xmlns:xml", "http://www.w3.org/XML/1998/namespace" ); } if( !xmlContainer.xmlAttributes.itemByName("xmlns:aid").isValid ) { xmlContainer.xmlAttributes.add( "xmlns:aid", "http://ns.adobe.com/AdobeInDesign/4.0/"); } if( !xmlContainer.xmlAttributes.itemByName("xmlns:aid5").isValid ) { xmlContainer.xmlAttributes.add( "xmlns:aid5", "http://ns.adobe.com/AdobeInDesign/5.0/"); } return xmlContainer; }