20 Replies Latest reply on Jun 15, 2013 12:08 PM by jarnoh

    Read metadata from file

    Alex_fra

      Hi,

       

      I have a plugin which executes ExifTool for some selected pictures. After ExifTool returns, I would like to reload the metadata from the changed files, so the change becomes visible to the user right away.

      Basically, let my plugin perform the task of right-clicking an image and choose 'Metadata' -> 'Reload metadata from file'.

       

      Unfortunately I haven't found a way in the API to do so. Can you point me to the right place?

       

      Thanks,

      Alex

        • 1. Re: Read metadata from file
          areohbee Level 5

          I don't think the API supports it. Keyboard stuffing is only way I know to do it. e.g. AutoHotKey(Windows) - dunno 'bout Mac.

          • 2. Re: Read metadata from file
            Alex_fra Level 1

            Hey Rob,

             

            thanks for your quick answer. But what the heck is an autokey script? :-)

            Is there any reading material about it?

             

            Alex

             

            (And on a sidenote: why isn't such a basic functionality in the API? Especially as it is possible to execute third party applications which most probably are invoked to apply some changes to the pictures or its metadata. I am a bit puzzled...)

            • 3. Re: Read metadata from file
              areohbee Level 5

              Look up AutoHotKey on the net. It stuffs keystrokes into the keyboard buffer, simulating a user's typing.

              • 4. Re: Read metadata from file
                john beardsworth Adobe Community Professional & MVP

                It depends what you are trying to do. If you want to modify adjustment metadata, you can't. If you want to read descriptive metadata, make your plug-in tell exiftool to generate an xmp extract, parse that, and update the appropriate fields.

                 

                John

                • 5. Re: Read metadata from file
                  areohbee Level 5

                  Alex_fra wrote:

                   

                  ...why isn't such a basic functionality in the API?

                   

                  All I know is that it is not my fault - I requested functions to save & read metadata be added to the API.

                   

                  Rob

                  • 6. Re: Read metadata from file
                    areohbee Level 5

                    Begging the question for Alex - can you have your plugin update the metadata fields directly? or does it have to be a Lightroom Read Metadata?

                    • 7. Re: Read metadata from file
                      Alex_fra Level 1

                      Here is some more information on what I am trying to do:

                       

                      I have a G1 camera and an adapter to use an old manual Minolta AF lens with it. The pair produces nice pictures, however, since the adapter is mechanically only, the camera isn't able to write the Lens, ApertureValue and FocalLength values into the EXIF info of the photo.

                       

                      That's where LR comes into the picture. I have all my pictures in LR and want to add the Lens info manually to the raw files. Since LR doesn't allow it (only IPTC data), I was looking for a different way to do this. I ended up with writing a plugin for that task. What the plugin does, is simply present a dialog to the user to let him enter the lens info, retrieves the list of selected photos and executes ExifTool for each picture. This works fine, but the newly written EXIF info does not show up in LR sidebar in the EXIF view. It, of course, only comes up as soon as you click on 'Reload Metadata from Files'.

                       

                      What I was looking for, is a method to to the last step automatically by the plugin. This, however, seems to be a problem. You told me that there is no API functionality for it and even HotKey scripting is not a solution, since this menu entry has no hotkey assigned to it. :-(

                       

                      Any other suggestions?

                      (but thanks so far!)

                       

                       

                      ---

                       

                      local function updateExif()

                       

                           -- get the path of the selected photo

                           catalog = LrApplication.activeCatalog()

                           photos = catalog:getTargetPhotos()

                       

                           -- invoke ExifTool for each picture

                           for loop, photo in ipairs(photos) do

                                pathName = photo:getRawMetadata('path')

                                LrTasks.execute('exiftool -overwrite_original -Lens=\"' .. properties.lens .. '\" -FocalLength=\"' .. properties.focalLength .. '\" -ApertureValue=\"' .. properties.aperture .. '\" ' .. pathName)

                           end

                       

                           LrDialogs.message( "EXIF Complete", "Done processing " .. #photos .. " pictures. You have to reload the metadata from the files now.", "info" );

                      end

                      • 8. Re: Read metadata from file
                        areohbee Level 5

                        OK.

                         

                        You need the Lr:Read-Metadata function.

                         

                        But you can use the auto-hot-key script to do it, in fact I am doing just that in one of my (Jeff's that I helped with) plugins.

                         

                        Alt-M-R once you are in the Library module does it, and Ctrl-Alt-1 will take you to the library module.

                        (these are for windows - translate if you need mac equivalents)

                         

                        Somebody else (Jeff) wrote the script so I remain inexperienced personally, but it can be done!

                         

                        Rob

                        • 9. Re: Read metadata from file
                          JW Stephenson Level 4

                          Alex,

                           

                          Like Rob, I too would like Lr:SaveMetadata and Lr:ReadMetadata functions (and a few more of course).  I had the same problem as you whereby I had scans that I wanted to enter the scanner name and model, and create a CreateDate as well.  The code I have under JeffScanMetadataFill.lua does basically what you are doing in your code above except I execute a keyboard macro (AutoHotKey) before to save the metadata at that state, then I change the fields in the image file via Exiftool like you do, then I execute another keyboard macro to read the metadata from the file.

                           

                          These macros are a bit risky since they don't always know the state of your computer when they are running but I have found out that by selecting a small subset of images (less than 100) and not touching the computer while they are running I have had no problems.

                           

                          I don't know how to attach files as I would like to share the lua code and the autohotkey scripts.  I am traveling and my email is jeff.laptop at cox.net if you wish to email me I can send you a zip file.  Or if someone knows how to attach a zip file to this forum I would be happy to post.

                           

                          Bottom line, it can be done but I hope for a LR solution soon.

                           

                          Jeff

                          • 10. Re: Read metadata from file
                            Olli H.

                            Hello Jeff,

                             

                            although not being the original thread opener, I am having the same problem. Does your offer to mail/post your script code still stand?

                             

                            to all: Did LR4 bring the desired functionality to read and write metadata to files by now?

                             

                            Thank you!

                            Olli

                            • 11. Re: Read metadata from file
                              DawMatt Level 3

                              Olli H. wrote:

                               

                              to all: Did LR4 bring the desired functionality to read and write metadata to files by now?

                               

                              LR 4 provides the ability to read/write more metadata fields using the existing LrPhoto get and set raw/formatted metadata functions. But there are still a lot of metadata items that are inaccessible via the SDK.

                               

                              Matt

                              • 12. Re: Read metadata from file
                                areohbee Level 5

                                Here's the autohotkey script I use:

                                 

                                /*

                                    SendKeysAHK.ahk

                                */

                                 

                                 

                                /*

                                    Get unique window identifier of Lightroom's main app window.

                                 

                                    Initial motivation: Make sure Lightroom will be the recipient of emitted keystrokes.

                                   

                                    So far, for titles, we have:

                                        {Catalog} - Adobe Photoshop Lightroom - {Module}

                                        Lightroom

                                        Lightroom (x%)

                                       

                                    ###1 Not sure how to handle case if a dialog box is open, plugin or otherwise.

                                       

                                    ahk_class is AgWinMainFrame

                                */

                                GetLightroomWindowID()

                                {

                                    lr_win_id := 0

                                    Process, Exist, lightroom.exe

                                    lr_pid := ErrorLevel

                                 

                                    if lr_pid {

                                        WinGet, id, list, ahk_pid %lr_pid%

                                    } else {

                                        WinGet, id, list,,, Program Manager

                                    }

                                    Loop, %id%

                                    {

                                        this_id := id%A_Index%

                                       

                                        WinGetClass, this_class, ahk_id %this_id%

                                       

                                        if (this_class = "AgWinMainFrame") {

                                            ; MsgBox Got Lr Frame type

                                        } else {

                                            continue

                                        }

                                       

                                        WinGetTitle, this_title, ahk_id %this_id%

                                       

                                        if InStr( this_title, "Lightroom" ) {

                                            if (lr_win_id = 0) {

                                 

                                                lr_win_id := this_id

                                 

                                            } else { ; redundent

                                                lr_win_id := 0

                                                break

                                            }

                                        }

                                    }

                                    return lr_win_id

                                }

                                 

                                 

                                 

                                /*

                                    Make sure Lightroom will be the recipient of emitted keystrokes.

                                */

                                LightroomActivate()

                                {

                                    lr_win_id := GetLightroomWindowID()

                                    if lr_win_id {

                                        WinActivate, ahk_id %lr_win_id%

                                        return 1

                                    } else {

                                        return 0 ; false

                                    }

                                }

                                 

                                 

                                 

                                if LightroomActivate() {

                                    Send %1%

                                } else {

                                    MsgBox Unable to activate Lightroom window.

                                }

                                 

                                Compile it using AutoHotkey into a "SendKeys.exe" file and invoke via command line: SendKeys keys...

                                 

                                It will make sure Lr is the recipient (so as not to be sending keys to other apps/windows), but it can't target Lr main if Lr dialog box is open...

                                 

                                I use AppleScript to do similar things on Mac, but Mac version of Lr has no keyboard command for reading metadata.

                                 

                                R

                                • 13. Re: Read metadata from file
                                  Olli H. Level 1

                                  Thank you Rob for posting the script!

                                   

                                  Unfortunatley I am using a Mac. This probably means I still will be stuck with a dialog box to inform the user to reload meatdata manually... :-(

                                  • 14. Re: Read metadata from file
                                    areohbee Level 5

                                    Olli H. wrote:

                                     

                                    Unfortunatley I am using a Mac. This probably means I still will be stuck with a dialog box to inform the user to reload meatdata manually... :-(

                                    Yep, unless you or somebody else can think of another way...

                                    • 15. Re: Read metadata from file
                                      jarnoh Level 1

                                      One could write some AppleScript to trigger the menu item.  In fact, I might have it somewhere...

                                      • 16. Re: Read metadata from file
                                        areohbee Level 5

                                        Mac version doesn't have keystrokes for reading metadata (saving metadata: yes, reading metadata: no). So you have to do more than emit keystrokes via script...

                                         

                                        Windows version does (have keystrokes for reading metadata), but Mac version: nope, unfortunately.

                                        • 17. Re: Read metadata from file
                                          jarnoh Level 1

                                          Yes, AppleScript can also do more than keystrokes, you can get process's menu bar and click items on it.  Only gotcha is that you also need to "enable access for assitive devices".  You can also do that from AppleScript, but it'll prompt for the admin password.

                                           

                                          Note that metadata is in different menus when in Library or Develop and the item title changes to plural if multiple photos are selected. 

                                          • 18. Re: Read metadata from file
                                            jarnoh Level 1

                                            LR5 brings updates to LrPhoto: there is now photo:saveMetadata() and photo:readMetadata() methods which write XMP files.

                                            Attempting to write virtual copy's metadata results in error, and readMetadata seems to bring up the user confirmation dialog.

                                            • 19. Re: Read metadata from file
                                              areohbee Level 5

                                              Awesome - thanks JarnoH - mind if I ask how you figured that out? strings??

                                               

                                              PS - I hope there's a batch version 'cause doing one-at-a-time this way is *extremely* inefficient if you have more than a few.

                                               

                                              Thanks,

                                              Rob

                                              • 20. Re: Read metadata from file
                                                jarnoh Level 1

                                                Yep,

                                                 

                                                strings LrPhoto.lua