by Dan Rathbun » Tue Jun 22, 2010 9:33 pm
* * 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. * IntroductionAfter 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.
- Code: Select all
# # 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 ReferencePosts on this issue here at SCF: Posts on this issue at GoogleGroups:

Apple Developer Network in NSApplication Class Reference wrote:terminate:Terminates the receiver. (void)terminate:(id)sender ParameterssenderTypically, this parameter contains the object that initiated the termination request. DiscussionThis 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 )
Last edited by Dan Rathbun on Thu Aug 13, 2020 11:40 pm, edited 6 times in total.
I'm not here much anymore. But a PM will fire email notifications.
-

Dan Rathbun
- PluginStore Author

-
- Posts: 5051
- Joined: Tue Oct 06, 2009 3:06 am
- Location: Florida, USA
- Name: Dan Rathbun
- Operating system: Windows
- SketchUp version: 2015
- License type: Pro
- SketchUp use: education
- Level of SketchUp: Advanced
by Jim » Fri Aug 20, 2010 4:02 pm
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?
Hi
-
Jim
- Global Moderator
-
- Posts: 4678
- Joined: Mon Nov 12, 2007 10:13 pm
- Location: ohio
- Name: Jim
- Operating system: Windows
- SketchUp version: 2017
- License type: Pro
- SketchUp use: hobby
- Level of SketchUp: Intermediate
-
by Dan Rathbun » Fri Aug 20, 2010 5:14 pm
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.
I'm not here much anymore. But a PM will fire email notifications.
-

Dan Rathbun
- PluginStore Author

-
- Posts: 5051
- Joined: Tue Oct 06, 2009 3:06 am
- Location: Florida, USA
- Name: Dan Rathbun
- Operating system: Windows
- SketchUp version: 2015
- License type: Pro
- SketchUp use: education
- Level of SketchUp: Advanced
by Dan Rathbun » Fri Aug 20, 2010 5:17 pm
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 )
I'm not here much anymore. But a PM will fire email notifications.
-

Dan Rathbun
- PluginStore Author

-
- Posts: 5051
- Joined: Tue Oct 06, 2009 3:06 am
- Location: Florida, USA
- Name: Dan Rathbun
- Operating system: Windows
- SketchUp version: 2015
- License type: Pro
- SketchUp use: education
- Level of SketchUp: Advanced
by Dan Rathbun » Fri Aug 20, 2010 5:22 pm
Jim wrote: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.
I'm not here much anymore. But a PM will fire email notifications.
-

Dan Rathbun
- PluginStore Author

-
- Posts: 5051
- Joined: Tue Oct 06, 2009 3:06 am
- Location: Florida, USA
- Name: Dan Rathbun
- Operating system: Windows
- SketchUp version: 2015
- License type: Pro
- SketchUp use: education
- Level of SketchUp: Advanced
by Jim » Fri Aug 20, 2010 6:23 pm
Please don't spend time on this (unless you can't help it!) It's not terribly important and my fingers are not broken.
Hi
-
Jim
- Global Moderator
-
- Posts: 4678
- Joined: Mon Nov 12, 2007 10:13 pm
- Location: ohio
- Name: Jim
- Operating system: Windows
- SketchUp version: 2017
- License type: Pro
- SketchUp use: hobby
- Level of SketchUp: Intermediate
-
by Dan Rathbun » Sat Aug 21, 2010 8:38 pm
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, the Kernel '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.
I'm not here much anymore. But a PM will fire email notifications.
-

Dan Rathbun
- PluginStore Author

-
- Posts: 5051
- Joined: Tue Oct 06, 2009 3:06 am
- Location: Florida, USA
- Name: Dan Rathbun
- Operating system: Windows
- SketchUp version: 2015
- License type: Pro
- SketchUp use: education
- Level of SketchUp: Advanced
by cjthompson » Mon Aug 23, 2010 3:16 pm
Dan Rathbun wrote:If you attempt to use Ruby's system(), IO.popen or shell %x(command string) (ie, the Kernel '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.
-
cjthompson
-
- Posts: 152
- Joined: Mon Jul 13, 2009 9:13 pm
- Name: Chris Thomson
by Dan Rathbun » Mon Aug 23, 2010 5:47 pm
Yea, kinda. Trying to shut Ruby and Sketchup down (cleanly,) and don't really want a pipe.
I'm not here much anymore. But a PM will fire email notifications.
-

Dan Rathbun
- PluginStore Author

-
- Posts: 5051
- Joined: Tue Oct 06, 2009 3:06 am
- Location: Florida, USA
- Name: Dan Rathbun
- Operating system: Windows
- SketchUp version: 2015
- License type: Pro
- SketchUp use: education
- Level of SketchUp: Advanced
by driven » Sun Apr 14, 2013 12:35 am
censored
learn from the mistakes of others, you may not live long enough to make them all yourself...
-
driven
- PluginStore Author

-
- Posts: 3033
- Joined: Fri May 01, 2009 11:50 pm
- Name: driven
- Operating system: Mac
- SketchUp version: 2015
- License type: Pro
- SketchUp use: engineering and mechanical design
- Level of SketchUp: Intermediate
by Ad Machine » 5 minutes ago
-
Ad Machine
- Robot
-
- Posts: 2012
-
Return to Developers' Forum
|