Copy link to clipboard
Copied
Hi,
I couldn't find anything similar, so here is my question:
I am currently using 'LrPrefs'.prefsForPlugin() for storing Preferences directly in the LR Preference file:
local prefs = import 'LrPrefs'.prefsForPlugin()
local expresets = prefs['op_Presets']
I would like to switch to my own Preference file inside the plugin folder, as I save Lens Presets and everytime I move to a new Computer there is no easy way to export/import them from the Preference file. (No, I really dont want to move my Prefs file everytime)
can someone give me a hint on how I would do this?
cheers,
dirk
Copy link to clipboard
Copied
(Note that you can write the expression prefs['op_Presets'] more concisely as prefs.op_Presets.)
My plugins sometimes create preference files rather than use LrPrefs. They store data in the file as Lua-syntax expressions (analagous to JSON). Here are the functions I use (the Debug.pp function comes from my debugging toolkit: Debugging Toolkit for Lightroom SDK ).
local pcall = LrTasks.pcall
--[[----------------------------------------------------------------------------
public string
valueToString (value v)
Returns the value "v" as a string in Lua-expression form, suitable for
reading back in. Only data that can be printed with Debug.pp() can be
read back in.
------------------------------------------------------------------------------]]
function Util.valueToString (v)
return Debug.pp (v, 0, math.huge, math.huge)
end
--[[----------------------------------------------------------------------------
public value, errorMsg
stringToValue (string s)
Executes the string "s" (created by valueToString (v)) to return a value
equivalent to "v". If no error occurs, returns <value, nil>; otherwise
returns <nil, errorMsg>.
------------------------------------------------------------------------------]]
function Util.stringToValue (s)
local chunk, errorMsg = loadstring ("return " .. s)
if not chunk then return nil, errorMsg end
local success, v = pcall (chunk)
if not success then return nil, v end
return v, nil
end
--[[----------------------------------------------------------------------------
public value, errorMsg
writeValue (string path, value v)
Writes valueToString(v) to file "path". If no error occurs, returns
<value, nil>; otherwise returns <nil, error message>.
------------------------------------------------------------------------------]]
function Util.writeValue (path, v)
local file, errorMsg = io.open (path, "w")
if file == nil then return nil, errorMsg end
local success, errorMsg = file:write (Util.valueToString (v))
if not success then
file:close ()
return nil, errorMsg
end
local success, errorMsg = file:close ()
if not success then return nil, errorMsg end
return v, nil
end
--[[----------------------------------------------------------------------------
public value, errorMsg
readValue (string path)
Reads the value stored in file "path" (written by writeValue()). If no
error occurs, returns <value, nil>; otherwise returns <nil, error message>.
------------------------------------------------------------------------------]]
function Util.readValue (path)
local success, s = pcall (LrFileUtils.readFile, path )
if not success then return nil, s end
return Util.stringToValue (s)
end
--[[----------------------------------------------------------------------------
public string path, string errorMsg
writeFile (string path, string s)
Writes "s" to the file "path". If "path" is nil, a unique temp file is
created. If no error occurs, return <path to file, nil>; otherwise returns
<nil, error message>
------------------------------------------------------------------------------]]
function Util.writeFile (path, s)
if path == nil then
path = child (getStandardFilePath ("temp"), generateUUID ())
end
local file, errorMsg = io.open (path, "w")
if file == nil then return nil, errorMsg end
local success, errorMsg = file:write (s)
if not success then
file:close ()
return nil, errorMsg
end
local success, errorMsg = file:close ()
if not success then return nil, errorMsg end
return path, nil
end
Copy link to clipboard
Copied
Hi John,
thanks for your answer.
I think I need a bit more help on this. The code, you provided is for sure a good hint, but I am still stuck in implementing it.
I now have another idea on this and could use some hints.
Would it be possible to read a folder with textfiles and use the files (could be lua tables, just to make it simpler) and read those in the plugin? In this case it would be much simpler to share Lens Presets.
cheers,
dirk