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 21, 2015

record action to get local file location.

add that to the code below.

remember replace "\" with "/"

var myfile = File("C:/Users/tristan/AppData/Roaming/Adobe/Creative Cloud Libraries/LIBS/70DE407249FFD926992016B8_AdobeID/creative_cloud/dcx/2FB37286-9657-4C80-B2DA-00D4F3C215EA/components/3ced2d9e-4390-4971-b0f5-6d6682111925.png");

var placed = app.activeDocument.placedItems.add();

placed.file = myfile;

placed.position = [0,0];

placed.embed();