# Import to LR from external application



## Grinchman (Jul 20, 2012)

Hi!
Can't find a solution or any information about some interface or trick for importing photos from external application.
Situation is below:
I've created export plug-in. It exports selected photos from LR to my application. Application is processing that photos, creates new ones. Next I need to put that new photos to LR library (for example in section 'All Photographs'). There is exist some API or tequinics to update LR library from external application? Thanks in advance for your tips!


----------



## bobrobert (Jul 20, 2012)

I am not 100% sure what you are asking but I will give this answer. LR isn't a browsing application as such. So what I do if I want to add just one image to a catalogue is browse with Faststone - freeware - and when I see one I want to process then I press Alt + 2 on the keyboard and this opens all of the images in the folder chosen from my hard drive to LR but only the image chosen is ticked for adding to the catalogue.


----------



## Grinchman (Jul 20, 2012)

bobrobert said:


> I am not 100% sure what you are asking but I will give this answer. LR isn't a browsing application as such. So what I do if I want to add just one image to a catalogue is browse with Faststone - freeware - and when I see one I want to process then I press Alt + 2 on the keyboard and this opens all of the images in the folder chosen from my hard drive to LR but only the image chosen is ticked for adding to the catalogue.


Thanks for reply!
I asked not from user side but from software developer side. Thus I need programmer's tips, such as 'in your source code open this file for writing and put there some lines with information about new photos and then LR automatically will update library'.
If we'll look at your case, I`m interesting in what happenes in Faststone, what algorithm works, to open browsed photos in LR.


----------



## bobrobert (Jul 20, 2012)

I don't know anything about coding but I find it a useful way of selecting one image. There is another setting for opening directly to Photoshop. The browser is light on resources and a - imo - good browser. Don't need to use bridge or mini bridge. You can rate images but I only use it to browse.


----------



## johnbeardy (Jul 20, 2012)

Odd, I thought I'd posted an answer to this.

There is a Lightroom 4 SDK. However, it doesn't allow external control of LR features.

One alternative is setting up an auto import folder - a folder LR watches and automatically adds any new images to the catalogue. Another is to drag and drop items from another app into LR. This triggers the import dialog for those files. 

John


----------



## Fotografo Steel (Jul 20, 2012)

If you're really hardcore you could think about adding your images directly to the catalogue (what is "just" a SQLite database). However, I'm not sure if I'd recommend this approach for you need to know the internal DB structure quite well to do this.

(I wrote a program in the past _querying_ the DB, but a SELECT is much easier than an INSERT or UPDATE  )


----------



## johnbeardy (Jul 20, 2012)

I would recommend not writing directly to the database. 

John


----------



## clee01l (Jul 20, 2012)

Fotografo Steel said:


> (I wrote a program in the past _querying_ the DB, but a SELECT is much easier than an INSERT or UPDATE  )


And also more benign.  There are too many table dependencies to have a clear understanding of all of the ramifications of making changes outside of LR and the LR API. 

I don't think the OP has a clear understanding of what LR actually is.  It is not an editor like FastStone or PS.  You do not open an image file in LR. You open catalogs and the catalogs read and act upon images that are in the catalog's content.  The only proper file type that can be passed to LR through a call is the name of catalog not an individual image.


----------



## Fotografo Steel (Jul 20, 2012)

johnbeardy said:


> I would recommend not writing directly to the database.


I'm 100% at one with you.

But its a possibility, even if you should you *really really really* know, what you're doing.
Of course you should be aware that you could damage your catalog beyond any repair with this method, otherwise.


----------



## bobrobert (Jul 20, 2012)

clee01l said:


> And also more benign.  There are too many table dependencies to have a clear understanding of all of the ramifications of making changes outside of LR and the LR API.
> 
> I don't think the OP has a clear understanding of what LR actually is.  It is not an editor like FastStone or PS.  You do not open an image file in LR. You open catalogs and the catalogs read and act upon images that are in the catalog's content.  The only proper file type that can be passed to LR through a call is the name of catalog not an individual image.



Sorry have you read my original post? You can open an individual image via LR. A reference to that image is added to the catalogue and you can then edit it. I outlined the method for doing it in my post.


----------



## Fotografo Steel (Jul 20, 2012)

bobrobert said:


> You can open an individual image via LR. A reference to that image is added to the catalogue and you can then edit it. I outlined the method for doing it in my post.


However, Grinchman wasn't looking for a way to do it from FastStone, but a programatic (API) way to add this feature to his own application...

... unfortunately FastStone is not OpenSource and debugging trough its assembler code is probably not the main goal of grinchman...


----------



## Grinchman (Jul 23, 2012)

Fotografo Steel said:


> However, Grinchman wasn't looking for a way to do it from FastStone, but a programatic (API) way to add this feature to his own application...
> 
> ... unfortunately FastStone is not OpenSource and debugging trough its assembler code is probably not the main goal of grinchman...


You're absolutelly right! Moreover, I know one more application that imports images to LR - it's Photomatix. But it isn't open source too.


----------



## Fotografo Steel (Jul 23, 2012)

Without having looked deeper into that link the Lightroom SDK might be worth a glimpse:
http://www.adobe.com/devnet/photoshoplightroom.html


----------



## johnbeardy (Jul 23, 2012)

And as I said before "there is a Lightroom 4 SDK. However, it doesn't allow external control of LR features". I'm pretty certain that Photomatix and Nik are not importing files to LR, and that what's happening is that an LR plugin sends instructions to the external app and then imports the results.


----------



## DawMatt (Jul 24, 2012)

Lookup the SDK documentation for LrCatalog, and in particular catalog:addPhoto().

As John said Photomatix etc are sending images to an external application for processing, then importing the processed images probably via this API call. This is all driven from the Lightroom side (pushing images out, and pulling modified images back in). As you seem to be exporting images to an external application for processing and want to import the results you could try the same approach.

Keep in mind that you need to think of a way to ensure the images are written where you expect, with names you expect, and you know when to look for/import them. This might require some level of communication between your plugin and external application to co-ordinate the import. 

Matt


----------



## Grinchman (Jul 24, 2012)

DawMatt said:


> Lookup the SDK documentation for LrCatalog, and in particular catalog:addPhoto().
> 
> As John said Photomatix etc are sending images to an external application for processing, then importing the processed images probably via this API call. This is all driven from the Lightroom side (pushing images out, and pulling modified images back in). As you seem to be exporting images to an external application for processing and want to import the results you could try the same approach.
> 
> ...



Probably that's the only right way to import images. My problem is that I still can't find a way to make a sort of 'callback' from application to plugin. To export images I use ExportServiceProvider which call LrShell.openFilesInApp function. But how to make a back call from app?
Suppose, Application has saved an image to some location and now it must give the path to that file for LR plugin.


----------



## johnbeardy (Jul 24, 2012)

You might try saving the file in a watched folder, or a command line call to LR including the file path. But LR will have already created a TIF used by the external app, so as usual it's probably better to use LR to move the file.


----------



## DawMatt (Jul 25, 2012)

Grinchman said:


> Suppose, Application has saved an image to some location and now it must give the path to that file for LR plugin.


A few options off the top of my head:
- Import via convention. If you can guarantee that all new files will have a certain name or naming convention, or be saved in a certain location, you could just check for the expected file/file location at regular intervals and import whatever is found there.
- Avoid importing all together. As John mentioned, LR has already created an output file for you. If you chose the "add image to catalog" option when exporting the file, and then overwrite or replace the input file with the results, Lightroom should automatically detect the change and update the thumbnail at some point.
- Import via inter-process communications. There are limited ways to pass information back into Lightroom from an external program. One of the simplest would be for your application to write instructions into a text file in a known location. Your plugin would then check the file for instructions regularly, perform them and delete the instructions from the file.


----------



## Grinchman (Jul 25, 2012)

DawMatt said:


