• Login
sketchucation logo sketchucation
  • Login
🤑 SketchPlus 1.3 | 44 Tools for $15 until June 20th Buy Now

Problem when running Ruby Script from command line

Scheduled Pinned Locked Moved Developers' Forum
16 Posts 3 Posters 3.5k 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.
  • Y Offline
    Ying
    last edited by 3 Aug 2011, 15:36

    This might be a simple question but I searched for the solution for a couple of days and got nothing.

    I want to run Skechup from command line using a Ruby Script to open a sketchup model and then export it as a dae model, on may MacBook Pro with Mac OS. However, everytime I run the command ".../sketchup -RubyStartup myrubyscript.rb" from the Terminal, it launches the sketchup but stops running the Ruby scripts. I think that it is because that the Terminal is the current active process and the Sketchup is not. (The Terminal window is on the top of the desktop.) As long as I manually click on the Sketchup UI to bring it to the the current active process (now the Sketchup UI window is on the top), the Ruby scripts will continue. I really don't want to involve human interaction in this whole process. I want them to be automatically. Can anyone tell me how can I solve this problem?

    Thank you very much!

    1 Reply Last reply Reply Quote 0
    • T Offline
      TIG Moderator
      last edited by 3 Aug 2011, 15:59

      Sketchup must run the script.

      Write your script in the ../Plugins/ folder called Ying-auto_export_dae.rb that will do the .dae export etc...
      Let us assume your module is called Ying and the method auto_export_dae...
      At the end of the script's code instead of using the command inside a menu coding etc simply add the command 'raw' - e.g.
      Ying.auto_export_dae().
      Now every time you open a SKP it will be auto-exported as a .dae file as the Ying-auto_export_dae.rb file auto-loads.
      You probably don't want this to happen!
      Rename the file as Ying-auto_export_dae.txt so it doesn't now auto-load.

      You then write your command file to do the following...
      Rename the file back as Ying-auto_export_dae.rb.
      Open the SKP file by path.
      As the SKP opens the .rb file will auto-load and export the SKP as a .dae. file.
      Close the SKP file [if possible?]
      Rename the file back as Ying-auto_export_dae.txt after a few seconds of it loading [sleep? or when the .dae file is made?].
      Once again it won't auto-load for normally opened SKP files...

      If you find that the export fails because it runs too soon after the SKP starts to open try using a timer to delay running the export code by a few seconds - thus:
      UI.start_timer{2,false){Ying.auto_export_dae()}
      here it waits 2 seconds before executing the export code once [ false means there will be no repeat] - note how you include the export command inside 'braces' {}... any code in the script or later loading scripts after this continues because only the command's commencement is delayed. Also naming the script zzzzYing-auto_export_dae.txt/.rb will means it loads near the end too...

      TIG

      1 Reply Last reply Reply Quote 0
      • D Offline
        Dan Rathbun
        last edited by 3 Aug 2011, 17:56

        TIG his problem is running from a shell script (batch mode.)

        It seem to be an OS blocking issue.

        Ying, what version OSX are you running ?

        Try using a symbolic link, or shell script instead of typing the command into Terminal.

        I'm not here much anymore.

        1 Reply Last reply Reply Quote 0
        • Y Offline
          Ying
          last edited by 3 Aug 2011, 18:15

          I am using Mac OS 10.5.8
          Actually I'd like to write a c++ program to run the command instead of directly running from the Terminal. Now I just want to test this command line beforehand to make sure it is a possible solution.

          @dan rathbun said:

          TIG his problem is running from a shell script (batch mode.)

          It seem to be an OS blocking issue.

          Ying, what version OSX are you running ?

          Try using a symbolic link, or shell script instead of typing the command into Terminal.

          1 Reply Last reply Reply Quote 0
          • D Offline
            Dan Rathbun
            last edited by 3 Aug 2011, 21:05

            @ying said:

            Now I just want to test this command line beforehand to make sure it is a possible solution.

            Well put the command into a one line shell script, and run the script.

            I'm not here much anymore.

            1 Reply Last reply Reply Quote 0
            • T Offline
              TIG Moderator
              last edited by 3 Aug 2011, 21:57

              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 5 Aug 2011, 13:41

                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
                • D Offline
                  Dan Rathbun
                  last edited by 8 Aug 2011, 09:53

                  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
                  • T Offline
                    TIG Moderator
                    last edited by 8 Aug 2011, 10:19

                    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
                    • D Offline
                      Dan Rathbun
                      last edited by 8 Aug 2011, 11:04

                      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 8 Aug 2011, 14:32

                        @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
                        • T Offline
                          TIG Moderator
                          last edited by 8 Aug 2011, 14:54

                          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 8 Aug 2011, 15:13

                            @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
                            • T Offline
                              TIG Moderator
                              last edited by 8 Aug 2011, 15:26

                              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
                              • D Offline
                                Dan Rathbun
                                last edited by 8 Aug 2011, 23:32

                                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
                                • T Offline
                                  TIG Moderator
                                  last edited by 9 Aug 2011, 10:07

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

                                  Advertisement