Applescript get list of running apps?

前端 未结 8 1132
梦谈多话
梦谈多话 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:23
    tell application "System Events"
        set processList to get the name of every process whose background only is false
        set processNameList to choose from list processList with prompt "Select process to quit" with multiple selections allowed
        if the result is not false then
            repeat with processName in processNameList
                do shell script "Killall " & quoted form of processName
            end repeat
        end if
    end tell
    

    enter image description here

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题