rubymacosapisketchup

Google SketchUp close file


The Ruby API to Google SketchUp has a function, open_file, but I can't find a close_file function. Since I have to batch process many files, I want to close each file before moving on to the next, otherwise the program will crash from memory exhaustion.

What is the best way to close SketchUp files programmatically?

I am using Mac OS X and am willing to use AppleScript functions to signal the window to close.

EDIT

I am considering a few approaches that have proven fruitless so far.

  1. Using the appscript Ruby gem, as described in this question. The problem here is that I cannot get SketchUp to recognize my installed gems.
  2. In a similar vein, I am trying to use osascript (a bash program that executes AppleScripts from the shell) to close the window. That is, I call out to the shell from SketchUp's Ruby console window using one of the following:

    %x[osascript -e 'tell application "SketchUp" to close window 1']

    %x[osascript -e 'tell application "SketchUp" to close window 1' &]

    %x[osascript -e 'tell application "SketchUp" to close every window']

    %x[osascript -e 'tell application "SketchUp" to close every window' &]

    Whenever I try this second approach, SketchUp just freezes. However, when I execute any of these commands from an IRB or directly from the Bash prompt outside of SketchUp, I get the desired behavior: the model window closes (incidentally, the Ruby console window remains open, which is fine).

  3. Have a master script that launches a slave script to process each model. The slave will run within the Google SketchUp program while the master waits. When the slave is finished, it signals the master, and the master closes the SketchUp file. To do this interprocess communication, I tried using drb. However, when I try to require drb within SketchUp, I get the following message:

Error: LoadError: (eval):5:in 'require': no such file to load -- drb

EDIT 2

Having a separate process continuously running that closes Google Sketchup windows using AppleScript when signaled is clumsy for a number of reasons. First, it's ugly to have to have a separate process devoted to closing Sketchup windows. Second, the only effective way of communicating with the external script is through the creation of files, which is wasteful and the disk access may be slowing things down.

However, the most severe issue is that Sketchup is slow at responding to AppleScript commands. I have a pretty computation intensive script running in Sketchup, and it seems to starve the AppleScript response, which means that the osascript times out before the windows close. Sketchup only gets around to responding to AppleScript when there is a dialogue box prompt in Sketchup that pauses the execution of my computationally intensive script.

EDIT 3

I have modified my close_file function to pause execution of the script by displaying a dialog box. This essentially yields the current thread and allows the thread that responds to AppleScript commands to execute:

def close_file()
  f = '/temp/mutex.txt' # for finer control, use different mutex for each window you want closed
  File.new(f, 'w').close
  result = UI.messagebox "Click OK when window has closed."
end

Then the separate ruby script that closes windows via AppleScript will additionally have to click "OK" in the dialog box. AppleScript to do that is:

tell application "System Events"
    tell process "SketchUp"
        set frontmost to true
        keystroke return
    end tell
end tell

This modification is an improvement. It corrects the "most severe issue" mentioned in EDIT 2, but the other issues remain.


Solution

  • One solution is to have a separate process continuously running that will close Google Sketchup windows using AppleScript when signaled. The approach described here uses files to do the interprocess communication (drb did not seem to work).

    First, you have to make sure Sketchup supports AppleScripting. To do this, execute the following at the command prompt (with appropriate substitutions for different Sketchup versions, etc.):

    $ defaults write /Applications/Google\ SketchUp\ 8/SketchUp.app/Contents/Info NSAppleScriptEnabled -bool YES
    

    Now, create a separate file that will close Sketchup windows. Here is my closer.rb file:

    #!/usr/bin/ruby
    
    ## closer.rb ##
    
    def wait(f)
      while !File::exists?(f)
      end 
      File.delete(f)
    end
    
    def signal(f)
      File.new(f, 'w').close
    end
    
    while true
      wait('~/temp/mutex.txt')
      msg = %x[osascript -e 'tell application "SketchUp" to close every window'] 
      signal('~/temp/conf.txt')
    end
    

    Run this script from the shell. It waits until the file ~/temp/mutex.txt is created. Once the file is created, it runs osascript (essentially AppleScript) to close all the windows in Sketchup, and then signals that the windows were closed by creating a ~/temp/conf.txt file.

    Here is the client code (which can be placed in your Sketchup plugins) that signals the closer script:

    def wait(f)
      while !File::exists?(f)
      end 
      File.delete(f)
    end
    
    def signal(f)
      File.new(f, 'w').close
    end
    
    def close_file
      signal('~/temp/mutex.txt')
      wait('~/temp/conf.txt')
    end
    

    The close_file signals the closer script and then waits for a confirmation that the file was closed before returning. Now you can close files in Sketchup.