Google SketchUp close file

2.5k views Asked by At

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.

5

There are 5 answers

0
dsg On BEST ANSWER

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.

1
driven On

You can do a lot with SketchUp using applescript and/or Automator.

tell application "SketchUp"
activate
tell application "System Events"
    delay 1.0 --close window, adjust delay to suit
    --key code 13 using command down -- Press ⌘W or you can use
    keystroke "w" using command down
end tell

end tell

I have a number of status bar items for open new, paste to console, closing, killing SU etc.

They're tiny files that trigger the SU shortcut keys, and can be used externally, from 'Ruby Console' or as part of a plugin.

I'm not sure what kind of 'batch convert' your trying to achieve, but I used Automator to convert most of my old v5, V6, v7 files to v8 today... 100's of them, and your probably aware, on opening you get 'alert' warning that saving will convert the file to the latest version (which is what I wanted).

It's tricky to click this "OK" programatically.

Automator on it's own is quite restricted, but you can add an applescript to the workflow and I've found that if you use it's 'record function' to demonstrate what you need to do.

You can then copy/paste that code into 'Script Editor', get rid of the 'dowithTimeout' bits, copy it back into the workflow with delays either side and let it run.

I had to play around with delays, to accommodate some larger files, but achieved >95% success. [Success being able to select all, right click, Create Icon and Preview.]

Unfortunately I then binned the workflow, but could recreate it, if you want to have a look.

2
driven On

success, in Ruby Console system("osascript -e 'tell application \"suoff\"' -e 'activate' -e 'end tell'") and this script inside an Automator.app in applications folder....

    on run
    -- Make sure SU is foremost, don't click on anything else either
    tell application "System Events"
    tell process "SketchUp"
        set frontmost to true
        -- gives a message if you try to select when SU's not running
        delay 0.2
        tell application "SketchUp" to quit saving no
        -- no dialog box etc...
        delay 0.1

    end tell
    end tell
    end run

the problem is SU has an anti self destruct ruby, that mines any linked files and aborts all efforts to shut it down... from inside itself.

so you need to bury it...

this combination works on 10.5.8 with SU8.1.

if you have a look at SketchUcation [Developer] Forum, I left a call for mac/applescript testers... I've rewritten the app since then but if you PM me I'll send you an open copy of all the bits I've got working..

john

0
Alessandro On

I had to convert hundreds of .skp files with a batch processing script. I faced similar issue, since every time I opened a new file it remained opened. For me it was enough to close the active_model and the file goes with it.

This was true for SketchUp 2019 Pro Mac OS X version.

model = Sketchup.active_model
model.save(filename, Sketchup::Model::VERSION_2015)
model.close

Hope it will help

0
Kevin On

There is an important limitation with using an external AppleScript to force SketchUp to do something. SketchUp will not accept any user input while a menu script is running.

I found this out trying to setup an automated rendering solution. I added a menu item which opens a WebDialog to get a list of models to download from a server. It then steps through the list and uses cURL to download each model. Once downloaded it loads the model, renders it out, uploads the images using cURL again, and goes to the next model.

What I wanted was to activate the AppleScript after each model is rendered (using the "mutex" file solution shown above) and have it close the model window. Unfortunately, since the menu script is still running, SketchUp will not respond to anything the AppleScript tells it to do. Only once all of the models have been processed and the menu script exits, will the AppleScript finally run.

This would not be so bad if all of the calls to the AppleScript were queued up, but they aren't. It seems that only the last two get honored (that number may be just an accident dependent on the timing of when my calls to the AppleScript were made relative to when the menu script finished).

Because of this limitation, I was not able to use the mutex wait() function in my menu Ruby code. Since the AppleScript was not able to execute, it never created its "I'm done" mutex file, and therefore the wait() function in Ruby never got the signal that it could continue. The result was a deadlock between AppleScript and SketchUp, such that SketchUp just freezes up (well, actually it is just stuck in a really tight loop in the wait() function).

So, when approaching this problem, think of AppleScript as a cleanup tool you can call when all of your processing is done.