sketchucation logo sketchucation
    • Login
    Oops, your profile's looking a bit empty! To help us tailor your experience, please fill in key details like your SketchUp version, skill level, operating system, and more. Update and save your info on your profile page today!
    πŸ«› Lightbeans Update | Metallic and Roughness auto-applied in SketchUp 2025+ Download

    Sketchup.write_default needs escaping?

    Scheduled Pinned Locked Moved Developers' Forum
    7 Posts 3 Posters 388 Views 3 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • thomthomT Offline
      thomthom
      last edited by

      Here's an extract from my code:

      
      filename = Sketchup.read_default(S_SECTION, 'datafile')
      puts "\nread_default; #{filename}"
      if filename.nil? || !File.exist?(filename)
        filename = UI.openpanel('Choose NCS data file')
        puts "UI.openpanel; #{filename}"
        return nil if filename.nil?
        Sketchup.write_default(S_SECTION, 'datafile', filename)
      end
      
      

      The code tries to get the filename from stored settings.
      If no settings is found or the file does not exist it will prompt the user for a file.

      On the first run, when it prompts for the file - everything works fine.
      But the next time it fail.

      read_default: C:Users hm.ARCDesktopNCS-RGB-HTML.txt UI.openpanel: C:\Users\thm.ARC\Desktop\NCS-RGB-HTML.txt

      The string I passed to write_default is not the same as the one I get when I read it.
      Looks like it evaluates the string and treats the backslashes as special characters.

      I can't find that behaviour described in the docs. Have I missed something, or is it a bug?

      Thomas Thomassen β€” SketchUp Monkey & Coding addict
      List of my plugins and link to the CookieWare fund

      1 Reply Last reply Reply Quote 0
      • TIGT Offline
        TIG Moderator
        last edited by

        I think it's 'buggered'.
        I've noticed it messes with backslashes. πŸ˜’
        You could try doing a 'translation' of the \ to something else as you write and read...
        say writing as...
        filename.gsub!(/\\/,':')
        on reading it make all of the : into \ then make the first \ back to : to preserve the drive letter...
        filename.gsub!(/:/,'\\').gsub!(/\\\\/,':\\')
        ❓
        PS: I chose : as it seems not to be messed with AND as it isn't allowed in file names etc it won't clash; and the double :: let's you easily revert the drive as C:\ etc...

        TIG

        1 Reply Last reply Reply Quote 0
        • thomthomT Offline
          thomthom
          last edited by

          To work around this I just added filename = File.expand_path(filename) which converted \ to /.
          But I just wanted to check if it really was a bug in the API.

          
          filename = Sketchup.read_default(S_SECTION, 'datafile')
          puts "\nread_default; #{filename}"
          if filename.nil? || !File.exist?(filename)
            filename = UI.openpanel('Choose NCS data file')
            puts "UI.openpanel; #{filename}"
            return nil if filename.nil?
            filename = File.expand_path(filename)
            Sketchup.write_default(S_SECTION, 'datafile', filename)
          end
          
          

          Thomas Thomassen β€” SketchUp Monkey & Coding addict
          List of my plugins and link to the CookieWare fund

          1 Reply Last reply Reply Quote 0
          • thomthomT Offline
            thomthom
            last edited by

            And it is the read_default method that is bugged - because the registry setting is correct.

            Thomas Thomassen β€” SketchUp Monkey & Coding addict
            List of my plugins and link to the CookieWare fund

            1 Reply Last reply Reply Quote 0
            • thomthomT Offline
              thomthom
              last edited by

              Is this a new bug?

              Thomas Thomassen β€” SketchUp Monkey & Coding addict
              List of my plugins and link to the CookieWare fund

              1 Reply Last reply Reply Quote 0
              • TIGT Offline
                TIG Moderator
                last edited by

                Don't expect so...

                TIG

                1 Reply Last reply Reply Quote 0
                • M Offline
                  MartinRinehart
                  last edited by

                  @thomthom said:

                  Looks like it evaluates the string and treats the backslashes as special characters.

                  I had no end of troubles with slashes. Finally implemented this none-too-clever code and all the bugs went away:

                  =begin
                  qq() and qq!() do these replacements;
                      "'" -> "qq1"
                      '"' -> "qq2"
                      '/' -> "qq3"
                      '\' -> "qq4"
                      '\n' -> "qq5"
                      '\r' -> "qq6"
                  =end
                  
                  def self.qq( str )
                      return str.gsub( 
                          /\'/, 'qq1' ).gsub( 
                          /\"/, 'qq2' ).gsub( 
                          /\//, 'qq3' ).gsub( 
                          /\\/, 'qq4' ).gsub(
                          /\n/, 'qq5' ).gsub(
                          /\r/, 'qq6' )
                  end # of qq()
                  
                  def self.qq!( str )
                      str.gsub!( /\'/, 'qq1' )    
                      str.gsub!( /\"/, 'qq2' )    
                      str.gsub!( /\//, 'qq3' )    
                      str.gsub!( /\\/, 'qq4' )
                      str.gsub!( /\n/, 'qq5' )
                      str.gsub!( /\r/, 'qq6' )
                  end # of qq!()
                  
                  

                  These reversers complete the Ruby:

                  
                  def self.unqq( str ) # reverses qq()
                      return str.gsub( 
                          /qq1/, '\'' ).gsub( 
                          /qq2/, '"' ).gsub( 
                          /qq3/, '/' ).gsub( 
                          /qq4/, '\\' ).gsub(
                          /qq5/, '\n' ).gsub(
                          /qq6/, '\r' )
                  end # of unqq()
                  
                  def self.unqq!( str ) # reverses qq!()
                      str.gsub!( /qq1/, '\'' )
                      str.gsub!( /qq2/, '"' )
                      str.gsub!( /qq3/, '/' )
                      str.gsub!( /qq4/, '\\' )
                      str.gsub!( /qq5/, '\n' )
                      str.gsub!( /qq6/, '\r' )
                  end # of unqq!()
                  
                  

                  These are the reversers in JavaScript:

                  
                      single_quote = String.fromCharCode( 39 );
                      double_quote = String.fromCharCode( 34 );
                      ford_slash = String.fromCharCode( 47 );
                      back_slash = String.fromCharCode( 92 );
                      
                  
                      function unqq( msg ) {
                          s = msg.replace( /qq1/g, single_quote );
                          s = s.replace( /qq2/g, double_quote );
                          s = s.replace( /qq3/g, ford_slash );
                          s = s.replace( /qq4/g, back_slash );
                          s = s.replace( /qq5/g, '\\n' );
                          s = s.replace( /qq6/g, '\\r' );
                          return s;
                      }
                  
                  

                  Author, Edges to Rubies - The Complete SketchUp Tutorial at http://www.MartinRinehart.com/models/tutorial.

                  1 Reply Last reply Reply Quote 0
                  • 1 / 1
                  • First post
                    Last post
                  Buy SketchPlus
                  Buy SUbD
                  Buy WrapR
                  Buy eBook
                  Buy Modelur
                  Buy Vertex Tools
                  Buy SketchCuisine
                  Buy FormFonts

                  Advertisement