9 Replies Latest reply: Dec 3, 2012 8:13 PM by madhusudhanjr
madhusudhanjr Level 1 Level 1 (0 points)

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)
  • 1. Re: How to close all the openend items on Mac programatically with java or Applescript or Selenium?
    Camelot Level 8 Level 8 (45,790 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.

  • 2. Re: How to close all the openend items on Mac programatically with java or Applescript or Selenium?
    madhusudhanjr Level 1 Level 1 (0 points)

    thanks for your suggestion.. is their any way to get the list of apps or windows opened ie active ones, so that we can quit the same??

  • 3. Re: How to close all the openend items on Mac programatically with java or Applescript or Selenium?
    twtwtw Level 5 Level 5 (4,690 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

  • 4. Re: How to close all the openend items on Mac programatically with java or Applescript or Selenium?
    madhusudhanjr Level 1 Level 1 (0 points)

    Hi.. thanks for the script.  i was able to get the process whose visible is true, but quiting is not happening...!!!

  • 5. Re: How to close all the openend items on Mac programatically with java or Applescript or Selenium?
    twtwtw Level 5 Level 5 (4,690 points)

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

  • 6. Re: How to close all the openend items on Mac programatically with java or Applescript or Selenium?
    madhusudhanjr Level 1 Level 1 (0 points)

    ya i used your updated script only.. quitting is not happening..

  • 7. Re: How to close all the openend items on Mac programatically with java or Applescript or Selenium?
    twtwtw Level 5 Level 5 (4,690 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.

  • 8. Re: How to close all the openend items on Mac programatically with java or Applescript or Selenium?
    madhusudhanjr Level 1 Level 1 (0 points)

    Hi,

    thanks for the solution, am trying to get the process of browsers alone and trying to quit the same.. I followed the below code, but unable to to get the browser process, pls suggest on the same

     

    tell application "System Events"
        tell application "Finder"
            set process_list to the name of every process whose name is "Browser" and visible is true
            
        end tell
    end tel
    

    l

  • 9. Re: How to close all the openend items on Mac programatically with java or Applescript or Selenium?
    madhusudhanjr Level 1 Level 1 (0 points)

    I also found the script below..

     

    set browserList to {"Safari", "Firefox"}
    
    tell application "System Events"
        set appList to name of every process whose visible is true
    end tell
    
    repeat with i from 1 to count of appList
        set thisApp to item i of appList
        if thisApp is in browserList then
            tell application thisApp to quit
        end if
    end repeat
    

    is their any way to get list of browsers alone on runtime and close or quit the same??