Skip to main content
suneelkv
Known Participant
October 16, 2015
Answered

Getting the File Name from an Embedded Link

  • October 16, 2015
  • 4 replies
  • 2718 views

Hi All,

I'm using the below code to get the raster item (file name) for the embedded links in an artboard.

But I am facing the below issues.

1) if any link is missing then the script is not running

2) if the files are not in the respective path (say if a single file even though embedded not available in the path mentioned) am getting the error.

Below is the code am using,

       for (var i = 0, l = doc.artboards.length; i < l; i++)

       { 

            doc.artboards.setActiveArtboardIndex(i); 

            doc.selectObjectsOnActiveArtboard();

             $.writeln("doc.rasterItems.length ",doc.rasterItems.length);

            for (var iSelectedItems=0; iSelectedItems<doc.rasterItems.length; iSelectedItems++)

                   {

                             var topLeftPosDoc= doc.rasterItems[iSelectedItems].position;   

                             try

                                {

                                         $.writeln("doc.rasterItems[iSelectedItems].file.name    ",doc.rasterItems[iSelectedItems].file.name);

                                         $.writeln(":::::IN getFileName() function:::::: doc.rasterItems[iSelectedItems].file.name  :::::"+doc.rasterItems[iSelectedItems].file.name);

                                         var boundingRectanglePath = doc.pathItems.rectangle(doc.rasterItems[iSelectedItems].visibleBounds[1],

                                                                              doc.rasterItems[iSelectedItems].visibleBounds[0],

                                                                              doc.rasterItems[iSelectedItems].width,

                                                                              doc.rasterItems[iSelectedItems].height);

                                        boundingRectanglePath.filled = false;

                                        boundingRectanglePath.stroked = true;  

                                        var color = new RGBColor;

                                        color.red = 0;

                                        color.green =255;

                                        color.blue = 0;

                                        boundingRectanglePath.strokeColor = color; 

                                        boundingRectanglePath.strokeWidth = 1;

                                }

I would like to fine tune and make this independent.

Am printing the name and also drawing a green box for the file name which is found and hence the code.

Can somebody suggest me the easiest way of having embedd link name?

Regards,

Suneel

Correct answer Qwertyfly___

My understanding is that OP is trying to loop the artboards and display the filename on the artboard. Relinking would require intervention on some level. It sounds like they just want to take the name of the linked file, whatever it is, and print it to the screen.

Or maybe i'm misunderstanding.


when you place an item it is placed as a link, this is referred to in JS as a placedItem

if you then embed this placed item it embeds the raster info into the .ai file, now JS referres to it as a rasterItem.

the OP only refers to rasterItems so I guess they are after the location of the file before it was embedded.

if the original file gets renamed or moved it no longer holds the path to the original file.

due to the JS engine in illustrator. if you ask for the file path and it does not exist, you get an Error.

this is what was causing the OP's issues.

introduce a Try Catch statement to catch this error and work around it.

I don't know of any other way to get this info...

4 replies

Shadoufang
Participant
September 16, 2025

Thank you all for this.
Commenting here only to "bookmark".
I can't see any other way to do this on here.
Nothing similar to a save function for discussions/threads, right?

Thanks again,
Lorgen

fredsky2014
Known Participant
December 10, 2015

hello !

the only way i found to know if a item still exist (so filepath is correct) is to check for existence.

//artItem exists?

if (sourceDoc.pageItems[index].file.exists) {

artItem = sourceDoc.pageItems[index];

}

it took me so much time to find this trick... because in some case even try..catch fails.

fred

suneelkv
suneelkvAuthor
Known Participant
December 18, 2015

Thanks Fred, Even thats a good trick of checking it. It worked. -Suneel

suneelkv
suneelkvAuthor
Known Participant
October 20, 2015

Yeah, I found it sorry forgot to reply again in this loop....

I've actually integrated with my actual full script and is working fine..

Thank you Qwertyfly...‌and williamadowling

My actual question is other than doc.rasterItems[index].file.name any other ways to do this?

Qwertyfly___
Legend
October 21, 2015

this is the only place I know of that holds info about the embedded items original path.

I'm not sure why it keeps this as once it is embedded the raster information is saved within the ai file.

this negates the need for the link.

not sure exactly what you are doing with these paths.

but you could unembed a rasterItem, creating a .psd file that is linked to your .ai file

Disposition_Dev
Legend
October 21, 2015

My understanding is that OP is trying to loop the artboards and display the filename on the artboard. Relinking would require intervention on some level. It sounds like they just want to take the name of the linked file, whatever it is, and print it to the screen.

Or maybe i'm misunderstanding.

Qwertyfly___
Legend
October 19, 2015

your try statement does not have a catch so I cannot see what you are doing if the rasterItem has no associated file.

here is a simplified version of your snippet, modified to alert filename, or error text if filename not available, then I would run your draw box section.

var doc = app.activeDocument;

var ras = doc.rasterItems;

try{

    alert("ras[0].file.name = " + ras[0].file.name);

}catch(e){

    alert("ras[0].file.name = "+e);

}

// now draw your box

does this help at all?

if not I will need to see the rest of the script to get a better Idea of what is going on...

suneelkv
suneelkvAuthor
Known Participant
October 19, 2015

Hi Qwertyfly,

Thanks for the prompt response.

Even catch has also the same piece of code, Only that I am drawing different colour (green box if it is found/else red box)

Anyways I've put the whole function here.

What it does in simple terms is find the raster items available in all artboards, if found will draw a green box on the item, put the file name on the asset and x,y coordinates if not found shall draw a red box and quit.

Now since it looks like a limitation for the script that if the file link is missing it shall not give the file name associated, I'm also looking to change this script based on the objects selected, rather than doing for all the artboards, just print the file names only to the selected objects.

Please help.

****

function getFileName()

{

   

        var semiRectangleHeight = 0;

        var semiRectangleWidth= 0;

        var rectangleCenterDoc = new Array (0,0);

        var recangleWidth = 0;

 

   

     $.writeln("doc.artboards.length    ",doc.artboards.length);

       for (var i = 0, l = doc.artboards.length; i < l; i++)

       { 

            doc.artboards.setActiveArtboardIndex(i); 

            doc.selectObjectsOnActiveArtboard();

             $.writeln("doc.rasterItems.length ",doc.rasterItems.length);

            for (var iSelectedItems=0; iSelectedItems<doc.rasterItems.length; iSelectedItems++)

                   {

                             var topLeftPosDoc= doc.rasterItems[iSelectedItems].position;   

                             try

                                {

                                         $.writeln("doc.rasterItems[iSelectedItems].file.name    ",doc.rasterItems[iSelectedItems].file.name);

                                         $.writeln(":::::IN getFileName() function:::::: doc.rasterItems[iSelectedItems].file.name  :::::"+doc.rasterItems[iSelectedItems].file.name);

                                         var boundingRectanglePath = doc.pathItems.rectangle(doc.rasterItems[iSelectedItems].visibleBounds[1],

                                                                              doc.rasterItems[iSelectedItems].visibleBounds[0],

                                                                              doc.rasterItems[iSelectedItems].width,

                                                                              doc.rasterItems[iSelectedItems].height);

                                        boundingRectanglePath.filled = false;

                                        boundingRectanglePath.stroked = true;  

                                        var color = new RGBColor;

                                        color.red = 0;

                                        color.green =255;

                                        color.blue = 0;

                                        boundingRectanglePath.strokeColor = color; 

                                        boundingRectanglePath.strokeWidth = 1;

                                }

                                catch(err)

                                {

                                   alert("File for the asset with red boundary is not found,pls re-link and run the script again....");

                                    //$.writeln(":::::IN getFileName() function:::::: doc.rasterItems[iSelectedItems].file.name  :::::"+doc.rasterItems[iSelectedItems].file.name);

                                       var boundingRectanglePath = doc.pathItems.rectangle(doc.rasterItems[iSelectedItems].visibleBounds[1],

                                                                              doc.rasterItems[iSelectedItems].visibleBounds[0],

                                                                              doc.rasterItems[iSelectedItems].width,

                                                                              doc.rasterItems[iSelectedItems].height);

                                        boundingRectanglePath.filled = false;

                                        boundingRectanglePath.stroked = true;  

                                        var color = new RGBColor;

                                        color.red =255;

                                        color.green = 0;

                                        color.blue = 0;

                                        boundingRectanglePath.strokeColor = color; 

                                        boundingRectanglePath.strokeWidth = 1;

                                    break;

                                }

                              // To get the file name next to the bottom left coordinates

                             

                             var topLeftPosDoc= doc.selection[iSelectedItems].position;                 

                            semiRectangleHeight = (doc.selection[iSelectedItems].height * 0.5);

                            semiRectangleWidth = (doc.selection[iSelectedItems].width * 0.5);

                             // To get the center coordinates

                             var topLeftPosArt = doc.convertCoordinate (topLeftPosDoc, CoordinateSystem.DOCUMENTCOORDINATESYSTEM, CoordinateSystem.ARTBOARDCOORDINATESYSTEM);

                             var cenPosArt = new Array(0,0);

                             cenPosArt[0] = topLeftPosArt[0] + semiRectangleWidth;

                             //cenPosArt[1] = Math.abs(topLeftPosArt[1] - semiRectangleHeight);

                             cenPosArt[1] = (topLeftPosArt[1] - semiRectangleHeight);

                             recangleWidth = doc.selection[iSelectedItems].width ;

                             rectangleHeight = doc.selection[iSelectedItems].height;

                

                             rectangleCenterDoc[0] = topLeftPosDoc[0]+semiRectangleWidth;

                             rectangleCenterDoc[1] = topLeftPosDoc[1]-semiRectangleHeight;     

                              var bottomLeftPosArt= doc.convertCoordinate (topLeftPosDoc, CoordinateSystem.DOCUMENTCOORDINATESYSTEM, CoordinateSystem.ARTBOARDCOORDINATESYSTEM);

                              bottomLeftPosArt[1] = Math.abs(bottomLeftPosArt[1] - (doc.rasterItems[iSelectedItems].height ));

                              //specLabelFileName(doc.rasterItems[iSelectedItems].file.name,topLeftPosDoc[0]+10, (topLeftPosDoc[1]- doc.rasterItems[iSelectedItems].height)+10);

                              specLabelFileName(doc.rasterItems[iSelectedItems].file.name,(topLeftPosDoc[0]+semiRectangleWidth - 20),topLeftPosDoc[1]-10);

                             

                            specLabelWidthHeight(concatenate(bottomLeftPosArt[0],bottomLeftPosArt[1]),topLeftPosDoc[0]+10, (topLeftPosDoc[1]- rectangleHeight)+15);

                            drawLeftBottomTriangle(topLeftPosDoc, rectangleHeight);

                     }

        }

        dlg.close ();

}

****

Qwertyfly___
Legend
October 19, 2015

your script is missing quite a few dependencies.

which makes it hard to know what is going on.

it also seems a bit long winded, and repetitive. ie. you remake the colors every time you need them.

try this function and see if it does what you want.

text location can be tweaked etc.. if it is doing what you want...

function Get_Link_Names(){

    var doc = app.activeDocument;

    var ras = doc.rasterItems;

    var TOmm = 2.83466796875;

    var gr = new RGBColor; gr.red = 0; gr.green = 255; gr.blue = 0;

    var rd = new RGBColor; rd.red = 255; rd.green = 0; rd.blue = 0;

    function Make_Box(col,loc){

        var frame = doc.pathItems.rectangle(loc[1],loc[0],loc[2]-loc[0],loc[1]-loc[3]);

        frame.filled = false;

        frame.stroked = true;

        frame.strokeColor = col;

        frame.strokeWidth = 1;

    }

    function Add_Text(txt,loc){

        var text = doc.textFrames.add();

        text.contents = txt;

        text.position = [loc[0],loc[3] - 20];

    }

    for(var i = 0; i < ras.length; i++){

        try{

            var txt = ras.file.name + "\nX:" + (ras.geometricBounds[0]/TOmm).toFixed(2) + "\nY:" + (ras.geometricBounds[1]/TOmm).toFixed(2);

            Make_Box(gr,ras.geometricBounds);

            Add_Text(txt,ras.geometricBounds);

        }catch(e){

            var txt = "this file has been renamed or moved.\nPlease Relink";

            Make_Box(rd,ras.geometricBounds);

            Add_Text(txt,ras.geometricBounds);

        }

    }

}

Get_Link_Names();