Applescript get list of running apps?

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

    0 讨论(0)
  • Try this:

    tell application "System Events"
        set listOfProcesses to (name of every process where background only is false)
        tell me to set selectedProcesses to choose from list listOfProcesses with multiple selections allowed
    end tell
    --The variable `selectedProcesses` will contain the list of selected items.
    repeat with processName in selectedProcesses
        do shell script "Killall " & quoted form of processName
    end repeat
    
    0 讨论(0)
  • 2020-12-30 06:14

    & (name of every process whose (name is "AppName") can be added to Michele Percich's and Parag Bafna's solutions to include specific menu bar applications by name.

    tell application processName to quit can be used instead of do shell script "Killall " & quoted form of processName.

    tell application "System Events"
        set processList to ¬
            (name of every process where background only is false) & ¬
            (name of every process whose ¬
                (name is "AppName") or ¬
                (name is "AnotherAppName"))
        tell me to set selectedProcesses to choose from list processList with prompt "Select process(es) to quit:" with multiple selections allowed
    end tell
    if the result is not false then
        repeat with processName in selectedProcesses
             tell application processName to quit
        end repeat
    end if
    
    0 讨论(0)
  • 2020-12-30 06:15

    you can use this script which is much simpler

    tell application "Finder"
        get the name of every process whose visible is true
    end tell
    
    0 讨论(0)
  • 2020-12-30 06:17

    If you want it from Terminal, you can use a simple script like this quit.rb

    0 讨论(0)
  • 2020-12-30 06:20

    You can try this

    tell application "System Events"
            set AppName to name of every process whose background only is false
            choose from list AppName OK button name "Ok" cancel button name "Cancel"
        end
    
    0 讨论(0)
提交回复
热议问题