• Login
sketchucation logo sketchucation
  • Login
๐Ÿค‘ 30% Off | Artisan 2 on sale until April 30th Buy Now

How to Write variable value into test.txt file

Scheduled Pinned Locked Moved Developers' Forum
8 Posts 3 Posters 794 Views
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.
  • I Offline
    isoz
    last edited by 31 May 2012, 19:20

    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

    1 Reply Last reply Reply Quote 0
    • S Offline
      sdmitch
      last edited by 1 Jun 2012, 16:18

      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/

      1 Reply Last reply Reply Quote 0
      • I Offline
        isoz
        last edited by 2 Jun 2012, 00:09

        @sdmitch said:

        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.

        1 Reply Last reply Reply Quote 0
        • S Offline
          sdmitch
          last edited by 2 Jun 2012, 02:03

          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

          	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/

          1 Reply Last reply Reply Quote 0
          • I Offline
            isoz
            last edited by 2 Jun 2012, 12:43

            @sdmitch said:

            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

            	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?

            1 Reply Last reply Reply Quote 0
            • T Online
              TIG Moderator
              last edited by 2 Jun 2012, 14:32

              Try
              File.open(.....)
              works for me... ๐Ÿ˜•

              TIG

              1 Reply Last reply Reply Quote 0
              • S Offline
                sdmitch
                last edited by 2 Jun 2012, 14:33

                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/

                1 Reply Last reply Reply Quote 0
                • I Offline
                  isoz
                  last edited by 2 Jun 2012, 15:37

                  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.

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

                  Advertisement