Copy link to clipboard
Copied
Hi Guys,
I have problem to remove an xml from my indesign file.
If I use this syntax:
1. doc = app.activeDocument;
2. var rootXE = doc.xmlElements.item(0).xmlElements.item(0);
3. rootXE.remove();
the xml file is correctly removed.
But if I delete the third line and I use a button in a dialog window like:
loadXml_button.onClick = function () {
rootXE.remove();
}
the script fail.
if I set an alert in:
loadXml_button.onClick = function () {
alert(rootXE)
rootXE.remove();
}
He return right xml object.
Anyone can help me?
Thanks.
1 Correct answer
Place the following line at the top of the code
#targetengine "test"
and change "dialog" to "palette" in your Window constructor, line no 7 and then try
-Manan
Copy link to clipboard
Copied
hi Daniel,
Cn you post the whole code of the dialog as well and also share which version of Indesign do you see the issue with
-Manan
Copy link to clipboard
Copied
INDESIGN CC18
this is my code
////// Variables
doc = app.activeDocument;
var rootXE = doc.xmlElements.item(0).xmlElements.item(0);
////// Dialog window
var w = new Window ("dialog","Price List Creator");
var xmlGroup = w.add ("group");
var xmlGroupSub = w.add ("group");
w.alignChildren = 'left';
xmlGroup.add ("statictext", undefined, 'XML Action\r-------\rLoaded xml delete old loaded xml', {multiline: true});
var loadXml_button = xmlGroupSub.add ("button", undefined, "Load XML");
loadXml_button.onClick = function () {
var blnFile = verifyXML();
if(blnFile == false){
alert("Unable to process XML file.");
}
else if(blnFile == true){
rootXE.remove();
}
}
var generateGroup = w.add ("group");
generateGroup.add ("statictext", undefined, 'Duplicate Group');
var generateGroupSub = w.add ("group");
var generate_button = generateGroupSub.add ("button", undefined, "Generate");
generate_button.onClick = function () {
docSetup();
}
w.add ("button", undefined, "OK");
w.show ();
Copy link to clipboard
Copied
The code seems to be incomplete, i suppose the code having issues is within the loadXML_button click handler but it contains a method call verifyXML whose definition is not present in the code. If i comment out this code then also i get an error that document operation can't be done when modal dialog is open, on changing the type from dialog to palette it does delete a node from the XML
-Manan
Copy link to clipboard
Copied
Yeah I paste only necessary code, loadXML_button don't have any problem.
My only problem is delete rootXE, because if I put "remove" inside of any function:
rootXE.remove();
this not work.
complete code:
////// Variables
doc = app.activeDocument;
var rootXE = doc.xmlElements.item(0).xmlElements.item(0);
////// Dialog window
var w = new Window ("dialog","Price List Creator");
var xmlGroup = w.add ("group");
var xmlGroupSub = w.add ("group");
w.alignChildren = 'left';
xmlGroup.add ("statictext", undefined, 'XML Action\r-------\rLoaded xml delete old loaded xml', {multiline: true});
var loadXml_button = xmlGroupSub.add ("button", undefined, "Load XML");
loadXml_button.onClick = function () {
var blnFile = verifyXML();
if(blnFile == false){
alert("Unable to process XML file.");
}
else if(blnFile == true){
rootXE.remove();
}
}
var generateGroup = w.add ("group");
generateGroup.add ("statictext", undefined, 'Duplicate Group');
var generateGroupSub = w.add ("group");
var generate_button = generateGroupSub.add ("button", undefined, "Generate");
generate_button.onClick = function () {
docSetup();
}
w.add ("button", undefined, "OK");
w.show ();
//////////
function verifyXML(){
xmlFile = File.openDialog("Please locate the XML data.");
if (xmlFile == null){
return false;
}else{
try{
xmlFile.open("r");
var xmlImport = new XML(xmlFile.read());
if (xmlImport){
return true;
}else{
return false;
}
}
catch(err){
return false;
}
}
}
//////////////////
function docSetup(){
alert("do");
}
Copy link to clipboard
Copied
Place the following line at the top of the code
#targetengine "test"
and change "dialog" to "palette" in your Window constructor, line no 7 and then try
-Manan
Copy link to clipboard
Copied
Great! now work like a charm!!!!
Why?! where is the problem of my code?
#targetengine how work this?
Copy link to clipboard
Copied
You were creating a modal dialog which stops all execution/interaction until it is dismissed, hence it was not able to make changes to the document. When you use the window type as palette it does not stop you from interacting with the InDesign UI so your code is able to make the changes in the document
As regards targetengine, it is used to create a persistent space for your script to execute, so that things from your script are alive even when your code exits, ex your event handler. If you remove this the UI will appear and then disappear simultaneously.
-Manan

