How to convert String to Path?
-
Hy, I'm trying to "learn" by doing something and I need some help.
I got the current model's path like this:model = Sketchup.active_model pth = model.path
and
pth
is returned as string, so I cannot do this:pth = model.path.dirname
My question: how I can convert the returned string to path, so I can get model's directory?
Or is any easier way to get model's directory?
Thanks.
-
Use the 'File' methods...
` pth=model.path >> "C:/Temp/MySkip.skp"if the file has never been saved it '' ! so trap for that??
folder=File.dirname(pth) >> "C:/Temp"
file_name=File.basename(pth) >> "MySkip.skp"
file=File.basename(pth, ".*") >> "MySkip"
ext=File.extname(pth).downcase >> ".skp"OR
ext=File.extname(pth).upcase >> ".SKP"
file_path=File.join(folder, file+".txt") >> "C:/Temp/MySkip.txt"
f=File.open(file_path, "w")opens/makes the file to 'w'rite to...
or 'r'ead, 'a'ppend, 'b'inary etc
f.puts("Hello word!")
f.close
important to close any open file as it's 'locked' by SUp/Ruby otherwise...`
-
pth = File.dirname( model.path )
-
@thomthom said:
pth = File.dirname( model.path )
On new unsaved model,
model.path
returns an empty string.On an empty string,
File.dirname("")
returns"."
, which usually means the current working directory (but is false really, as there is no actual path set for the model.)
Dir.getwd()
will return the path for the current working directory (which is likely to be the user's HOME directory, when Sketchup first starts, if no other script has changed it.)
The HOME dir on Mac:ENV['HOME']
The HOME dir on Win:ENV['USERPROFILE']
So you test
pth
this way:pth =( model.path.empty? ? nil ; File.dirname( model.path ) ) if pth # not nil, a valid path # do something here with the model path else # model was not saved # perhaps save the model here # then do something else end
-
As I explained in my earlier post
pth=model.path
will be''
[an empty string] if the SKP's never been saved NOTnil
, so a test likeif pth
will always returntrue
because an empty string is not equivalent tofalse
ornil
! so you need aif pth != ''
test etc... -
@tig said:
... so a test like
if pth
will always returntrue
NOT if you use the conditional statement on line 1 of my code example, which will set
pth
tonil
if themodel.path.empty?
test istrue
, otherwise the valid path will be referenced bypth
.There are many ways to do the same thing, here's another:
if model.path.empty? # model was not saved # perhaps save the model here; pth = "some/new/path/filename.skp" valid = model.save(pth) else # it's a valid path pth = model.path valid = true end if valid # do some more stuff else puts("Model save was unsucessful to path; #{pth}.") end
-
So didn't spot your initial .empty? test.
Of course you could have avoided some more extra steps with a streamlinedif pth=model.path.empty? ### do stuff with an alternative path else ### do stuff with 'pth' as it's not empty end
-
Isn't Ruby great! So many ways to do the same thing... we can all be happy and flexible.
phrase for today: COBOL SUCKS!
-
@tig said:
So didn't spot your initial .empty? test.
Of course you could have avoided some more extra steps with a streamlinedif pth=model.path.empty? > ### do stuff with an alternative path > else > ### do stuff with 'pth' as it's not empty > end
### do stuff with 'pth' as it's not empty - incorrect.
pth
will have the result of.empty?
- true/false. -
Thomthom you are right!
Here is a fixed versionif (pth=model.path).empty? ### do stuff with an alternative path else ### do stuff with 'pth' as it's not empty end
Advertisement