Skip to main content
December 5, 2013
Question

How to speed up a javascript looping through all pathItems?

  • December 5, 2013
  • 2 replies
  • 2217 views

I have a very simple javascript for Illustrator that is intended to render all filled objects semi-transparent. It works nicely but for large documents with many objects this takes ages, even beyond the time it would take to do it manually.

I'm wondering if there is any way to improve this script so it works in a reasonable time for large files? I'm running Illustrator CS5 on a Windows 7 64-bit Intel Quadcore i5-2400 @ 3.10 GHz System with 12 GB of RAM and an AMD Radeon HD 6350 graphics. Would upgrading to CS6 significantly improve the speed? CPU load is only at 25%, but I guess javascript cannot be run in multiple threads with the Illustrator engine? Is there another scripting language that would allow me faster processing of that task in illustrator or is my code just poorly optimized?

Thanks in advance for any suggestions!

with (app.activeDocument) {

    if (pathItems.length > 0)

    {

        for (var g = 0 ; g < pathItems.length; g++)

          {

               if (pathItems.filled == true)

               {

                        pathItems.opacity = 50;

               }

          }

      }

}

Kind regards,

Azzip

This topic has been closed for replies.

2 replies

CarlosCanto
Community Expert
Community Expert
December 5, 2013

hmm...not much room for improvement, how many paths is your file?

try this

with (app.activeDocument) {

    var pathCount = pathItems.length;

    if (pathCount > 0)

    {

        for (var g = 0 ; g < pathCount; g++)

          {

               if (pathItems.filled == true)

               {

                        pathItems.opacity = 50;

               }

          }

      }

}

or this

with (app.activeDocument) {

    var pathCount = pathItems.length;

    if (pathCount > 0)

    {

        for (var g = 0 ; g < pathCount; g++)

          {

              var ipath = pathItems;

               if (ipath.filled == true)

               {

                        ipath.opacity = 50;

               }

          }

      }

}

December 5, 2013

Hi Carlos,

Thanks a lot to you as well for so quicky addressing my problem in this detail! I won't be in my office for the next 10 days, but I'll compare the runtime of your suggestions as soon as possible and post my results! Thank you very much!

Kind regards,

Azzip

Inspiring
December 6, 2013

Hi Azzip

I downloaded your example PDF. Yes, all the paths are inside groups and clipping groups.

If it were isolated paths (not inside groups), then you could check them via Layers panel (you write in the script a function that looks directlly for pathItems in each layer, then change them). This would be faster than cycling into all the pathItems of the document. But that's not your case.

I think Carlos is correct. There's not so much to do to improve the speed since all the task you perform must check the condition for all paths. For example, you could think to apply the opacity only for orange and red objects, but, anyway, script would need to verify all the paths in order to know the ones filled with red and orange. So, that's the same kind of verification hehe.

Best Regards

Gustavo.

Inspiring
December 5, 2013

Hi Azzip

When you mention "make semi-transparent all filled objects", you are telling to change the isolated paths as well as paths inside groups, right?

Because if this is not the intent, you could take off the verification for paths inside groups and concentrate in the isolated paths. It would speed the process.

Can you give some more informations about the objective of your script?

Best Regards

Gustavo.

December 5, 2013

Hi Gustavo,

Thanks for your quick reply. Sorry for not being precise enough. I'm actually trying the following: I have several PDFs generated from some scientific analysis software we use in our lab. In these, semi-transparent areas from the program are due to some bug exported in full opacity (thus hiding the data points behind it), resulting in a practically useless output (see picture: red, orange and light blue shapes). While I'm quite fast in the task when using Illustrator's internal function to select objects of lets say 'same fill color' and then changing their opacity, I would like to use a script to automatize these few but annoying steps. Maybe my approach of looping through all pathitems is not the right one in this case? I think the objects I'm refering to are paths within groups, but I'm rather new to Illustrator vocabulary (and scripting, obviously), so I might be wrong.

Here is a picture of the faulty output I'm trying to correct in illustrator:

I'm not sure if I can post example files in this forum or if such links are removed for security purposes, anyway, I'll try: Here is an example PDF (smaller than the usual files but it illustrates the problem): http://wikisend.com/download/552752/example.pdf

Thanks again for your interest in the problem!

Kind regards,

Azzip