Skip to main content
Known Participant
December 23, 2009
Answered

How do I get the caption (Description) out of an image's metadata?

  • December 23, 2009
  • 1 reply
  • 5900 views

Taking a break from a monstrous project that has had me tied up for days, I have embarked upon what I thought would be a bit of light relief (hah!).

A colleague asked me for an easy way to get captions into InDesign. So I started scripting there, asking for the "description of the link xmp".

This was not forthcoming and a search of the InDesign scripting forum yielded the information that in order to get this information it was necessary to open the doc in Photoshop, save it and re-link. This indeed does work, but it is a waste of time in the file-saving stage and also degrades the image, as we have a jpg workflow.

So I thought that if I was going to have to got to Photoshop anyway, why not have the script go straight there, open the doc, ask for the description and close without saving?

Photoshop so far has jealously guarded its XMP information.

In Applescript I ask go "set theData to raw data of XMP metadata of document 1" and there it is in the event log, but in forbidding red type. I try adding "as string" and it appears twice, once in friendly black type, enclosed in quotes, and then the red stuff. However I find myself refused access to the information in the variable.

AS or JS solutions welcome...

This topic has been closed for replies.
Correct answer Muppet_Mark-QAl63s

Thanks for the responses. I am heading home from the office, where scripts are welcome but scripting isn't, to try out your offerings.

No, Mark, I didn't have time to write to the other list. I thought about approaching this from the Photoshop side of the fence on my way in to work and made a rather rushed attempt before the boss arrived

I do most of my scripting between 6.30 and 9 in the morning. Then it's off to play a full role in a production environment.


I could not say for newer versions of CS than my CS2 but XMP description is the same value as file info caption?

tell application "Adobe Photoshop CS2"

set docRef to the current document

tell docRef

set x to caption of info

display dialog x giving up after 2

end tell

end tell

1 reply

Inspiring
December 23, 2009

I don't script InDesign but dees the link info have the path to the linked file? If it does you can have Photoshop get the metadata without opening the file ( CS4 ) or you might be able to have Bridge do it.

If InDesign supports external libraries you might be able to just load XMPLibrary and get the metadata in InDesign.

Here is the code to get the description from a file without opening the file in Photoshop.

function getDescription( file ){// File object
    try{
        loadXMPLibrary();
        var xmpf = new XMPFile( file.fsName, XMPConst.UNKNOWN, XMPConst.OPEN_FOR_UPDATE );
        var xmp = xmpf.getXMP();
          try{
               var desc = xmp.getArrayItem(XMPConst.NS_DC, "description", 1).toString();
          } catch(e) {
               var desc = '';
          }
          xmpf.closeFile();
          return desc;
          }catch(e){
               unloadXMPLibrary();
               return -1;
          }
     unloadXMPLibrary();
}


function loadXMPLibrary(){
     if ( !ExternalObject.AdobeXMPScript ){
          try{
               ExternalObject.AdobeXMPScript = new ExternalObject
                                                            ( 'lib:AdobeXMPScript' );
          }catch (e){
               alert( ErrStrs.XMPLIB );
               return false;
          }
     }
     return true;
}
function unloadXMPLibrary(){
     if( ExternalObject.AdobeXMPScript ) {
          try{
               ExternalObject.AdobeXMPScript.unload();
               ExternalObject.AdobeXMPScript = undefined;
          }catch (e){
               alert( ErrStrs.XMPLIB );
          }
     }
}

Muppet_Mark-QAl63s
Inspiring
December 23, 2009

tell application "Adobe InDesign CS2"

activate

tell active document

set This_Selection to get selection

try

if This_Selection {} then

if length of This_Selection 1 then

display dialog "You have more than 1 item seleted!!!" giving up after 2

else

if class of item 1 of This_Selection is image then

set This_Image to item 1 of This_Selection

end if

if class of item 1 of This_Selection is rectangle then

set This_Image to image 1 of item 1 of This_Selection

end if

set Image_Path to file path of item link of (item 1 of This_Image)

set MDLS_Result to my Get_XMP_Description(Image_Path)

set {ASTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, " = "}

set Image_Desc to text item 2 of MDLS_Result

set AppleScript's text item delimiters to ASTID

display dialog Image_Desc giving up after 2

end if

else

display dialog "You have 0 items seleted!!!" giving up after 2

end if

on error

display dialog "This rectange contains NO image!!!" giving up after 2

end try

end tell

end tell

on Get_XMP_Description(File_Path)

try

do shell script "/usr/bin/mdls -name kMDItemDescription " & quoted form of POSIX path of File_Path

return the result

on error

return false

end try

end Get_XMP_Description

c.pfaffenbichler
Community Expert
Community Expert
December 28, 2009

The AppleScript was based on something that i was already using. And should work for single item user selection in ID. The shell's mdls & mdfind are by a country mile my personal favorites on the mac os (look up the man pages on these). I use these for almost all my local file filtering (lightning fast when I move to a CS that will execute shells I hope to have these available in maybe Ruby but thats another story).

The JavaScript would be the way I would try to go now (just for the learning exercise) this was just rushed out to show you that you have access to this info. It was looking for this info in the first link item and should have wrote it back out to the ESTK JavaScript Console.

I find the ESTK is quite difficult to work with (by comparison to Apple's Script Editor) IMO they should have got this like LateNight's Script Debugger 4.5 (a dream for the AppleScript user) I find myself having to use $.write(blah); || alert(blah); all over the place so's I can get an idea of where Im up to. Not convinced Im even using it right yet…

Spent almost all day yesterday looking at bridgetalk again!!! failed again!!! AppleScript is going to have to handle my IAC for the foreseeable future. Have a good holidays…


Mark, what are You trying to achieve with BridgeTalk?

I myself am not great with it, but I’ve found an Indesign-Script by Kasyan Servetsky that provided me with the necessary framework.

And now I even use it to drag ai-files through Photoshop to measure the actually visible bounds of illustrations – which seems to be out of reach for Illustrator-Scripting itself (what with Clipping Paths and Raster Effects).