Copy link to clipboard
Copied
Hi,
I'm unfamiliar with Lua and have just started hacking with Lightroom to automate some keywording. Apologies for possibly familiar 'code resuse', but I thought the result of this script would be a list of arKeyword rather than table
LrTasks.startAsyncTask( function()
local all_keywords=catalog:getKeywords()
if #all_keywords == 0 then return end
lrItem = 1
varFileName = ""
varFileNames = {}
local ProgressScope = ProgressScope({title = "My progress bar title" ,caption = "My progress bar caption",})
for j, a_keyword in ipairs(all_keywords) do
varFileName = varFileName .. type(a_keyword) .. ", "
lrItem = lrItem + 1
ProgressScope:setPortionComplete(lrItem/#all_keywords)
ProgressScope:setCaption( lrItem .. "/" .. #all_keywords .. ' ' .. j)
end
ProgressScope:done()
local f = LrView.osFactory()
local c =
f:row{
bind_to_object = props,
f:column {
f:edit_field { value = varFileName, width_in_chars = 80, height_in_lines = 10 },
},
}
dialogValue = LrDialogs.presentModalDialog(
{
title = "Filenames" ,
contents = c,
}
)
end)
but it actually generates:
I'm obviously doing something stupid, but what? Thank you!
"I'm not sure I totally understand why it failed but I've now learnt that I need to use :getName() rather than .getName() to get the name"
If you have an LrKeyword object stored in the variable k, then the expression k:getName() is equivalent to k.getName(k). A common mistake is to write k.getName(), in which case you'll get the error "assertion failed!", because no argument has been supplied to the getName() function.
Copy link to clipboard
Copied
So, I'm not sure I totally understand why it failed but I've now learnt that I need to use :getName() rather than .getName() to get the name
Copy link to clipboard
Copied
[This post contains formatting and embedded images that don't appear in email. View the post in your Web browser.]
Most objects in the LR SDK are represented as tables with methods that make them "opaque". For example:
This example shows that you can't use pairs() to iterate through the keys/values of such LR objects (that is, they are "opaque"):
Copy link to clipboard
Copied
"I'm not sure I totally understand why it failed but I've now learnt that I need to use :getName() rather than .getName() to get the name"
If you have an LrKeyword object stored in the variable k, then the expression k:getName() is equivalent to k.getName(k). A common mistake is to write k.getName(), in which case you'll get the error "assertion failed!", because no argument has been supplied to the getName() function.
Copy link to clipboard
Copied
Thank you so much John, and sorry for my delay in responding. Your advice helped me understand this subtle differences in Lua with other programming languages that I'm familiar with. I've progressed a lot with my script now once I'd used your advice to get past that confusion.