Applescript get list of running apps?

前端 未结 8 1180
梦谈多话
梦谈多话 2020-12-30 05:47

Applescript newbie question again :) I am trying to create a small applescript that will allow me to select multiple items from a list of currently running applications and

8条回答
  •  独厮守ぢ
    2020-12-30 06:25

    The following example AppleScript code is pretty straight forward and will gracefully quit the selected application(s), providing the selected application(s) is (are) in a stable state:

    tell application "System Events" to ¬
        set appList to the name of ¬
            every process whose visible is true
    
    set quitAppList to ¬
        choose from list appList ¬
            with multiple selections allowed
    
    repeat with thisApp in quitAppList
        quit application thisApp
    end repeat
    

    When I present a list to choose from, I prefer to have it in alphabetical order and to that end I use a handler to first sort the list before presenting it:

    on SortList(thisList)
        set indexList to {}
        set sortedList to {}
        set theCount to (count thisList)
        repeat theCount times
            set lowItem to ""
            repeat with i from 1 to theCount
                if i is not in the indexList then
                    set thisItem to item i of thisList as text
                    if lowItem is "" then
                        set lowItem to thisItem
                        set lowItemIndex to i
                    else if thisItem comes before lowItem then
                        set lowItem to thisItem
                        set lowItemIndex to i
                    end if
                end if
            end repeat
            set end of sortedList to lowItem
            set end of indexList to lowItemIndex
        end repeat
        return the sortedList
    end SortList
    

    To use this with the first block of code presented I typically add handlers at the bottom of my code and then to use it, add the following example AppleScript code between the tell application "Finder" to ¬ and set quitAppList to ¬ statements:

    set appList to SortList(appList)
    

    Note: I acquired this particular handler somewhere on the Internet too many years ago to remember and unfortunately lost who to credit it to. My apologies to whomever you are.

提交回复
热议问题