sketchucation logo sketchucation
    • Login
    ℹ️ Licensed Extensions | FredoBatch, ElevationProfile, FredoSketch, LayOps, MatSim and Pic2Shape will require license from Sept 1st More Info

    How to Write variable value into test.txt file

    Scheduled Pinned Locked Moved Developers' Forum
    8 Posts 3 Posters 794 Views 3 Watching
    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

      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
      • sdmitchS Offline
        sdmitch
        last edited by

        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

          @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
          • sdmitchS Offline
            sdmitch
            last edited by

            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

              @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
              • TIGT Offline
                TIG Moderator
                last edited by

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

                TIG

                1 Reply Last reply Reply Quote 0
                • sdmitchS Offline
                  sdmitch
                  last edited by

                  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

                    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
                    • First post
                      Last post
                    Buy SketchPlus
                    Buy SUbD
                    Buy WrapR
                    Buy eBook
                    Buy Modelur
                    Buy Vertex Tools
                    Buy SketchCuisine
                    Buy FormFonts

                    Advertisement