sketchucation logo sketchucation
    • Login
    🤑 SketchPlus 1.3 | 44 Tools for $15 until June 20th Buy Now

    [code] Sketchup Safe Shutdown method

    Scheduled Pinned Locked Moved Developers' Forum
    10 Posts 4 Posters 5.3k Views 4 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Dan RathbunD Offline
      Dan Rathbun
      last edited by Dan Rathbun

      • 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&#058; '#{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?

      • Ruby Command to shutdown Sketchup

      • Closing Sketchup from a Ruby script


      http://devimages.apple.com/assets/elements/header/developer.png

      • Mac OSX

      @unknownuser said:

      (http://developer.apple.com/mac/library/documentation/Cocoa/Reference/ApplicationKit/Classes/NSApplication_Class/Reference/Reference.html#//apple_ref/occ/instm/NSApplication/terminate;)":35u8ppdq]

      %(#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 )

      I'm not here much anymore.

      1 Reply Last reply Reply Quote 0
      • J Offline
        Jim
        last edited by

        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

        1 Reply Last reply Reply Quote 0
        • Dan RathbunD Offline
          Dan Rathbun
          last edited by

          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.

          1 Reply Last reply Reply Quote 0
          • Dan RathbunD Offline
            Dan Rathbun
            last edited by

            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.

            1 Reply Last reply Reply Quote 0
            • Dan RathbunD Offline
              Dan Rathbun
              last edited by

              @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.

              I'm not here much anymore.

              1 Reply Last reply Reply Quote 0
              • J Offline
                Jim
                last edited by

                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

                1 Reply Last reply Reply Quote 0
                • Dan RathbunD Offline
                  Dan Rathbun
                  last edited by

                  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.

                  1 Reply Last reply Reply Quote 0
                  • C Offline
                    cjthompson
                    last edited by

                    @dan rathbun said:

                    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.

                    1 Reply Last reply Reply Quote 0
                    • Dan RathbunD Offline
                      Dan Rathbun
                      last edited by

                      Yea, kinda. Trying to shut Ruby and Sketchup down (cleanly,) and don't really want a pipe.

                      I'm not here much anymore.

                      1 Reply Last reply Reply Quote 0
                      • D Offline
                        driven
                        last edited by

                        censored

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

                        1 Reply Last reply Reply Quote 0
                        • 1 / 1
                        • First post
                          Last post
                        Buy SketchPlus
                        Buy SUbD
                        Buy WrapR
                        Buy eBook
                        Buy Modelur
                        Buy Vertex Tools
                        Buy SketchCuisine
                        Buy FormFonts

                        Advertisement