--[[
Changes all keywords in the catalogue to title case - ie first letter of each word will be capitalised
SETUP INSTRUCTIONS
Temporarily save this file onto your desktop and call it "File path to keywords.lua".
Now you need to create a scripts folder:
In Lightroom, choose Lightroom > Preferences (Mac OS) or Edit > Preferences (Windows).
Choose the Preset tab and select Show Lightroom Presets folder.
Create a folder in the Lightroom folder called Scripts.
Copy the "File path to keywords.lua" script into the Scripts folder.
Quit and reopen Lightroom.
You should now see a little scripts menu to the right of the Help menu. It has a single item Title case keywords.
--]]
------------------------------YOU CAN CHANGE THESE
local scriptTitle = "File path to keyword"
local scriptPrefix = "~"
local includeFileName = true
local allowExport = true
------------------------------DON'T TOUCH BELOW HERE
local LrFunctionContext = import 'LrFunctionContext'
local LrTasks = import 'LrTasks'
local catalog = import "LrApplication".activeCatalog()
local ProgressScope = import 'LrProgressScope'
local LrDialogs = import 'LrDialogs'
local LrPathUtils = import 'LrPathUtils'
if MAC_ENV then SEP = '/' else SEP = '\\' end
function fileName_to_keywords()
local photos=catalog:getTargetPhotos()
local ProgressScope = ProgressScope({title = scriptTitle ,caption = "",})
for j, photo in ipairs(photos) do
topKeyword = CreateParentKeyword()
varPath = photo:getRawMetadata('path')
if includeFileName == true then
varPath = LrPathUtils.parent( varPath )
end
local folders ={}
folders = split(varPath , SEP )
for e, folder in ipairs(folders ) do
catalog:withWriteAccessDo( scriptTitle , function()
topKeyword = catalog:createKeyword( folder , {} , allowExport , topKeyword , true)
end)
catalog:withWriteAccessDo( scriptTitle , function()
photo:addKeyword( topKeyword )
end)
end
ProgressScope:setPortionComplete(j/#photos)
ProgressScope:setCaption( j .. "/" .. #photos .. ' ' .. photo:getFormattedMetadata('fileName'))
end
ProgressScope:done()
end
function CreateParentKeyword()
catalog:withWriteAccessDo( scriptTitle , function()
topKeyword = catalog:createKeyword( scriptPrefix .. scriptTitle , {} , false, nil , true)
while not topKeyword do
LrTasks.yield()
end
end)
return topKeyword
end
function split(str, pat)
local t = {} -- NOTE: use {n = 0} in Lua-5.0
local fpat = "(.-)" .. pat
local last_end = 1
local s, e, cap = str:find(fpat, 1)
while s do
if s ~= 1 or cap ~= "" then
table.insert(t,cap)
end
last_end = e+1
s, e, cap = str:find(fpat, last_end)
end
if last_end <= #str then
cap = str:sub(last_end)
table.insert(t, cap)
end
return t
end
LrTasks.startAsyncTask( function()
fileName_to_keywords()
end)