Skip to main content
Participating Frequently
August 2, 2024
Answered

catalog:getKeywords() seem to return a table of tables?

  • August 2, 2024
  • 1 reply
  • 326 views

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!

This topic has been closed for replies.
Correct answer johnrellis

"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.

1 reply

Participating Frequently
August 2, 2024

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

johnrellis
Legend
August 2, 2024

[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"):

 

 

 

johnrellis
johnrellisCorrect answer
Legend
August 2, 2024

"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.