Skip to main content
Inspiring
March 25, 2013
Answered

reply try catch till right paragraph name

  • March 25, 2013
  • 1 reply
  • 770 views

Hi, I have to apply a paragraph style to grep preferences before a find cycle.

If user input is a paragraph name not existing in the document the script outputs an error.

First error rules right but second stops script. And, of course, I need to go ahead till right paragraph name.

here's javascript

app.findGrepPreferences = app.changeGrepPreferences = null;

var myPara = prompt ("input a paragraph style", "");

try {

    app.activeDocument.paragraphStyles.itemByName(myPara).name;

    alert ("This is a paragraph style!");

    }

catch (myError) {

    var myPara = prompt ("This is NOT a paragraph style! \rType existing style ", "")

    }

app.findGrepPreferences.appliedParagraphStyle = myPara;

alert ("Correct! \nStyle applied in grep preferences!");

Any idea? ...maybe a while  loop could resolve (tryied without success)

Thanks

This topic has been closed for replies.
Correct answer absqua

No real need for try/catch here. This scenario is what the .isValid property is for. Example with while loop:

var doc = app.activeDocument,

    promptText = "Input a paragraph style name.",

    shouldPrompt = true,

    tempStyle, style, name;

while (!style && shouldPrompt) {

    name = prompt(promptText, '');

    if (name === null) {

        shouldPrompt = false; // or skip the shouldPrompt checks and just exit(), if you prefer

        continue;

    }

    tempStyle = doc.paragraphStyles.itemByName(name);

    if (tempStyle.isValid) {

        style = tempStyle;

    }

    else {

        promptText = "\"" + name + "\" is NOT a paragraph style!\rType existing style.";

    }

}

if (style) {

    // carry on with find/change

}

Note that this will only find styles at the root level. If you have, or might have, paragraph style groups in your document, you'll need to loop through doc.allParagraphStyles instead.

If I were doing this, I think I would just present the user with a dropdown of all the style names the document contains, and let them choose. You could use ScriptUI or just the normal InDesign dialog API. See "User Interfaces" in the Scripting Guide.

Good luck,

Jeff

Edited; Jive does weird stuff with <code> tags.

1 reply

absquaCorrect answer
Inspiring
March 25, 2013

No real need for try/catch here. This scenario is what the .isValid property is for. Example with while loop:

var doc = app.activeDocument,

    promptText = "Input a paragraph style name.",

    shouldPrompt = true,

    tempStyle, style, name;

while (!style && shouldPrompt) {

    name = prompt(promptText, '');

    if (name === null) {

        shouldPrompt = false; // or skip the shouldPrompt checks and just exit(), if you prefer

        continue;

    }

    tempStyle = doc.paragraphStyles.itemByName(name);

    if (tempStyle.isValid) {

        style = tempStyle;

    }

    else {

        promptText = "\"" + name + "\" is NOT a paragraph style!\rType existing style.";

    }

}

if (style) {

    // carry on with find/change

}

Note that this will only find styles at the root level. If you have, or might have, paragraph style groups in your document, you'll need to loop through doc.allParagraphStyles instead.

If I were doing this, I think I would just present the user with a dropdown of all the style names the document contains, and let them choose. You could use ScriptUI or just the normal InDesign dialog API. See "User Interfaces" in the Scripting Guide.

Good luck,

Jeff

Edited; Jive does weird stuff with <code> tags.

foreditAuthor
Inspiring
March 26, 2013

Thanks for script ...you give me a lesson and "isValid" property light me.

Change try/catch to a while loop so:

app.findGrepPreferences = app.changeGrepPreferences = null;

var myPara = prompt ("input a paragraph style", "");

  while (!app.activeDocument.paragraphStyles.itemByName(myPara).isValid)

    {myPara = prompt ("\"" + myPara + "\" is NOT a paragraph style!\rType existing style")}

    app.findGrepPreferences.appliedParagraphStyle = myPara;

alert ("Correct! \nStyle applied in grep preferences!");

Better as you told is a UI but I'm new to script so...

thanks again