Skip navigation

How to close all the openend items on Mac programatically with java or Applescript or Selenium?

768 Views 9 Replies Latest reply: Dec 3, 2012 8:13 PM by madhusudhanjr RSS
madhusudhanjr Level 1 Level 1 (0 points)
Currently Being Moderated
Nov 15, 2012 1:43 AM

I am installing an programatically app on Mac which asks me to close the open items like browsers, system preference window etc. before installing.. is their anyway to close all the open browsers and windows programmatically in Java or Applescript or Selenium??

Mac OS X (10.7.4)
  • Camelot Level 8 Level 8 (45,670 points)

    There's no one single answer to this. There are too many variables.

     

    If you know the list of applications you want to close it's relatively easy, but if you have an arbitrary, unspecified list then it gets a little harder.

     

    Part of the problem is that there are few standards for document handling across applications. Some applications have no documents at all (e.g. System Preferences), and you can just tell the app to close/quit and move on. Other applications have documents/windows that can be closed with impunity (e.g. web browsers), while others still have specific documents that might need to be saved (e.g. word processing apps, spreadsheets, etc.).


    Writing one unified statement for all use cases is not easy.

     

    You may also need to distinguish between needing to close a window and quitting an application.

    Many modern apps will gracefully tolerate a quit, saving their state and automatically resuming/reopening previous documents when they're relaunched. This means you could just quit the app, then relaunch it after the install is complete and not have to worry about its documents. Again, knowing whether you're dealing with a finite list of apps would go a long way towards answering that.

  • twtwtw Level 5 Level 5 (4,580 points)

    tell application "System Events"

              set appList to every process whose visible is true

    end tell

     

    the 'visible is true' part is necessary, otherwise it will report system and background apps that you probably don't want to quit.  If you want to quit the apps as well, the following will get the job done most of the time:

     

    tell application "System Events"

              set appList to every process whose visible is true

              repeat with thisApp in appList

                        try

      -- attempt to quit saving open files

                                  tell thisApp to quit saving yes

                        on error

                                  try

      -- app may not be document based, so attempt a simple quit

                                            tell thisApp to quit

                                  on error

      -- weird app that doesn't seem to respond properly to apple events

                                            do shell script "killall " & name of thisApp

                                  end try

                        end try

              end repeat

    end tell

     

    Message was edited by: twtwtw: removed spurious 'application' keywords

  • twtwtw Level 5 Level 5 (4,580 points)

    Did you notice that I revised the script on the forum?  I'd originally added some spurious 'application' keywords.

  • twtwtw Level 5 Level 5 (4,580 points)

    hmph.  try this instead.

     

    tell application "System Events"

              set appBundleIDList to bundle identifier of every process whose visible is true and name is not "Finder" and name is not "AppleScript Editor"

     

              repeat with thisBID in appBundleIDList

                        my quitter(thisBID)

              end repeat

    end tell

     

    on quitter(BID)

              tell application id BID to quit saving no

    end quitter

     

     

    The Finder is excluded because I doubt you want to quit it.  The script editor is excluded for testing purposes - it won't quit when a script is running.  remove that bit when you're done testing.

Actions

More Like This

  • Retrieving data ...

Bookmarked By (0)

Legend

  • This solved my question - 10 points
  • This helped me - 5 points
This site contains user submitted content, comments and opinions and is for informational purposes only. Apple disclaims any and all liability for the acts, omissions and conduct of any third parties in connection with or related to your use of the site. All postings and use of the content on this site are subject to the Apple Support Communities Terms of Use.