Skip to main content
Inspiring
January 31, 2024
Answered

dynamic popup_menu

  • January 31, 2024
  • 1 reply
  • 360 views

I have search this forum and found some relating questions, but it does not help me.

https://community.adobe.com/t5/lightroom-classic-discussions/dynamic-table-for-popup-menu/m-p/4298429

 

I have a text file that contains multiple lines:

{ title = "Nikon F80s", value = "NF80" },
{ title = "Nikon ZF II", value = "NZFII" },

I read the file into a variable:

for line in io.lines(file) do 
	lines[#lines + 1] = line
  end

then in my function:

cameraContent = {}
local cameraFile = _PLUGIN.path .. '/lists/cameraList.txt'
local cameraContent = lines_from(cameraFile)
---
....
LrFunctionContext.callWithContext ("CreateDir", function (context)
		local f = LrView.osFactory ()
		local properties = LrBinding.makePropertyTable (context)
			-- Default values for boxes
			properties.scanDir = "yesScanDir"
			-- Present dialogbox 
			local result = LrDialogs.presentModalDialog {
...
f:popup_menu {
items =  cameraContent ,						
value = bind 'myCamera' , 
},		

The popup menu is still filled with each full line from the textfile.

The use of load loadstring  does not help and gives an error - bad argument #1 to '?' (string expected, got table)

This topic has been closed for replies.
Correct answer johnrellis

The "items" must be a Lua array, where each entry is a table with keys "title" and "value". Instead, the code above is passing in an array of strings (lines).


The idiom for reading a Lua expression from a file is to a) use dofile() or b) to read the file using LrFilteUtils.readFile(), compiling the resulting string with loadstring(), which returns a function, and then invoking the function.  With either approach, the contents of the file should be one or more statements that, when evaluated, should return the desired data structure.  E.g.

return {
    {title = "Nikon F80s", value = "NF80"},
    {title = "Nikon ZF II", value = "NZFII"}}

 

In LR 3 and 4 on Windows, dofile() had a bug where it wouldn't accept file paths containing non-ASCII characters. I don't know if that bug still exists -- if it does, you'll have to do LrFileUtils.readFile(), loadstring(), and then invoke the returned function.

 

1 reply

johnrellis
johnrellisCorrect answer
Legend
January 31, 2024

The "items" must be a Lua array, where each entry is a table with keys "title" and "value". Instead, the code above is passing in an array of strings (lines).


The idiom for reading a Lua expression from a file is to a) use dofile() or b) to read the file using LrFilteUtils.readFile(), compiling the resulting string with loadstring(), which returns a function, and then invoking the function.  With either approach, the contents of the file should be one or more statements that, when evaluated, should return the desired data structure.  E.g.

return {
    {title = "Nikon F80s", value = "NF80"},
    {title = "Nikon ZF II", value = "NZFII"}}

 

In LR 3 and 4 on Windows, dofile() had a bug where it wouldn't accept file paths containing non-ASCII characters. I don't know if that bug still exists -- if it does, you'll have to do LrFileUtils.readFile(), loadstring(), and then invoke the returned function.

 

Inspiring
January 31, 2024

Thank you John,

Going to try to get it to work.

I'm a analog photographer who just wants to automate his workflow, and just concentrate on taking images. My old app for recording my camera settings that I have been using for many years does not work anymore. I have switch to C+F wonderfull software but I need to start from scratch again.

Not easy when you don't have an developpers background.

Inspiring
February 1, 2024

Solved it:

ReturnContentFile = LrFileUtils.readFile(file) 
strTable = "{ " ..  ReturnContentFile .. " }"  	
strTable = loadstring("return "..strTable)()
return strTable

It took me a while before I found that I needed  an extra  { } surrounding my file contents