Skip to main content
fredsky2014
Known Participant
June 18, 2015
Answered

how to place a copy of a librairie item with a script?

  • June 18, 2015
  • 2 replies
  • 2154 views

i've created a librairie with 1 graphic.

i can place this item as a embeded copy (only option available in cc 2014), or as a link (new with cc 2015)- just right click and choose the place in context menu.

now, how can i do this from script?

with this little skeleton from from the reference PDF i get access to the item from librairie in current document = so should be easy to place it as copy ...

if ( app.documents.length > 0 ) {

var fileReferences = new Array();

var sourceDoc = app.activeDocument;

var sourceName =sourceDoc.name;

for ( i = 0; i < sourceDoc.pageItems.length; i++ ) {

artItem = sourceDoc.pageItems;

    switch ( artItem.typename ) {

        case "PlacedItem":

        fileReferences.push( artItem.file.fsName );

       

       

        break;

}

}

fred

This topic has been closed for replies.
Correct answer Qwertyfly___

perhaps i'm not clear, but this script is about the same i explained first.

however this DOES NOT make a copy, it convert a link to embed. and this is a buggy feature of illustrator, because when it does so, it will convert anything to graph & outlines.

so a text will become a outline (Loosing text editability), and svg interactivity is lost.

this is how people where working with symbols, before cc 2015 with librairies.

so what i need is something to :

select a linked object (in fact all same reference object), find it into the libraries (easy to do, see first script), and replace it by the "place a COPY". this now is a perfect file, all copy are updated correctly and in place.

fred


I don't get text becoming outlined,

but I do get a dumb amount of clipping paths.

I hate the over use of unnecessary clipping paths.

I'm still not sure what you really want to do.

But this may be getting closer...

start by dragging your desired library Items to your document.

this places them all as links.

once you are happy, run this script.

it will replace each linked library item with a copy.

if ( app.documents.length > 0 ) {

    var sourceDoc = app.activeDocument, artItem, myAsset, pos = [];

    sourceDoc.selection = null;

    for (var i = sourceDoc.placedItems.length-1; i > -1; i-- ) {

        artItem = sourceDoc.placedItems;

        pos = [artItem.left,artItem.top];

        myAsset = File(artItem.file.fsName);

        app.openCloudLibraryAssetForEditing(myAsset,myAsset);

        app.executeMenuCommand ('selectall'); 

        app.executeMenuCommand ('copy'); 

        app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);

        sourceDoc.activate();

        app.executeMenuCommand ('paste');

        app.executeMenuCommand ('group');

        sourceDoc.selection[0].position = pos;

        artItem.remove();

    }

}

2 replies

fredsky2014
Known Participant
July 1, 2015

here is my current working and improved code. it does automatically convert ALL linked items into a copy, with same position and size.

it also take care of missing links.

but if there is NONE placed items, i get an error...

function convertLinksLibrariesToCopies()

{

    var sourceDoc = app.activeDocument;

    var artItem;

    var myAsset;

    var pos = [];

    var fileName;

    sourceDoc.selection = null;

   

    var updatedItems = app.activeDocument.placedItems.length;

    var artItem = app.activeDocument.placedItems[0];

    //have to make this to retrieve ALL linked items, even those linked inside others links (recursive)

    while ( (updatedItems>0) && ( artItem.name > "") ) {

        updatedItems=app.activeDocument.placedItems.length;

       

        //loop through all linked items of the doc

        for ( i = 0; i < updatedItems; i++ ) {

           

            artItem = app.activeDocument.placedItems;

            alert("artItem.name "+ artItem.name);

           

            if ( artItem.name > "")

            {

            // get the file reference path

            myAsset = File(artItem.file.fsName); 

            alert("myAsset "+ myAsset);

           

            //save info from this item

            pos = [artItem.left,artItem.top]; 

            width = artItem.width;

            height = artItem.height;

           

            // CC 2015 only, open item as edit in new doc

            app.openCloudLibraryAssetForEditing(myAsset,myAsset); 

            // selectall, then group then copy

            app.executeMenuCommand ('selectall');

            app.executeMenuCommand ('group');              

            app.executeMenuCommand ('copy');   

            app.activeDocument.close(SaveOptions.DONOTSAVECHANGES); 

           

            // return to doc

            sourceDoc.activate();

            //paste the group from the edited library item

            app.executeMenuCommand ('paste');

            //position it over the original linked item, with original size

            sourceDoc.selection[0].position = pos;

            sourceDoc.selection[0].width = width;

            sourceDoc.selection[0].height = height;

           

            //remove the linked item

            artItem.remove(); 

            }

            else{

                //next item

                i= i + 1;               

                alert("i "+ i);

            }

        } 

    }

}

Qwertyfly___
Legend
June 19, 2015

not sure the above is what you are after.

if you create an action and while recording drag your graphic from the library to your document.

the action will contain the local path to the graphic.

you could place the graphic with javascript using that path.

or just use the action

fredsky2014
Known Participant
June 21, 2015

hello!

even in action the "place as copy" doesn't appears. only the "place as link" will be show in the action palette.

seem that is a "new rushed" feature that is not present in any form of script/macro

fred

Qwertyfly___
Legend
June 24, 2015

"there should be an option to set the default when dragged and dropped to document" this is not needed.

because the quirk in the way they've implemented is :

- either to "place links" everywhere you place a library item=> the only way that all you links will be updated once you modify the master item in library... / but again links does not preserve text/svg interactivity and others...

-either you "place a copy" of the library item, but all notions of update are gone... so useless .

so the only good way to get both features is to :

- create a document with links everywhere. you can also create/use links to other librairies items inside the same item (recursive). EXTREMELY easy and powerfull. you can store graphics, colors, text styles, text => any time you modify/edit any library items they will updated on all related AI pages

- save this page as it, do NOT save the version with copy!!!

- use you script to convert all links to copy => now we got access back to text and svg interactivity, since basically you edit each library item, selectall+copy & replace the corresponding link.  export this is other format for web or pdf...


My main use for items in the cloud library is for element I use over and over again.

These do not change, and do not need to be dynamic.

in CC2014 I would drag a graphic to where I want it and move on.

Now I Must right click, chose place Copy, then drag the graphic to where I want it.

this takes twice as long and is driving me crazy.

if I forget to do this and drag in a link then it is not available when the printer opens the document due to it being a link.

I would like the option of making the default for graphics dragged from the library panel to be placed as a copy!