Skip to main content
Participating Frequently
March 12, 2019
Answered

Add items to library via script

  • March 12, 2019
  • 1 reply
  • 3024 views

Hello! I want to add items from a page as separate objects to the library via script. May be someone know how it could be done. What command should I use in ExtendScript or AppleScript. Thanks!

This topic has been closed for replies.
Correct answer Manan Joshi

Thank you, again Manan Joshi! I've combine your script with selection script, but I can't make it right to loop through pages. Can your help me again?

var myDoc=app.documents[0]

var myPages = myDoc.pages;

var newLib = app.libraries.add("Test.indl");

for(i=0; i<myDoc.pages.length; i++)

{

   

    app.selection = null; 

    pItems = app.windows[0].activeSpread.pageItems; 

    app.select (pItems, SelectionOptions.ADD_TO); 

for(var i=0; i < app.selection.length; i++)  

     var obj = app.selection 

     if(obj.allGraphics.length > 0) 

         newLib.store(obj,{name:obj.allGraphics[0].itemLink.name}); 

         

     else 

          newLib.store(obj,{name:"myObject" + i}); 

}

}


I am not sure what you are trying to do, as you iterate pages but then add pageitems of the spread. Remember that pageitems of the page would be included in the spread pageitems as well. So either iterate the spread or iterate the pages but not both.

Apart from that you don't actually need to select these objects to add them the library, as you can pass the pageitem object directly to the store method. Below is the demonstration of what i explained, in this code snippet i am iterating the pages, if needed you can iterate the spreads as well.

var myDoc=app.documents[0]

var myPages = myDoc.pages;

var newLib = app.libraries.add("Test.indl");

for(i=0; i<myDoc.pages.length; i++)

{

     for(var j=0; j < myDoc.pages.allGraphics.length; j++)

     {

          var obj = myDoc.pages.allGraphics

          newLib.store(obj,{name:obj.itemLink.name});

     }

}

To iterate the spreads you could use something like below, this will give you the graphics in the first spread

app.documents[0].spreads[0].allGraphics

-Manan

1 reply

Inspiring
March 13, 2019

Hi,

     Try like this according your requirement.

  

     var newLib = app.libraries.add("Test.indl");          // You can define here existing library instead of creating

     var sel = app.selection[0];               // Selection object from a page

     newLib.store(sel,{name:"ImgFrame"});

    

egikasAuthor
Participating Frequently
March 13, 2019

Hi, and thanks for the reply. But you suggest to store only SELECTED items into library. Besides they store as single object "ImgFrame".

I can't find the way to store them as SEPARATED objects via script. The same way as it does in menu. Maybe you know how?

robertniemeyer1983
Participant
April 22, 2023

I am not sure what you are trying to do, as you iterate pages but then add pageitems of the spread. Remember that pageitems of the page would be included in the spread pageitems as well. So either iterate the spread or iterate the pages but not both.

Apart from that you don't actually need to select these objects to add them the library, as you can pass the pageitem object directly to the store method. Below is the demonstration of what i explained, in this code snippet i am iterating the pages, if needed you can iterate the spreads as well.

var myDoc=app.documents[0]

var myPages = myDoc.pages;

var newLib = app.libraries.add("Test.indl");

for(i=0; i<myDoc.pages.length; i++)

{

     for(var j=0; j < myDoc.pages.allGraphics.length; j++)

     {

          var obj = myDoc.pages.allGraphics

          newLib.store(obj,{name:obj.itemLink.name});

     }

}

To iterate the spreads you could use something like below, this will give you the graphics in the first spread

app.documents[0].spreads[0].allGraphics

-Manan


Hello!  I'm using Illustrator 2021 (v25) and your code does not work.  Specifically, I get the error, "Undefined is not an object; var newLib = app.libraries.add("Test.indl");"  I am using a mac with MacOS Monterey.  I copy and pasted your script into a *.jsx file and into a *.js file in case *.jsx was special. I've ran the script with a document open and with the libraries panel visible and not.  Nothing seems to get this app.libraries statement to connect witht he object.  Can you please help?