Help needed with replaceWithData

Home Forums General Programming Help needed with replaceWithData

Viewing 12 posts - 1 through 12 (of 12 total)
  • Author
    Posts
  • #7523
    DMM
    Participant
      • Topics: 6
      • Replies: 46
      • Total: 52

      I’m stuck with replaceWithData.

      My code is:

       

      savePatchToDisk = function(modulator, newValue)

       

      if modulator:isRestoring() then return end

      if utils == nil then return end

       

       

      file_name = “”

      file_name = getCurrentPatchNameFromPanel()

       

      data = getDataForSaving()

      hex_string_data = convertToHexString(data)

      mb = CtrlrLuaMemoryBlock()

      mb:loadFromHexString(hex_string_data)

      console(mb:toHexString(1))

       

      my_file = utils.saveFileWindow (“save JX8P sysex file”, File(file_name) , “*.syx”, true)

      if my_file ~= nil then –and my_file:existsAsFile() then

      my_file:replaceWithData(mb)

      end

      modulator:setValue(0,false)

      end

       

      Can someone rewrite this so it works?

      #7531
      Netwave
      Participant
        • Topics: 0
        • Replies: 1
        • Total: 1

        hello

        i unfortunatly i have the same problem , can t find a saving sysex methode that works.

        #7532
        atom
        Keymaster
          • Topics: 159
          • Replies: 2945
          • Total: 3104
          • ★★★★★

          can you be more specific, what’s not working. what error do you get ?

          #7558
          DMM
          Participant
            • Topics: 6
            • Replies: 46
            • Total: 52

            I’ve solved the problem,

            Had to change mb = CtrlrLuaMemoryBlock() to mb = MemoryBlock()

             

            Now I have a problem with the file naming.

            When I take the property of a label as file name it also copy spaces like (BASS          .syx) and

            Ctrlr seems to forget the last used directory.

            How can I remove the spaces in LUA and when saving the last used directory is chosen?

            —script—

            savePatchToDisk = function(modulator, newValue)

             

            if newValue == 1 then

            b = panel:getModulatorByName(“PATCH NAME”)

            c = b:getComponent()

            file_name = c:getProperty(“uiLabelText”)

            f = utils.saveFileWindow (“Save JX8P Patch”, File(“”), “*.syx”, true)

            f:create()

            if f:existsAsFile() then

             

            data = getDataForSaving()

            hex_string_data = convertToHexString(data)

            mb = MemoryBlock()

            mb:loadFromHexString(hex_string_data)

            –console(mb:toHexString(1))

            f:replaceWithData(mb)

             

            end

            modulator:setValue(0,false)

            end

            end

            #7559
            atom
            Keymaster
              • Topics: 159
              • Replies: 2945
              • Total: 3104
              • ★★★★★

              First of all why remove the spaces, they are valid filename characters. Second i think that the file class has a special method for that:

              static String 	createLegalFileName (const String &fileNameToFix)
              

              I see i bound it incorrectly, but it’s there, i’ll post a fix today evening so that it works and i’ll post an example on how to use that, though it should be as simple as

              file_name = File.createLegalFileName (c:getProperty(“uiLabelText”))
              
              #7560
              DMM
              Participant
                • Topics: 6
                • Replies: 46
                • Total: 52

                Thank you Atom,

                I’ve seen that one but didn’t get it working.

                Do you also know why the save window doesn’t remember the last used directory when I use the

                label text property as file name?

                 

                This works normal:

                f = utils.saveFileWindow (“Save JX8P Patch”, File(“”), “*.syx”, true)

                This doesn’t:
                b = panel:getModulatorByName(“PATCH NAME”)
                c = b:getComponent()
                file_name = c:getProperty(“uiLabelText”)
                f = utils.saveFileWindow (“Save JX8P Patch”, File(file_name), “*.syx”, true)

                #7561
                atom
                Keymaster
                  • Topics: 159
                  • Replies: 2945
                  • Total: 3104
                  • ★★★★★

                  If you pass a file_name as the second paraamter, it’s just the file name, it contains no directory information, you’d need to pass the full absolute path in order for the dialog to show that directory initially.

                  #7562
                  DMM
                  Participant
                    • Topics: 6
                    • Replies: 46
                    • Total: 52

                    Can I get the last used directory with some code instead of a absolute path?

                    This would make it more easy for the user.

                    #7563
                    atom
                    Keymaster
                      • Topics: 159
                      • Replies: 2945
                      • Total: 3104
                      • ★★★★★

                      You’d need to save it yourself in a variable, that’s the simplest way, and use that. Create a function wrapper for the save dialog window that stores the last browsed path in a variable (remember all variables in Lua are global) and uses that whenever it’s called.

                      #7564
                      DMM
                      Participant
                        • Topics: 6
                        • Replies: 46
                        • Total: 52

                        Haha I really don’t understand what you are writing 😉

                        #7565
                        atom
                        Keymaster
                          • Topics: 159
                          • Replies: 2945
                          • Total: 3104
                          • ★★★★★

                          You’re lucky i’m bored at work for now, here is a function that will ask for a file to save, remember the last directory the file was saved in, it returns the file chosen by the user in the dialog.

                          function saveFile(suggestedFileName)
                          	-- Your method code here
                          	if lastBrowsedDir ~= nil then
                          		result = utils.saveFileWindow (suggestedFileName, lastBrowsedDir, "*.syx", true)
                          	else
                                          -- since the is no last save directory, let's default to the user's home directory
                          		result = utils.saveFileWindow (suggestedFileName, File.getSpecialLocation(File.userHomeDirectory), "*.syx", true)
                          	end
                                  -- save the last directory the user save the file into for later
                          	lastBrowsedDir = result:getParentDirectory()
                          	return (result)
                          end
                          
                          #7597
                          atom
                          Keymaster
                            • Topics: 159
                            • Replies: 2945
                            • Total: 3104
                            • ★★★★★

                            you can now use the method mentioned, so you can create file names without spaces and only with allowed characters like this:

                            illegalFileName = "This is a illegal file name. Or is it?.txt"
                            legalFileName = String(File.createLegalFileName(illegalFileName)):replace(" ", "_", false)
                            

                            if you paste that in the Lua console you’ll get

                            >>> illegalFileName = "This is a illegal file name. Or is it?.txt"
                            legalFileName = String(File.createLegalFileName(illegalFileName)):replace(" ", "_", false)
                            console (legalFileName)
                            This_is_a_illegal_file_name._Or_is_it.txt
                            
                          Viewing 12 posts - 1 through 12 (of 12 total)
                          • The forum ‘Programming’ is closed to new topics and replies.
                          There is currently 0 users and 91 guests online
                          No users are currently active
                          Forum Statistics
                          Threads: 2,495, Posts: 17,374, Members: 77,605
                          Most users ever online was 12 on January 22, 2019 3:47 pm
                          Ctrlr