File Path with backslashes
-
Hello,
I'm new to this. I'm using a Windows system and am tweaking the nifty SVG plugin:
http://flightsofideas.net/?p=572It has a line in the code of:
@svgFilename = Sketchup.active_model.get_attribute "foi_svg_export", "svgFilename", "flightofideas.svg"
When one invokes the plugin, a pop-up box comes up with that path for the "Output File":
http://www.flickr.com/photos/27621953@N02/3768086037/When first invoked, that path is simply "flightofideas.svg" until you specify (or navigate) a path. Subsequent changes remember that path. I'd like to automate that a bit and default that field to the drawing's working path to get you started. So far, from this post, I am at:
@svgFilename = Sketchup.active_model.get_attribute "foi_svg_export", "svgFilename", File.dirname(mymodel.path)+mymodel.title+".svg"
But instead of "C:/users/user/desktop/Untitled.svg" I get "C:usersuserdesktopUntitled.svg" (note the lack of slashes)
Also, I have a feeling that I'll need to add a slash on my own after the path and before the model. But I can't seem to get that working either.
Any ideas/help?
Thanks!
-
Try
File.join(File.dirname(mymodel.path), mymodel.title+".svg").tr("\\","/")
OR even the clunkier
(File.dirname(mymodel.path)+"/"+mymodel.title+".svg").tr("\\","/")
It you don't tell your code what to do it won't do itBUT note how the path part will automatically contain separator slashes - which on a PC these are \ NOT /.
So it could be that the \ slashes in the path are getting things confused in the dialog code, so try the 'tr' conversion of \ to / - the / will work fine for both PC or MAC, and in js, webdialogs etc...
EDIT: Typo corrected to.tr("\\","/")
TIG. -
Maybe it would be logical to convert input file paths as early as possible to the Ruby notation, namely
mymodelpath = File.expand_path(mymodel.path) File.join(File.dirname(mymodelpath), mymodel.title+".svg")
This way you never have both delimiters at the same time, and by using Ruby's method%(#000000)[File.expand_path]
your script is truely platform-agnostic (which is better than hardcoding delimiters for 1, 2, etc. platforms). -
Hi,
I realize that I forgot to mention that I am defining mymodel as:
mymodel = Sketchup.active_model
TIG: thanks! I hear ya on the \ vs. / thing for windows. But / is what the SVG script shows on the screen when one chooses a path, even on this windows environment. I tried both:
File.join(File.dirname(mymodel.path), mymodel.title+".svg").tr("\"","/")
and:
(File.dirname(mymodel.path)+"/"+mymodel.title+".svg").tr("\"","/")
which both resulted in:
C:usersuserdesktop/Untitled.svgAerilius: that did the trick, thank you very much!
Your code results in:
C:/users/user/desktop/Untitled.svg(and it works with the SVG plugin!)
Final code that works for those googling down the road:
mymodel = Sketchup.active_model mymodelpath = File.expand_path(mymodel.path) @svgFilename = Sketchup.active_model.get_attribute "foi_svg_export", "svgFilename", File.join(File.dirname(mymodelpath), mymodel.title+".svg")
-
TIG's example:
.tr("\"","/")
means replace all double quotes with a slash.It probably should be:
.tr("\\","/")
-
@dan rathbun said:
TIG's example:
.tr("\"","/")
means replace all double quotes with a slash.
It probably should be:
**.tr("\\","/")**
Dan thanks for spotting my typo. I've corrected the original... By coincidence I was doing some changes between double and single quotes and it got stuck in my muscle memory !
Advertisement