Copy link to clipboard
Copied
I'm using InDesign CS6 and I'm trying to build my first Javascript within InDesign. I want to be able to create an entire XML structure whenever someone tags a frame. Ideally, it would work something like this: When text frame.tagged(record/item/@name="copy"), create Structure. I'm reading through a few books and trying to wr4ap my head around how InDesign scripting works, but I haven't been able to translate the examples into this thing I need to do. I appreciate any help. Thank you - Nathan
Copy link to clipboard
Copied
Hi
It seems tour issue covers two topics here. On one side creating à structure. On thé other side ensure that this structure is of à certain model.
Creating à structure is all but difficult. Thé simpliest way is to place à XML file that you ça' even créate on thé fly based on a XML object. Thé second topic is thé need of having à spécifications structure given that thé userbmay change things or wjatever. This is where you may want to use DTD validation so you can check thé structure validity. Besides if you load à DTD file, that will also créateur thé tags you need and thé usée wont ne ablz ro esit or remove thevtags.
Sorry for thé typos. Typing on m'y french phone ![]()
Copy link to clipboard
Copied
That is great insight, and I appreciate it. I know what I am trying to do logically, but I am having a hard time translating it into ExtendScript syntax. I have an XML structure set up that I am planning on adding to the structure pane once a user tags a frame using the tag "copy". The user needs to be able to edit these XML elements in the structure pane because we won't have access to all of the necessary values pre-build.
Copy link to clipboard
Copied
Well, if you need to react on the event of "tagging", you would probably need to listen on the AfterSelectionAttributesChanged as there isn't any OnTagging Event that I know of. I am not sure what your structure needs are but here is a sample of sth taht coul be used :
//Adding a targetengine instruction so the event listener is active for the whole session
#targetengine "onCopyTagging"
//Our main function
//Will add the listener if needed
var main = function() {
var evName = "OnCopyTagging";
//Referencing our eventlisteners
var ev = app.eventListeners.item ( evName );
//Declaring our event handler
var eh = function(evt) {
var xe, xa, sel, n, prop;
//Exit if non valid context
if (!app.documents.length
|| !app.selection.length ) return;
//Looping through selection items if any
n = app.selection.length;
while ( n-- ) {
sel = app.selection
; //Checking that the nth selection item is a text frame with a COPY tag.
//If so we are adjusting the structure through our addStructure method
sel.properties.parentStory
&& (sel instanceof TextFrame )
&& ( sel.properties.associatedXMLElement instanceof XMLElement )
&& sel.associatedXMLElement.markupTag.name == "COPY"
&& addStructure ( sel.associatedXMLElement, sel );
}
}
//Adding the listener only if not instantiated yet
if ( !ev.isValid ) {
ev = app.eventListeners.add("afterSelectionAttributeChanged", eh );
ev.name = evName;
}
}
//Adding a simple "id" xml attribute to the xmlElement
//The value is the id of the text frame.
//Do whatever you want from there
var addStructure = function ( xe, pi ) {
//Referencing the xml attribute
var xa = xe.xmlAttributes.item ( "id" ),
pid = String ( pi.id );
//Adding the attribute if needed
//Modifying the value only if needed
if ( !xa.isValid ) {
xa = xe.xmlAttributes.add( "id", pid );
}
else {
xa.value!=pid && xa.value = pid;
}
}
//calling the function
main();
HTH,
Loic
Copy link to clipboard
Copied
I appreciate your help, and I have been trying to wrap my head around your answer, but I'm still at the beginner level with JavaScript and InDesign it appears. I was hoping that there was a simpler way that would be easier for a beginner to implement and maintain. I'm having difficulties understanding the InDesign object model as well as how you reference all of the properties and methods. Here's a snippet of what I was hoping that I could do.
(Which is not working)
//Adds an action listener to every XML tag in the list of XML tags
app.activeDocument.xmlTags[0].addEventListener("afterInvoke", createStructure);
//Create structure based on tag name
function createStructure()
{
if (markupTag.name == "copy"){
//Filler
app.activeDocument.xmlElements[0].xmlElements.add("copy", "1");
app.activeDocument.xmlElements[0].xmlElements.add("copy", "2");
app.activeDocument.xmlElements[0].xmlElements.add("copy", "3");
}
elseif (markupTag.name == "item"){
//Filler
app.activeDocument.xmlElements[0].xmlElements.add("item", "Hi");
app.activeDocument.xmlElements[0].xmlElements.add("item", "Hi");
app.activeDocument.xmlElements[0].xmlElements.add("item", "Hi");
}
}
Any help again is greatly appreciated.
Copy link to clipboard
Copied
I have yet to master filling an Indesign template with an xml file myself but if you're just looking to add another element to a pre loaded xml object, wouldn't it work to just implicitly add the element and save the value?
app.activeDocument.xmlElements[0].xmlElements.item = "Hi";
(Assuming I understand your structure correctly as I'm not really sure why you would have nested elements both named "xmlElements")
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more