6 Replies Latest reply on Apr 15, 2008 1:50 PM by (Jay_Sparks)

    JS CS2: Image Link

      Is it possible to store in a variable the file name of an image that is selected in a document?

      Cheers
      Norbert
        • 1. Re: JS CS2: Image Link
          Sorry forgot to mention that the selected item could be jpeg/pdf/eps tiff.

          Regards
          Norbert
          • 2. Re: JS CS2: Image Link
            I found this code snippet in the forum but this only works on jpegs or tiffs and not on eps/ai or tiffs.

            Any help will be much appreciated.

            Cheers
            Norbert

            var myString = "";
            myDoc=app.activeDocument
            myImage = myDoc.selection[0].images[0];
            myCurrLink = myImage.itemLink;
            myName = getFileNameOnly (myCurrLink.name);
            alert(myString);

            function getFileNameOnly (myFileName)
            {
            var myResult = myFileName.lastIndexOf(".");
            if (myResult == -1)
            {
            myString = myFileName;
            }
            else
            {
            myString = myFileName.substr(0, myResult);
            }
            }
            • 3. Re: JS CS2: Image Link
              Peter Kahrel Adobe Community Professional & MVP
              For EPS and AI, use epss[0] instead of images[0]

              Peter
              • 4. Re: JS CS2: Image Link
                Peter Kahrel Adobe Community Professional & MVP
                ... but then you need to know the type of image, which could be epss, images, picts, pdfs, or wmfs (maybe some more). It's safer to avoid that and use "graphics" instead:

                try
                {
                alert (File (app.selection[0].graphics[0].itemLink.filePath).name)
                }
                catch (_)
                {
                alert ("Selection doesn't contain any images.")
                }

                Peter
                • 5. Re: JS CS2: Image Link
                  Thanks very much Peter!!
                  • 6. Re: JS CS2: Image Link
                    Something like this may work too...<br /><br />var filenameVariable = ""; <br />g = app.activeDocument.allGraphics;<br />for (i=0; i<g.length; i++){<br />     if (g[i].itemLink == null) {<br />     i++;}<br />     else {<br />     filenameVariable = g[i].itemLink.name;<br />}<br />}