• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Creating a new paragraph style with try/catch (jsx)

Explorer ,
Sep 18, 2019 Sep 18, 2019

Copy link to clipboard

Copied

Good Morning, 

I'm hoping someone can help me understand this snippit, I got from the adobe scripting guide. (createStyles.jsx) Specifically line 4 below. 

    myName = myParagraphStyle.name;


My understanding is that I am assigning "myParagraphStyle" to a variable. But because It may or may not exist, (and throws an error when it doesn't) I have to put it in the try{} statement. The catch statement is what gets executed when an error would otherwise be thrown. In this case it creates myParagraph style. 

So what does  myName = myParagraphStyle.name; do? And why bother assigning it to a variable? The variable never gets used, but it does get reassigned for myColor.name, and myCharaterStyle.name.


//Create paragraph style 
try{
    myParagraphStyle = myDocument.paragraphStyles.item("myParagraphStyle");
    myName = myParagraphStyle.name;
}
catch (myError){
    myParagraphStyle = myDocument.paragraphStyles.add({name:"myParagraphStyle"});
}

 

TOPICS
Scripting

Views

648

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 18, 2019 Sep 18, 2019

Copy link to clipboard

Copied

Remove the try..catch error handler and run your script to see what that line is for.

Make sure this style does not exist!

 

Pay close attention to what the error line number is, and what the error message says. Then you should be able to reason why it is like that.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Sep 18, 2019 Sep 18, 2019

Copy link to clipboard

Copied

Take the whole try/catch off? It points to line 4 and says that the object is invalid. Thats because it can't access myParagraphStyle.name because it still doesn't exist. right? Does that also mean that myDocument.paragraphStyles.item("myParagraphStyle") was "successfully" assigned to myParagraphStyle. Because otherwise the error would have been thrown from line 3.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 18, 2019 Sep 18, 2019

Copy link to clipboard

Copied

🙂You're almost there! Yes: the error says that "myParagraphStyle.name" does not exist, but this indeed does NOT automatically mean that "myParagrahStyle" does not exist! It does -- and it has the value "undefined". That IS a valid value (hence, you don't get an error on that line) but the next line tries to access this as if it's an object, "undefined.name", and THAT shows the error. So assigning a non-existent object is okay ... as long as you don't try to use its (non-existent) properties.

 

For even more fun with this, after the assignment, add this line:

alert (myParagraphStyle.constructor.name);

It will show you "ParagraphStyle" -- even though it does not exist! Think of this like a variable of some type that is declared but not yet has been given a value: 'var x' is pretty much the same, as here the value of 'x' is undefined and you cannot do "alert (x.length)" nor "alert (x+1)", and expect a meaningful result.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Sep 18, 2019 Sep 18, 2019

Copy link to clipboard

Copied

This is fantastic! Thank you so much! One more related question. I'm using a similar try/catch setup to create a swatch. It works as expected for the most part, but when I tried to "try" for the default [Black] swatch it wigs out. Are there special rules for the bracketed swatches? because "Black" works, but "[Black]" doesn't. I hope that makes sense.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 18, 2019 Sep 18, 2019

Copy link to clipboard

Copied

Well I'd need to see what you are attempting there before I could say. I think the main difference is that you cannot do anything to standard swatches -- renaming, changing their color model, etc. But inquiring their name, as with your paragraph style example, should be okay.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Sep 18, 2019 Sep 18, 2019

Copy link to clipboard

Copied

I just realized that [black] can't be deleted, so there is really no reason to use the try/catch, because it will always be there. So I'm just creating extra work. 

But I guess for novelty's sake, here is the code that fails. If I take the brakets off the word Black, it works.  

try{
    myColor = myDocument.colors.item("[Black]");
    myName = myColor.name;
}
catch (myError){
    myColor = myDocument.colors.add({name:"[Black]", model:ColorModel.process, colorValue:[0, 0, 0, 100]});
}

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Sep 18, 2019 Sep 18, 2019

Copy link to clipboard

Copied

LATEST

HI,

Try below code and compare with your code:

 

try{
myParagraphStyle = app.activeDocument.paragraphStyles.itemByName("myParagraphStyle");
myName = myParagraphStyle.name;
}
catch (myError){
myParagraphStyle = app.activeDocument.paragraphStyles.add({name:"myParagraphStyle", appliedFont:"Avenir Next", fontStyle:"Regular", pointSize:6, leading:8, justification:Justification.RIGHT_ALIGN}); // extra property
}

 

Thanks,

Karthik

 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines