Saturday, November 11, 2017

Importing a collection in Lightroom using SQLite

Sometimes I need to convert a list of files into a Lighroom collection: I usually select some files in Lightroom using computer A, say I add them to a collection, but shortly I need to move to computer B (which is a clone of A) and I need to take the list with me.

In theory, I could export the collection as a catalog, but most of the time, when I try to import it, I get some conflict warning, and I don't want to risk to overwrite picture settings: I'd like to add just my collection, and nothing else.

One option is as follows: export the collection as a catalog (without the pictures, just the catalog).
Then open the catalog in sqlite and run the following query:

select printf('{{ criteria = "filename", operation = "beginsWith", value = "%s", value2 = "", }, {criteria = "folder", operation = "words", value = "%s", value2 = "", }, combine = "intersect",},', AgLibraryFile.baseName || '.' || AgLibraryFile.extension, replace(AgLibraryFolder.pathFromRoot,'/',' ')) from AgLibraryFile inner join AgLibraryFolder on AgLibraryFile.folder == AgLibraryFolder.id_local;


On windows you might have to replace the path separator '/' with '\'.

If you paste the output of this query into a template file called "List of files.lrsmcol", you can easily generate a smart collection that contains all of your selected files, and smart collections can be imported.


s = {
id = "A0FA4F65-C596-4D9E-94B8-C2BB5B6D3740",
internalName = "List of files",
title = "List of files",
type = "LibrarySmartCollection",
value = {

// paste the query output here

combine = "union",
},
version = 0,
}

This can be easily be generalized to work with any list of files.