Sketchup.write_default needs escaping?
-
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?
-
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... -
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
-
And it is the
read_default
method that is bugged - because the registry setting is correct. -
Is this a new bug?
-
Don't expect so...
-
@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; }
Advertisement