Skip to main content
Inspiring
August 10, 2022
Answered

Expanding a lot of symbols via script

  • August 10, 2022
  • 1 reply
  • 829 views

Due to a long unresolved glitch, I'm writing a script that expands all symbols before saving a pdf.

If I were to do this without a script, I would go to the symbols pane, select all symbols in the list, click the trash can icon, and choose to expand all instances in the next dialog.

 

I'm not sure how to replicate these actions with a script, but here's my best try:

app.selection = [];
for(i=0; i<doc.symbolItems.length; i++)
    doc.symbolItems[i].selected = true;
app.executeMenuCommand("Expand3");

The strange thing is that when this code runs, only some of the symbols are expanded. I ran the code repeatedly on a file with 248 symbolItems on the canvas. First iteration expanded 77 symbols. The next iteration expanded 59 symbols. Then 34, 37, 36, and finally the last 5. If I run it again, it expands the same symbols in the same order.

 

I thought this was a problem with the script until I manually selected all the symbols and clicked "Object > Expand..." and got the same result. Only a portion of the symbols expanded. Is there some sort of upper limit to what the "Object > Expand..." command can handle until or something? It's strange that the number of expanded symbols is inconsistent. Maybe it depends on the complexity of the symbols?

 

Whatever the cause, I get the feeling that there's a better way to do this other than having the script just hunt down symbols and keep opening the "Object > Expand..." dialog until none remain. Is there a way to directly access the commands in the symbol pane instead?

This topic has been closed for replies.
Correct answer Siev

...and the executeMenuCommand fails because removing symbolItems inside the loop pretty much skips every other item, check the link I posted for more info.


the executeMenuCommand is not inside the loop. The loop just selects all symbolItems. Then the Expand3 command executes once. If I repeat the process manually, with no scripting whatsoever, the expand command still fails to break all the links. This means the expand command has its own opaque limitations and isn't suitable for the job.

 

This works:

while(doc.symbolItems.length > 0){

    doc.symbolItems[0].breakLink();

}

which, to me, seems more intuitive than a reverse-for loop. It just isn't able to expand symbols that are used inside transparency masks.

 

Symbols residing inside transparency masks are off-limits to scripts(?), but they're also the ones that cause the most problems for me. After breaking all the links, the only symbols still in use are inside transparency masks. Symbols that are still in use throw an error when you use the remove() method on them. So with some exception handling, I can remove() all symbols except those inside clipping masks. If there are any symbols left after the processes is over, the script assumes they are beyond its scope and it has to stop and ask the user to manually delete them using the symbol window.

 

I think it's a very ugly solution, but unless there's a way to get at those symbols inside transparency masks, I can't think of a better way.

1 reply

SievAuthor
Inspiring
August 10, 2022

after a bit more searching the forums, I found symbolItem has a breakLink() method. I tried using that in place of selecting and expanding. It is an improvement because it doesn't open a new dialog window, but it still only works for a number of symbols before quitting and leaving the rest unbroken.

 

So the question remains why some symbols get expanded and others are skipped, and if there is a better method.

 

also side question: why isn't the breakLink() method mentioned here? Is there a more comprehensive source for documentation? This is not the first time I've searched the forums to find really useful but undocumented properties and methods.

CarlosCanto
Community Expert
Community Expert
August 10, 2022

use a backwards loop instead of the more common forward loop, more info here

 

https://community.adobe.com/t5/illustrator-discussions/breaklink-illustrator-2020-issues/m-p/10881620#M163395

SievAuthor
Inspiring
August 11, 2022

I realized the same thing in the shower. The for loop's iterator is counting up while the array length counts down, so the loop stopped when they met in the middle. This code works:

while(doc.symbolItems.length>0){
    doc.symbolItems[0].breakLink();
}

But its reach does not extend inside transparency masks. Is there any way to break symbols used inside transparency masks?

 

Also Still curious about where breaklink() is documented and why the expand command failed