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

[JS][CS3] ParagraphStyles problem

New Here ,
Oct 30, 2008 Oct 30, 2008
Why oh why won't this snippet work?

#target illustrator
var aiDocRef = app.documents[0];
var paraStyles = aiDocRef.paragraphStyles;
if(paraStyles){
var captStyle = paraStyles.getByName("Caption");
}

It throws "No such element" error at the last line...

best,
Art
TOPICS
Scripting
751
Translate
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
Adobe
Community Expert ,
Oct 31, 2008 Oct 31, 2008
Not sure that's the problem, but try replacing:
if(paraStyles){
With:
if(paraStyles.length>0){
Translate
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
New Here ,
Oct 31, 2008 Oct 31, 2008
This snippet is working fine for me. I am getting the reported message only when there is no paragraph style with the name supplied to the 'getByName' function. You may want to check the existence of paragraph style you are trying to retrieve.
Translate
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
New Here ,
Oct 31, 2008 Oct 31, 2008
try67: that's not what's causing the problem, the if is working ok - the later calling getByName is problematic, as hashmi noticed.

somewhat surprising what you are suggesting, hashmi - I'd imagine this function to work fine, but just return null if the style is not defined.
I know I can try/catch, but prefer to eliminate obvious cases with plain vanilla ifs.
Translate
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
LEGEND ,
Nov 02, 2008 Nov 02, 2008
> if(paraStyles)

Why would you want the above if statement? That statement will always return as true. All AI documents contain the default [NormalParagraphStyle], which cannot be deleted.

JET
Translate
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
New Here ,
Nov 02, 2008 Nov 02, 2008
Seems like the issue was solved.

James: this was just a desperate method of isolating the problem. This lead me to the cause of the problem. Shame to admit that I re-declared a global variable locally in a function getting the style.

Since I found that, the code has been working ok for me, so I think the problem is gone.

But I don't do such silly things all the time, so I count on your taking me seriously next time ;-)

Speaking of re-declaration: I experience a problem with "const" declarations, such as you would use for version string. The second time I run the script I get an error saying that a variable is re-declared... As I don't have time to dig into this, I stick with "var" instead. Anyone with advice?
Translate
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
New Here ,
Nov 02, 2008 Nov 02, 2008
Nope - it does NOT work ok, but it's puzzling why it has been working for a few times

The script passes through the if, but stumbles upon .getByName
:(
Translate
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
LEGEND ,
Nov 02, 2008 Nov 02, 2008
LATEST
> so I count on your taking me seriously next time <br /><br />Maybe you're not taking me seriously. I was asking a simple question: what is the purpose of an if() (or even a portion of an if()) that always returns true?<br /><br />As I understand it, Javascript if() does not return true or false when you put one of the elements of an array in it. It returns the element's value. If the element does not exist, the return is not null or undefined, because the element's value is not null or undefined; the element can't be found to be evaluated.<br /><br />You can see this if in your script you do:<br /><br />alert(paraStyles.getByName("Caption"));<br /><br />What will be returned is not true or false, but [ParagraphStyle Caption].<br /><br />try it again with:<br /><br />alert(paraStyles.getByName("Caption")==true);<br /><br />If the style exists, you get false. If the style does not exist you'll get the element not found error.<br /><br />Evidently this is not peculiar to Illustrator. This understanding seems to be borne out all over the web by the presence of various workarounds designed expressly to check for the existence of an element in a Javascript array, like you can in PHP via the in_array function.<br /><br />One such workaround is described <a href="http://snook.ca/archives/javascript/testing_for_a_v/">here</a>. There are others.<br /><br />So if paragraphStyles.getByName() does not find the referenced element, it is not going to return false; it is going to return the element not found error.<br /><br />It seems unavoidable, then, if you are trying to test for the presence of a Paragraph Style named "Caption", to use a loop to check for the occurrence of the name within the collection. Something like:<br /><br />var docRef=app.activeDocument;<br />var pStyles=docRef.paragraphStyles;<br /><br />var styleNameSought="Caption";<br />var foundIt=false;<br /><br />for(i=0;i<pStyles.length;i++){<br /><br />var currStyleName=pStyles.name;<br /><br />if(currStyleName==styleNameSought){<br /><br />foundIt=true;<br />}<br /><br />}<br /><br />alert(foundIt);<br /><br />JET
Translate
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