# managing "export" setting of many keywords



## dap (Jul 3, 2011)

Is there any way to bulk-edit the "export" property of many keywords?

I haven't been careful about correctly setting the "export" property for keywords, so nearly all of them are set to be exported.  But I always check "minimize embedded metadata" on export so they don't show up in the exported image.  Now I want to start including metadata and most of the keywords, but I've got large groups of keywords (which are pretty much arranged hierarchically) that I _don't _want exported.  Setting the parent to not be exported doesn't seem to prevent the others from being exported -- it looks like I have to set it on each one individually.  Is there any way to bulk-edit the keywords so that they aren't exported?

I also tried exporting, editing the text file, and importing again, but this had no effect.

I've heard you can edit the SQLite database directly, but I don't really want to much with that unless there's a supported way to do it.

Thanks in advance!


----------



## clee01l (Jul 3, 2011)

dap said:


> Is there any way to bulk-edit the "export" property of many keywords?...
> I also tried exporting, editing the text file, and importing again, but this had no effect.
> 
> I've heard you can edit the SQLite database directly, but I don't really want to much with that unless there's a supported way to do it.


Welcome to the forum.  These are the only two ways that I can think of to manipulate the attributes of the keywords other than one by one.  I'm not sure if method 1 (export/import) will work but it ought to be possible. Method number 2 (Update the SQLite table(s) ) will work and there are various SQL managers that you can use to edit the catalog directly.  It is always safe to work with a copy of the catalog. That way if your SQL updates break referential integrity, you have the original catalog copy to fall back on. 
If you are interested in following up on method 2 with a copy of your current catalog, I can probably produce the SQL statement to update the proper table(s).


----------



## dap (Jul 3, 2011)

clee01l said:


> Welcome to the forum.  These are the only two ways that I can think of to manipulate the attributes of the keywords other than one by one.  I'm not sure if method 1 (export/import) will work but it ought to be possible. Method number 2 (Update the SQLite table(s) ) will work and there are various SQL managers that you can use to edit the catalog directly.  It is always safe to work with a copy of the catalog. That way if your SQL updates break referential integrity, you have the original catalog copy to fall back on.




Thanks for the advice.  Method (1) doesn't work to change the "export" setting.  I'm wary of method (2), but if that's the only way, I might just have to go for it.

-- Dave


----------



## johnbeardy (Jul 3, 2011)

You're right to be wary - see here. You can do it through the SDK mechanism and it is the supported way.

John


----------



## dap (Jul 3, 2011)

John,

Thanks for the tip.  I looked into the SDK and wrote a plugin to do precisely this.  I'm attaching it here for future reference, though it should be noted that I've never seen Lua nor the LR SDK before this morning, so it should be used with caution.

To use this script I wrapped it inside a plugin that just adds a Library menu item to invoke this script:


```
--
-- This script is provided for reference only.  Do *not* run this without
-- understanding how it works.  It was written by an amateur.
-- 
-- This script is intended to hide keywords whose top-level parent keyword is
-- not hidden (marked for export), and warn about keywords which are hidden
-- whose top-level parent is not hidden.  That is, this script iterates the
-- keywords in the active catalog.  For each one, if its top-level parent
-- keyword is marked for export but this keyword is not, the script prints a
-- warning.  If its top-level parent keyword is not marked for export but the
-- keyword itself is, this script unmarks the keyword for export.
--


local LrDialogs = import 'LrDialogs'
local LrLogger = import 'LrLogger'


local log = LrLogger( 'dapLogger' )
log:enable( "logfile" )
log:trace("BEGIN")


-- Track how many keywords we've hidden.
local nhidden = 0;


--
-- Hide the given keyword and its children (recursively).
--
local function fixKeyword(keyword)
    local attrs = keyword:getAttributes()


    if not attrs.includeOnExport then
        log:trace("skipping '" .. keyword:getName() .. "' (already not exported)")
    else
        log:trace("hide keyword '" .. keyword:getName() .. "'")
        keyword:setAttributes({ includeOnExport = false });
        nhidden = nhidden + 1
    end


    for key,child in pairs(keyword:getChildren()) do
        fixKeyword(child)
    end
end


--
-- Check that the given keyword and its children are not hidden (recursively).
--
local function checkKeyword(keyword)
    local attrs = keyword:getAttributes()


    if not attrs.includeOnExport then
        log:trace("surprised to find '" .. keyword:getName() .. "' not exported");
    end


    for key,child in pairs(keyword:getChildren()) do
        checkKeyword(child)
    end
end


--
-- Body of this operation: start an asynchronous task that modifies the catalog
-- as described above.
--
import "LrTasks".startAsyncTask(function ()
    local catalog = import "LrApplication".activeCatalog()
    local allkeywords = catalog:getKeywords()


    catalog:withWriteAccessDo('Hide specific keywords', function ()
        for key,keyword in pairs(allkeywords) do
            local attrs = keyword:getAttributes()
            if not attrs.includeOnExport then
                fixKeyword(keyword)
            else
                checkKeyword(keyword)
            end
        end
    end)


    LrDialogs.message('Hid ' .. nhidden .. ' keywords.', '', 'info')
    log:trace("END")
end)
```


----------



## johnbeardy (Jul 3, 2011)

You did that quickly! It took me ages to get my head around Lua!John


----------



## clee01l (Jul 3, 2011)

johnbeardy said:


> You did that quickly! It took me ages to get my head around Lua!John


And some of us still don't have a handle on the SDK. I'm envious!


----------



## dap (Jul 3, 2011)

johnbeardy said:


> You did that quickly! It took me ages to get my head around Lua!John




I still wouldn't say I know much Lua  But having written a lot of code in other languages, I was able to cobble together enough to solve this narrow problem.

Thanks again for the tip!  Makes me think there's an opportunity for someone to write a better keyword management interface for Lightroom.

-- Dave


----------



## Kiwigeoff (Jul 3, 2011)

dap said:


> Makes me think there's an opportunity for someone to write a better keyword management interface for Lightroom.
> 
> -- Dave


I agree a whole window for keyword management would be brilliant, something like the Import window. A post on the bug report forum linked at the top of this page would garner many votes I believe.


----------

