🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: adobe.scripting.indesign
3 messages
3 total messages Started by Rick_Quatro Wed, 14 Jan 2009 14:49
[CS4 JavaScript] Adding a CrossReferenceFormat
#2035
Author: Rick_Quatro
Date: Wed, 14 Jan 2009 14:49
29 lines
1730 bytes
I am using a function to return a cross-reference format. If it doesn't exist, then I want to create it. Here is the code:



var doc = app.activeDocument; 
var xrefFmt = getXRefFmt(doc,'Hoffman Accessory'); 
alert(xrefFmt.name);

function getXRefFmt(doc,formatName) { 
  var xrefFmt = null; 
  try { 
    xrefFmt = doc.crossReferenceFormats.item(formatName); 
    $.writeln(xrefFmt.name); 
  } 
  catch (e) { 
    xrefFmt = doc.crossReferenceFormats.add({name:formatName}); 
    xrefFmt.buildingBlocks.add({blockType:BuildingBlockTypes.FULL_PARAGRAPH_BUILDING_BLOCK}); 
    xrefFmt.buildingBlocks.add({blockType:BuildingBlockTypes.CUSTOM_STRING_BUILDING_BLOCK,customText:', page '}); 
    xrefFmt.buildingBlocks.add({blockType:BuildingBlockTypes.PAGE_NUMBER_BUILDING_BLOCK}); 
  } 
  return xrefFmt; 
}



The function works as long as I have the $.writeln statement in the try block; otherwise the format is not created and I get an error on the alert statement on line 3. Why does the function fail without the $.writeln statement? Thanks.

Rick Quatro
585-659-8267
Re: [CS4 JavaScript] Adding a CrossReferenceFormat
#2040
Author: Olav_Kvern
Date: Wed, 14 Jan 2009 18:35
9 lines
1128 bytes
Hi Rick,

It's most likely that "$.writeln(xrefFmt.name)" throws an error that is then handled by the catch statement. JavaScript will always think that "xrefFmt = doc.crossReferenceFormats.item(formatName)" returns a valid object--it's only when you try to use some property of that object that it'll realize that the object is invalid.

You could just change the line to: "xrefFmt.name;"--that will generate an error if the cross reference format does not exist.

Thanks,

Ole
Re: [CS4 JavaScript] Adding a CrossReferenceFormat
#2044
Author: Rick_Quatro
Date: Wed, 14 Jan 2009 19:36
16 lines
919 bytes
Hi Ole,

OK, I see now. I modeled my code after an example in the "Adobe InDesign CS4 Scripting Guide: JavaScript", but I missed this part:



... 
//If the style does not exist, trying to get its name will generate an error. 
myName = myCharacterStyle.name; 
...



Thank you very much for your help.

Rick
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads