SketchUcation Plugin Store

 

 

How to Write variable value into test.txt file

How to Write variable value into test.txt file

Postby isoz » Thu May 31, 2012 8:20 pm

Hello guys my name is Gustavo and iam from Brazil, i would like to know how to how to Write variable value into (Ex:test.txt) file.

Iam using this code inside WebConsole " File::open('c:\foo.txt', 'wb') {|f| f.write $variablename } " it Works!!! create a file and writes variable value but when i try this same code inside Script area give me this msg error
String doesn't compile: Error in Script:
File::open('foo.txt', 'wb') {|f| f.write 'ruby!' }


String doesn't compile: Error in Script:
File::open('c:\foo.txt', 'wb') {|f| f.write 'ruby!' }


private method `open' called for MSketchyPhysics3::SP3xCommonContext::File:Class#<RuntimeError: Error in Script:
File::open('c:\foo.txt', 'wb') {|f| f.write 'ruby!' }


private method `open' called for MSketchyPhysics3::SP3xCommonContext::File:Class>
C:/PROGRA~2/Google/GOOGLE~1/Plugins/SketchyPhysics3/ControllerCommands.rb:1314:in `initEvents'
C:/PROGRA~2/Google/GOOGLE~1/Plugins/SketchyPhysics3/ControllerCommands.rb:1342:in `initialize'
C:/PROGRA~2/Google/GOOGLE~1/Plugins/SketchyPhysics3/ControllerCommands.rb:729:in `new'
C:/PROGRA~2/Google/GOOGLE~1/Plugins/SketchyPhysics3/ControllerCommands.rb:729:in `createBody'
C:/PROGRA~2/Google/GOOGLE~1/Plugins/SketchyPhysics3/sketchyphysicstool.rb:387:in `createBodyFromCollision'
C:/PROGRA~2/Google/GOOGLE~1/Plugins/SketchyPhysics3/sketchyphysicstool.rb:254:in `dumpCollision'
C:/PROGRA~2/Google/GOOGLE~1/Plugins/SketchyPhysics3/sketchyphysicstool.rb:238:in `each'
C:/PROGRA~2/Google/GOOGLE~1/Plugins/SketchyPhysics3/sketchyphysicstool.rb:238:in `dumpCollision'
C:/PROGRA~2/Google/GOOGLE~1/Plugins/SketchyPhysics3/sketchyphysicstool.rb:763:in `initialize'
C:/PROGRA~2/Google/GOOGLE~1/Plugins/SketchyPhysics3/sketchyphysicstool.rb:1623:in `new'
C:/PROGRA~2/Google/GOOGLE~1/Plugins/SketchyPhysics3/sketchyphysicstool.rb:1623:in `startphysics'
C:/PROGRA~2/Google/GOOGLE~1/Plugins/SketchyPhysics3/sketchyphysicstool.rb:1650:in `physicsTogglePlay'
C:/PROGRA~2/Google/GOOGLE~1/Plugins/SketchyPhysics3/sketchyphysicstool.rb:1738
(eval):3:in `call'
(eval):3
isoz
 
Posts: 6
Joined: Thu May 31, 2012 8:03 pm
Name: Gustavo

Re: How to Write variable value into test.txt file

Postby sdmitch » Fri Jun 01, 2012 5:18 pm

Ruby uses the '\' to quote the next character so in the script you either need to double the character '\\' or use '/' instead.

'wb' ??? Sketchup uses Ruby 8.? version and this may not be supported. I don't find this context in my Ruby reference book.
Nothing is worthless, it can always be used as a bad example.

http://sdmitch.blogspot.com/
User avatar
sdmitch
 
Posts: 617
Joined: Wed Mar 02, 2011 9:21 pm
Name: Sam
Operating system: Windows
SketchUp version: 8
License type: Free
SketchUp use: hobby
Level of SketchUp: Beginner

Re: How to Write variable value into test.txt file

Postby isoz » Sat Jun 02, 2012 1:09 am

sdmitch wrote:Ruby uses the '\' to quote the next character so in the script you either need to double the character '\\' or use '/' instead.

'wb' ??? Sketchup uses Ruby 8.? version and this may not be supported. I don't find this context in my Ruby reference book.

Hello Sdmitch! tnx!

I tried what you told, but same error :( any ideia?
File::open('c:/foo.txt', 'w+') {|f| f.write $variablename }

Works fine on console but not into script box.
isoz
 
Posts: 6
Joined: Thu May 31, 2012 8:03 pm
Name: Gustavo

Re: How to Write variable value into test.txt file

Postby sdmitch » Sat Jun 02, 2012 3:03 am

It could be something as simple as trying to write a file to the root directory. Windows Vista says that I don't have access even from the admistrator account.

Try this
Code: Select all
   File::open(File.join(File.dirname(__FILE__),'foo.txt'), 'wb') {|f| f.write $variablename }
which will create the file in the script directory.
Nothing is worthless, it can always be used as a bad example.

http://sdmitch.blogspot.com/
User avatar
sdmitch
 
Posts: 617
Joined: Wed Mar 02, 2011 9:21 pm
Name: Sam
Operating system: Windows
SketchUp version: 8
License type: Free
SketchUp use: hobby
Level of SketchUp: Beginner

Re: How to Write variable value into test.txt file

Postby isoz » Sat Jun 02, 2012 1:43 pm

sdmitch wrote:It could be something as simple as trying to write a file to the root directory. Windows Vista says that I don't have access even from the admistrator account.

Try this
Code: Select all
   File::open(File.join(File.dirname(__FILE__),'foo.txt'), 'wb') {|f| f.write $variablename }
which will create the file in the script directory.



Tnx iam going to test this when i get home, but i think the problem is this (private method `open' called for....
the Class File.open is private needs to be public i think.... where can i change this? where is this class?
isoz
 
