Applescript get list of running apps?

前端 未结 8 1139
梦谈多话
梦谈多话 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:07

    I wrote this following AppleScript code a few years back. I consider it to be a “Must Have” because I use it almost every single day.

    This code will generate a list of Visible and Hidden application processes, allowing multiple items to be selected to kill their processes. The first items in the list will be visible application processes (not sorted alphabetically), then an empty list item (used to separate the visible from the hidden processes), and the remaining list items will be the hidden application processes (sorted alphabetically)

    use framework "Foundation"
    use scripting additions
    
    property appsToKill : missing value
    property NSArray : a reference to current application's NSArray
    
    listAllAppProcesses()
    
    activate
    set killApp to (choose from list ¬
        appsToKill with title "Choose The App To Kill" with prompt ¬
        "Choose The App/Apps To Kill" & linefeed & linefeed ¬
        & "The Empty List Item Separates The Visible From The Hidden Applications" OK button name ¬
        "OK" cancel button name "CANCEL" with multiple selections allowed)
    
    set pidList to {}
    
    if killApp is not false then
        tell application "System Events"
            repeat with i from 1 to count of killApp
                set thisItem to item i of killApp
                tell application process thisItem
                    set thePID to unix id
                    set end of pidList to thePID
                end tell
            end repeat
        end tell
    else
        return
    end if
    
    set text item delimiters to space
    do shell script ({"kill -9", pidList} as text)
    
    on listAllAppProcesses()
        tell application "System Events"
            set visibleAppsToKill to name of every application process ¬
                where visible is true
            set invisibleAppsToKill to name of every application process ¬
                where visible is false
            set aList to ((NSArray's arrayWithArray:invisibleAppsToKill)'s ¬
                sortedArrayUsingSelector:"caseInsensitiveCompare:") as list
            set appsToKill to visibleAppsToKill & "" & aList
        end tell
    end listAllAppProcesses
    

提交回复
热议问题