sketchucation logo sketchucation
    • Login
    Oops, your profile's looking a bit empty! To help us tailor your experience, please fill in key details like your SketchUp version, skill level, operating system, and more. Update and save your info on your profile page today!
    πŸ«› Lightbeans Update | Metallic and Roughness auto-applied in SketchUp 2025+ Download

    Problem when running Ruby Script from command line

    Scheduled Pinned Locked Moved Developers' Forum
    16 Posts 3 Posters 3.7k 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.
    • TIGT Offline
      TIG Moderator
      last edited by

      A MAC 'command' file is much like a .cmd or .bat file on PC.

      You obviously need to get the syntax right but most certainly you will be able to open a file with its default application, manipulate file names etc...

      What you can't do [readily] is to run a Ruby script inside the then opened SKP - but you can make a script that will load and run at startup anyway so why bother ?
      The 'trick of renaming it to .rb so it auto-loads and then renaming .txt so it doesn't is all you need in your 'command' file, in addition to the code to open the SKP itself...

      You can easily write a test command script to rename a file.
      You can easily write a test script to open a SKP.

      Once you know your syntax works combine them into a few lines and you have it...
      Just rework the Ruby as I suggested so that it runs immediately after it loads...
      http://homepage.mac.com/rgriff/files/TerminalBasics.pdf
      http://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man1/open.1.html

      TIG

      1 Reply Last reply Reply Quote 0
      • Y Offline
        Ying
        last edited by

        Thank you guys for your helping. My problem is solve.
        I wrote a c++ program to run the command line ".../sketchup -RubyStartup myrubyscript.rb" using system() method. However, the Sketchup UI window is still covered by Mac OS Terminal and myrubyscript.rb can't be executed. (I probably have to run my c++ program through the Terminal.) Then, the solution is that I fork a new process and let this process to run a program call cliclick to mimic the mouse action to click the Sketchup UI window in order to bring the window to the top of the desktop. Eventually, the myrubyscript.rb gets executed.

        1 Reply Last reply Reply Quote 0
        • Dan RathbunD Offline
          Dan Rathbun
          last edited by

          What about:

          command & /path/to/Sketchup/sketchup -rubyStartup myfile.rb

          Isn't the & supposed to replace the current process ??

          I'm not here much anymore.

          1 Reply Last reply Reply Quote 0
          • TIGT Offline
            TIG Moderator
            last edited by

            I still don't see why you needed to digress off to complicated C solutions ?
            You could have a simple MAC xxx.command file that runs when you double-click on it - it renames a non-loading script called ../Plugins/xxx.txt to xxx.rb, then it opens Sketchup [and closes itself]***. The xxx.rb script will then auto-load as the SKP opens and it executes. ***To ensure that when the xxx.rb has done its stuff it reverts to its 'safe' name of xxx.txt, the change need not be done in the command-file - as the last thing that the xxx.rb file does we simply use a Ruby method File.rename(__File__, 'xxx.txt').

            TIG

            1 Reply Last reply Reply Quote 0
            • Dan RathbunD Offline
              Dan Rathbun
              last edited by

              Wasn't there a video that Jim pointed out on YouTube, of Scott running Jim's unfold plugin from the terminal (or some shell,) on a Mac ??

              I'm not here much anymore.

              1 Reply Last reply Reply Quote 0
              • Y Offline
                Ying
                last edited by

                @dan rathbun said:

                What about:

                command & /path/to/Sketchup/sketchup -rubyStartup myfile.rb

                Isn't the & supposed to replace the current process ??

                I did exactly the same but unfortunately it still didn't do what I want. Maybe the attached screenshot can explain my trouble better.

                My ruby script file is quite simple:

                open the sketchup file

                show_summary = true
                Sketchup.open_file ".../test.skp"

                export the model

                model = Sketchup.active_model
                status = model.export '.../test.dae', show_summary

                close the Sketchup window

                Sketchup.send_action('terminate:')

                From the screenshot you can see that the Sketchup window is under the Terminal window when I run "command & /path/to/Sketchup/sketchup -rubyStartup myrubyscript.rb" command. Although the Sketchup was launched, the ruby script file was not be executed unless I click on the Sketchup window. Haven't you ever met this problem before?


                sketchup.png

                1 Reply Last reply Reply Quote 0
                • TIGT Offline
                  TIG Moderator
                  last edited by

                  You are trying to fix a problem that doesn't have to exist...
                  A simple command-file to rename the .txt 'ruby' file as .rb AND then open the SKP [you should be able to code the command-file to accept dropped-files as its argument[s], so you simply drop the SKPs you want to 'export' onto its icon].
                  Before the 'ruby' file says 'terminate' it has an extra line to simply rename itself as .txt.
                  OR make a .rb script that 'exports' all of the SKP files in a particular selected folder. It opens them in turn and exports them, returning to the original SKP ??

                  TIG

                  1 Reply Last reply Reply Quote 0
                  • Y Offline
                    Ying
                    last edited by

                    @tig said:

                    You are trying to fix a problem that doesn't have to exist...
                    A simple command-file to rename the .txt 'ruby' file as .rb AND then open the SKP [you should be able to code the command-file to accept dropped-files as its argument[s], so you simply drop the SKPs you want to 'export' onto its icon].
                    Before the 'ruby' file says 'terminate' it has an extra line to simply rename itself as .txt.
                    OR make a .rb script that 'exports' all of the SKP files in a particular selected folder. It opens them in turn and exports them, returning to the original SKP ??

                    Please excuse me that I still don't get it. Why do I need to name the script file as .txt. Why can't I just use .rb? I am quite new for Sketchup and Ruby API. Maybe I just made a stupid mistake but I didn't know. I am pretty sure that my Ruby scripts are correct. It is just that the Sketchup doesn't get the window focus to run it.

                    1 Reply Last reply Reply Quote 0
                    • TIGT Offline
                      TIG Moderator
                      last edited by

                      A .rb file that is in the Plugins folder 'auto-loads' when Sketchup opens, so if you set its code to 'self.execute' [as you have here since the code is 'loose' and not within a module/class/method], it means you'd export every SKP every time you opened any SKP = that's NOT what you normally want!
                      However, a file in the Plugins folder containing Ruby code BUT with a name that is suffixed .txt will not auto-load.
                      You must either 'load' it within some other Ruby code OR as I thought I had explained... you write a MAC command-file that renames that 'ruby' file [.txt >> .rb] and then opens the SKP you want to export. The [now] .rb file will auto-load and it will export the SKP file, then close it. The temporarily renamed .rb file should have a line of code near its end to rename itself as .txt, and then close the SKP.
                      Thus it never auto-loads unless you want it to, by renaming it briefly with your command-file that will then open the SKP etc etc...
                      Simple πŸ€“

                      PS: Your current ruby code opens the same SKP all of the time.
                      You don't need that if your command-file opens the SKP.
                      Your ruby code needs to do the export the SKP, rename itself and close the SKP...

                      ### get the the model info
                      model=Sketchup.active_model
                      path=File.dirname(model.path)
                      folder=File.join(path,'Exported_DAEs')
                      if not File.exist?(folder)
                        Dir.mkdir(folder)
                      end
                      name=model.title+'.dae'
                      expo=File.join(folder,name)
                      ### export it
                      model.export(expo, true)
                      ### you can also assemble a hash to set all
                      ### of the .dae export options
                      ### see the API guide...
                      ### set second argument to
                      ### 'false' to skip the 'report dialog' each export.
                      ### 'Ruby' file renames itself...
                      File.rename(__FILE__, 'xxx.txt')
                      ### where xxx.txt is the script's 'safe' name
                      ### while xxx.rb is its auto-load name.
                      ### finally close the Sketchup window
                      Sketchup.send_action('terminate;')
                      

                      Here I have added some stuff to auto-name files etc... πŸ˜‰

                      TIG

                      1 Reply Last reply Reply Quote 0
                      • Dan RathbunD Offline
                        Dan Rathbun
                        last edited by

                        But I think his intention, is to do batch mode export to dae for a whole dir of skp files.

                        So manual drag-n-drop is out of picture.

                        He can use a shell script, no doubt,...

                        ... but he could also do batch export, from within SU Ruby.

                        I'm not here much anymore.

                        1 Reply Last reply Reply Quote 0
                        • TIGT Offline
                          TIG Moderator
                          last edited by

                          @dan rathbun said:

                          But I think his intention, is to do batch mode export to dae for a whole dir of skp files.

                          So manual drag-n-drop is out of picture.

                          He can use a shell script, no doubt,...

                          ... but he could also do batch export, from within SU Ruby.

                          You can drop multiple files onto a MAC command file or an 'applet', they are processed in turn, so the code could in turn make txt>>rb, open each SKP dropped; the rb file doing the SKP export, renaming itself txt and closing the SKP for each one.
                          A much simpler way to export a folder of SKPs as .dae is to have a permanently loaded Ruby tool - I quickly wrote this as an example... http://forums.sketchucation.com/viewtopic.php?p=347277#p347277

                          TIG

                          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