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

Add items to library via script

New Here ,
Mar 12, 2019 Mar 12, 2019

Copy link to clipboard

Copied

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!

TOPICS
Scripting

Views

2.1K

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

correct answers 1 Correct answer

Community Expert , Mar 17, 2019 Mar 17, 2019

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,

...

Votes

Translate

Translate
Contributor ,
Mar 12, 2019 Mar 12, 2019

Copy link to clipboard

Copied

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"});

    

Lib.png

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
New Here ,
Mar 13, 2019 Mar 13, 2019

Copy link to clipboard

Copied

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?

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 ,
Mar 13, 2019 Mar 13, 2019

Copy link to clipboard

Copied

The code sample given above by Sudha showed how to do it for an object, you can extend it to work for multiple objects. For ex if you want to add all the selected objects to the library you could run a loop and add the objects. Something like the below should work

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

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

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

Make a selection of all the objects that you want in the library and then execute the script above.

-Manan

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
New Here ,
Mar 14, 2019 Mar 14, 2019

Copy link to clipboard

Copied

Thanks, Manan, this really works. May be you know how to keep stored items(links) with its real names?

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 ,
Mar 14, 2019 Mar 14, 2019

Copy link to clipboard

Copied

Try with the following changed version

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

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

{

     var obj = app.selection

     if(obj.graphics.length > 0)

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

     else

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

}

-Manan

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
New Here ,
Mar 16, 2019 Mar 16, 2019

Copy link to clipboard

Copied

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}); 

}

}

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 ,
Mar 17, 2019 Mar 17, 2019

Copy link to clipboard

Copied

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

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
New Here ,
Mar 18, 2019 Mar 18, 2019

Copy link to clipboard

Copied

Yes! That's what I've needed! Thank you a lot!

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 ,
Aug 27, 2020 Aug 27, 2020

Copy link to clipboard

Copied

I can get the items to add to an existing Library, but only if it's closed.
Once I open it, other items I want to add give me this message.
Any help would be appreciated.
Thanks.
Larry

larryp22136904_0-1598571696128.png

 

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 ,
Aug 27, 2020 Aug 27, 2020

Copy link to clipboard

Copied

If you've already created a library called Test, and are trying to run the script again, you can try this: 

var myDoc=app.documents[0]
var myPages = myDoc.pages;
var theLib = app.libraries.itemByName('Test');
if (!theLib.isValid) {
    var theLib = 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 
          theLib.store(obj,{name:obj.itemLink.name}); 
     }
}

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 ,
Aug 28, 2020 Aug 28, 2020

Copy link to clipboard

Copied

Hi, Brianp311:
I tried the script you sent, but I'm getting the same message as before.
I tried all the post here with the same results.

I select the item(s) I want to add, run the script and the message below pops up.

I even remade another library called Test1....and got the same message after it inititally created the library for me. It simply isn't adding any other items because the library is open.
I appreciate you helping me out.

Larry

larryp22136904_0-1598630698510.png

 

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 Beginner ,
Apr 22, 2023 Apr 22, 2023

Copy link to clipboard

Copied

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?

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 ,
Apr 24, 2023 Apr 24, 2023

Copy link to clipboard

Copied

Hi @robertniemeyer1983,

The code I gave was relevant for InDesign and is not meant or supposed to run with Illustrator as well. The DOM API's for both the applications differ. You should ask you query in the Illustrator forum and scripters over there would help you. The link to the forum is given below

https://community.adobe.com/t5/illustrator/ct-p/ct-illustrator?page=1&sort=latest_replies&lang=all&t...

-Manan

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 Beginner ,
Apr 24, 2023 Apr 24, 2023

Copy link to clipboard

Copied

You are indeed correct. I hadn’t noticed I was in the indesign forum. Though it does bother me that the APIs are not the same across all apps that share similar functionality, e.g., libraries. Unless “libraries” is different in InDesign.

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 ,
Apr 24, 2023 Apr 24, 2023

Copy link to clipboard

Copied

LATEST

In an ideal world the API's would have been the same. However, we have different products, different teams, different people, different development journey of an application, all this leads to different ways of handling seemingly same things.

-Manan

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