Posts: 6
Joined: Thu May 31, 2012 8:03 pm
Name: Gustavo

Re: How to Write variable value into test.txt file

Postby TIG » Sat Jun 02, 2012 3:32 pm

Try
File.open(.....)
works for me... :?
TIG
User avatar
TIG
Global Moderator
 
Posts: 13925
Joined: Mon Nov 12, 2007 7:24 pm
Location: Northumbria UK
Name: TIG
Operating system: Windows
SketchUp version: 8
License type: Pro
SketchUp use: architecture
Level of SketchUp: Advanced

Re: How to Write variable value into test.txt file

Postby sdmitch » Sat Jun 02, 2012 3:33 pm

I have never had a problem with File.open just the path string for the file regarding the use of "\". The code I encluded in my last post was tested and worked in Ruby Web Console, Ruby Console, and a plugin.
Nothing is worthless, it can always be used as a bad example.

http://sdmitch.blogspot.com/
User avatar
sdmitch
 
Posts: 617
Joined: Wed Mar 02, 2011 9:21 pm
Name: Sam
Operating system: Windows
SketchUp version: 8
License type: Free
SketchUp use: hobby
Level of SketchUp: Beginner

Re: How to Write variable value into test.txt file

Postby isoz » Sat Jun 02, 2012 4:37 pm

I got it!!!! file.open works fine in webconsole, but inside script box dont! becouse open method is private.. so this is the code
File.send(:open,'c:\foo.txt', 'wb') {|f| f.write $f }

send "force" access to the method open. i think script box try to access File class and has no access.
Sorry for my english i hope you guys understand and tnx for the help.
isoz
 
Posts: 6
Joined: Thu May 31, 2012 8:03 pm
Name: Gustavo

SketchUcation One-Liner Adverts

by Ad Machine » 5 minutes ago

Artisan Organic Toolset - a set of powerful organic modeling tools.

Premium Members get 20% discount!

Ad Machine
Robot
 
Posts: 2012


Return to Developers' Forum

Who is online

Users browsing this forum: cescmari, eseraphim, michael310, pleonasm16, wel5323 and 4 guests