Saving the last directory a Preset file has been exported to via stateData #432
-
Hi everybody, I use the following command to open the save file dialog: The juce class is "File : https://docs.juce.com/master/classFile.html Is there a way to store the path to a custom userdirectory with the stateData valueTree and saveState/loadState methods? Thanks a lot for your help ;) Damien |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 8 replies
-
Salut Damien! J'espère que tu vas bien! Doing this since long in all my panels ;-) Indeed, the File object is a gold mine... Somewhere in a method in your panel:
SaveStateData method:
ReadStateData method:
Piece of cake ;-) |
Beta Was this translation helpful? Give feedback.
-
Alright so I got it almost working on Mac. saveFileWindow() requires (string, File, string, bool) So we need the full path + newFileName + .ext On mac the path are separated with "/" but if I recall on PC the separator for folders is antislash We need to concatenate the full path like this : sUserLastFullPath = tostring(sUserLastPath).."/"..FileName And this is the result:
the first time the console posts : the second pass, since sUserLastPath is stored : And an extra .syx extension is added obviously thanks to the 3argument (&pattern) the stateData has to be stored as string with :
and is shown in the ctrlr.settings like this : Can you please let me know if this is working on PC or it requires a antislash ? I'm pretty sure there's another way to compose the full path+name in a easier way and avoid this PC/MAC separator problem. Thanks |
Beta Was this translation helpful? Give feedback.
-
Alright I have it and it should work for both PC and Mac.
the console prints : LUA>> FileNameFromDialog : SPX900 - 14 - PERCUSSION ER.syx This way we can pass both an automated name for the file and the user last directory. with File(sLastFileFullPathWoName..FileName) If someone could check if it's working for PC that would be great. |
Beta Was this translation helpful? Give feedback.
-
This really is an excellent solution and it works on Windows! The trouble, as you already found out, is that So, as far as I know that userData cannot be output to string and therefore cannot be saved to I wrote a panel using Atom's code from the DEMO panel and also referring to this post, which is similar to your code and of course using your code. I would like to change some of my panels to incorporate this in future. Excellent work! remember last directory_1_0_0.zip -- Called when a modulator value changes -- @mod http://ctrlr.org/api/class_ctrlr_modulator.html -- @value new numeric value of the modulator -- https://ctrlr.org/forums/search/getParentDirectory/ saveContentAsText = function(mod, value) if panel:getBootstrapState() then return end if sLastFileFullPathWoName ~= nil then local sUserLastFullPathName = sLastFileFullPathWoName fileToWrite = utils.saveFileWindow("Save content as " .. ext, File(sUserLastFullPathName), "*" .. ext, true) else fileToWrite = utils.saveFileWindow( "Save content as " .. ext, File.getSpecialLocation(File.userHomeDirectory), "*." .. ext, true ) end if fileToWrite:isValid() == false then return end -- Let's see if the file exists if fileToWrite:existsAsFile() == false then -- The file is not there, that's ok, let's try to create it if fileToWrite:create() == false then -- Ooooh we can't create it, we need to fail here utils.warnWindow("File write", "The destination file does not exist, and I can't create it") return end end -- Update the file path panel:getLabelComponent("lastFileWritePathL"):setText(" \187 " .. fileToWrite:getFullPathName()) textToWrite = panel:getLabelComponent("saveMe"):getText() if textToWrite:length() == 0 then utils.warnWindow("Data to write", 'There is no data to write, we\'ll default to "' .. defText .. '"') textToWrite = defText end -- If we reached this point, we have a valid file we can try to write to -- There are two flags when writing text -- asUnicode - if should write our text as UNICODE -- writeUnicodeHeaderBytes - if we should add a special unicode header at the beginning of the file -- we set both to false if fileToWrite:replaceWithText(textToWrite, false, false) == false then utils.warnWindow("File write", "Failed to write data to file: " .. fileToWrite:getFullPathName()) end local sLastFileFullPath = fileToWrite:getFullPathName() -- get the absolute path+filename from dialog box local sLastFileName = fileToWrite:getFileName() -- get the name from dialog local sLastFileNameLen = string.len(sLastFileName) -- count the number of characters in the name from dialog sLastFileFullPathWoName = string.sub(sLastFileFullPath, 0, -tonumber(sLastFileNameLen) - 1) end saveState = function(--[[ ValueTree --]]stateData) if sLastFileFullPathWoName ~= nil then stateData:setProperty("UserLastPath", sLastFileFullPathWoName, nil) end end loadState = function(--[[ ValueTree --]] stateData) if stateData:hasProperty("UserLastPath") and string.len(stateData:getProperty("UserLastPath")) > 0 then sLastFileFullPathWoName=stateData:getProperty("UserLastPath") end end constructor = function() defText="no text submitted" ext="txt" end |
Beta Was this translation helpful? Give feedback.
Alright I have it and it should work for both PC and Mac.
SInce getParentDirectory() returns an objectID, I prefered using the getFullPathName function, then I remove the last x caracters from the returned string according to the length of the name of the file. This way I have the absolute path until the last folder separator slash for mac or antislash for PC :