> A few options off the top of my head:
> - Import via convention. If you can guarantee that all new files will have a certain name or naming convention, or be saved in a certain location, you could just check for the expected file/file location at regular intervals and import whatever is found there.
> - Avoid importing all together. As John mentioned, LR has already created an output file for you. If you chose the "add image to catalog" option when exporting the file, and then overwrite or replace the input file with the results, Lightroom should automatically detect the change and update the thumbnail at some point.
> - Import via inter-process communications. There are limited ways to pass information back into Lightroom from an external program. One of the simplest would be for your application to write instructions into a text file in a known location. Your plugin would then check the file for instructions regularly, perform them and delete the instructions from the file.



I like the third version!
After plugin launch my application it will start an timer to watch for instruction file. But quick googling doesn't give me an simple solution for timer in lua. It exists?


----------



## DawMatt (Jul 25, 2012)

Grinchman said:


> But quick googling doesn't give me an simple solution for timer in lua. It exists?


Look up LrTasks in the SDK's API Reference. LrTask.sleep in particular. just make sure you do all this work in an async task so you don't freeze your UI.


----------



## Grinchman (Jul 31, 2012)

So, almost all stuff works fine - export image to my application, pass new file name from my application to my LR plugin via interprocess communication text file.
 The only one thing is slowing me is _catalog.addPhoto_ method! It doesn't works! At some point of plugin script I read string from text file. This string contains path to file created by my application. I check that path on existence by LrFileUtils.exists(file_path). Path is exists!
Then, according to LR SDK documentation I call addPhoto like that:
*LrCatalog.withWriteAccessDo("Import from Application", LrCatalog.addPhoto(file_path))
*But I got error: 
_Unable to export: An internal error has occured: LrCatalog.addPhoto: path parameter is missing or is not a string.
_
What that means? Why variable file_path can be not a string or something else?


----------



## johnbeardy (Jul 31, 2012)

It usually works. You've probably made a small typing error or the variable is out of scope.


----------



## Grinchman (Jul 31, 2012)

johnbeardy said:


> It usually works. You've probably made a small typing error or the variable is out of scope.


And I thought so! And continue to think so! But I have that I have. Let's check my code. Maybe with your fresh look we'll find an bug in code?


> local file_path = LrFileUtils.readFile(lr_path)
> if (file_path ~= "") then
> if(LrFileUtils.exists(file_path)) then
> LrCatalog.withWriteAccessDo("Import from Application", LrCatalog.addPhoto(file_path))
> ...


----------



## johnbeardy (Jul 31, 2012)

Nothing obvious. I think it would scream, but is the code inside a task?

I assume there's nothing like a carriage return in lr_path's contents and it simply contains the path.

Maybe just write file_path to a log - just in case it's not nil. Nil and not "" are different.

Is the photo already in the catalogue? That might mean you don't see anything happening. Test with LrCatalog:findPhotoByPath( file_path)


----------



## Grinchman (Jul 31, 2012)

Sure, I've logged file_path just before call addPhoto and it was ok - full path to .jpg file.
I've tried LrCatalog:findPhotoByPath and it throws the same error! It means that the problem is file_path variable.

There is how I log that variable:


```
if(LrFileUtils.exists(file_path)) then
      AppExportServiceProvider.log("File path is exists: " .. file_path )
      LrCatalog.withWriteAccessDo("Import from Application", LrCatalog.addPhoto(file_path))
end
```
And in log file I see correct string:



> 7/31/2012 16:52:39 TRACE     File path is exists: D:\Projects\MyApplication\DSC01891_hdr.jpg



The function where it happens looks like that:


```
AppExportServiceProvider.waitFileFromApp = function(app_path)
```

And here is how it called:


```
LrTasks.startAsyncTask(AppExportServiceProvider.waitFileFromApp(app_path))
```


----------



## Grinchman (Jul 20, 2012)

Hi!
Can't find a solution or any information about some interface or trick for importing photos from external application.
Situation is below:
I've created export plug-in. It exports selected photos from LR to my application. Application is processing that photos, creates new ones. Next I need to put that new photos to LR library (for example in section 'All Photographs'). There is exist some API or tequinics to update LR library from external application? Thanks in advance for your tips!


----------



## johnbeardy (Jul 31, 2012)

Might it be that somewhere the "\" is being treated as an escape character?


----------

