[code] Sketchup Safe Shutdown method

[code] Sketchup Safe Shutdown method

Postby 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.
*


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.

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 Reference
Posts on this issue here at SCF:Posts on this issue at GoogleGroups:

  • Mac OSX
Apple Developer Network in NSApplication Class Reference wrote:
terminate:
Terminates the receiver.
    (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 )
0
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.
    User avatar
    Dan Rathbun 
    PluginStore Author
    PluginStore Author
     

    Re: [code] (beta) Sketchup Safe Shutdown

    Postby 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?
    0
    Hi

    Jim 
    Global Moderator
     

    Re: [code] (beta) Sketchup Safe Shutdown

    Postby 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.
    0
      I'm not here much anymore. But a PM will fire email notifications.
      User avatar
      Dan Rathbun 
      PluginStore Author
      PluginStore Author
       

      Re: [code] (beta) Sketchup Safe Shutdown

      Postby 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 )
      0
        I'm not here much anymore. But a PM will fire email notifications.
        User avatar
        Dan Rathbun 
        PluginStore Author
        PluginStore Author
         

        Re: [code] (beta) Sketchup Safe Shutdown

        Postby 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.
        0
          I'm not here much anymore. But a PM will fire email notifications.
          User avatar
          Dan Rathbun 
          PluginStore Author
          PluginStore Author
           

          Re: [code] (beta) Sketchup Safe Shutdown

          Postby 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.
          0
          Hi

          Jim 
          Global Moderator
           

          Re: [code] (beta) Sketchup Safe Shutdown

          Postby 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.
          0
            I'm not here much anymore. But a PM will fire email notifications.
            User avatar
            Dan Rathbun 
            PluginStore Author
            PluginStore Author
             

            Re: [code] (beta) Sketchup Safe Shutdown

            Postby 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.
            0

            cjthompson 
             

            Re: [code] (beta) Sketchup Safe Shutdown

            Postby 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.
            0
              I'm not here much anymore. But a PM will fire email notifications.
              User avatar
              Dan Rathbun 
              PluginStore Author
              PluginStore Author
               

              Re: [code] (beta) Sketchup Safe Shutdown

              Postby driven » Sun Apr 14, 2013 12:35 am

              censored
              0
              learn from the mistakes of others, you may not live long enough to make them all yourself...

              driven 
              PluginStore Author
              PluginStore Author
               

              SketchUcation One-Liner Adverts

              by Ad Machine » 5 minutes ago



              Ad Machine 
              Robot
               



               

              Return to Developers' Forum

              Who is online

              Users browsing this forum: No registered users and 13 guests