[code] Sketchup Safe Shutdown method
-
- EDIT: Since SketchUp 2014 the added method shown in this topic thread is no longer needed.
- Since that release, the API defines a
Sketchup.quit
module method that shuts down SketchUp safely.
Introduction
After reviewing the postings listed in the section Historic Reference (see below), it's time to post a simple, cross-platform safe shutdown Ruby method. (ie: The equivalent of the user choosing File > Exit or clicking the Titlebar X button.)Release - v1.0.0 - 27 DEC 2010
- Tested on PC v7.x and 8.x* The Mac Cocoa 'terminate:' id should work for all OSX vers.
# # app_safe_shutdown.rb # # defines Sketchup;;app_safe_shutdown method # # by Dan Rathbun - Palm Bay, FL, USA # # Public Domain # # version; 1.0.0 - 27 DEC 2010 # module Sketchup # check first if Google has added this method unless method_defined?( ;app_safe_shutdown ) if RUBY_PLATFORM.downcase.include?('mswin') # add Windows method def app_safe_shutdown send_action( 57665 ) # v7 and v8 end # def module_function(;app_safe_shutdown) elsif RUBY_PLATFORM.downcase.include?('darwin') # add Mac OSX method def app_safe_shutdown # per Greg Ewing send_action('terminate;') end # def module_function(;app_safe_shutdown) else # Unsupported platform > silently do nothing! end # if else # already defined unless $VERBOSE.nil? $stderr.write('Notice; #<!; method Sketchup;;app_safe_shutdown was already defined. ') $stderr.write("Script: '#{File.basename(__FILE__)}' did NOT redefine the method!>\n") end end # unless end # module
Historic Reference
Posts on this issue here at SCF:-
Exit/Shutdown SU 7 ??
Posts on this issue at GoogleGroups: -
Is it possible to initiate the termination of SketchUp from the Ruby script?
- Mac OSX
@unknownuser said:
%(#000000)[**terminate:**]
Terminates the receiver.%(#008B8B)[(void)terminate:(id)sender]
Parameters
sender
Typically, this parameter contains the object that initiated the termination request.Discussion
This method is typically invoked when the user chooses Quit or Exit from the application’s menu.When invoked, this method performs several steps to process the termination request. First, it asks the application’s document controller (if one exists) to save any unsaved changes in its documents. During this process, the document controller can cancel termination in response to input from the user.
.... ( for more, see link ) -
Seems to work fine for me, Dan.
I tested new, saved, and changed models under WinXP.
Is it possible to automatically restart SU somehow, maybe after a short delay?
-
PC
I did verify with VisualStudio that all PC versions above 6 have the ID 57665 for File > Exit.
(I did download ver 6, just to check it, but haven't had a chance to install it yet.)Mac
The ':terminate' string is a Mac Cocoa automation command, so it should also work for all Mac versions running OSX. -
A Ruby note: I'm UNsure how well this conditional works:
unless ( defined? app_safe_shutdown )=='method'
It seemed to, later playing around, it seemed NOT to work.
I may need to change that to just:
unless method_defined?( :app_safe_shutdown )
-
@jim said:
Is it possible to automatically restart SU somehow, maybe after a short delay?
Hmm... With a batch cmd file.. sure; but it would have had to start Sketchup in the first place.
Having the app itself restart itself... I'll need to think about that.. I'm sure I could whip out a VBS script that could restart SU.
-
Please don't spend time on this (unless you can't help it!) It's not terribly important and my fingers are not broken.
-
I will say that you should consider creating a Ruby method that makes a Win32API call to CreateProcess so that the Restart utilty can begin without being 'held up' by Sketchup's Ruby process.
If you attempt to use Ruby's
system()
,IO.popen
or shell%x(*command string*)
(ie, theKernel
'dot' backquote method,) .. Ruby cannot continue until the call returns, because they create subprocesses.Otherwise you'd be forced to use a nested external script. Ruby calls the a 'parent' script, which starts the restart script as a new process, so "it" (the parent,) can finish, and return to the Ruby
system()
call. -
@dan rathbun said:
If you attempt to use Ruby's
system()
,IO.popen
or shell%x(*command string*)
(ie, theKernel
'dot' backquote method,) .. Ruby cannot continue until the call returns, because they create subprocesses.For at least IO.popen, ruby will continue until you start reading on the pipe, although I have a feeling I took your statement out of context.
-
Yea, kinda. Trying to shut Ruby and Sketchup down (cleanly,) and don't really want a pipe.
-
censored
Advertisement