Applescript获取正在运行的应用程序列表?

时间:2013-08-22 05:32:20

标签: macos applescript osx-mountain-lion

再次发现Applescript新手问题:)我正在尝试创建一个小AppleScript,允许我从当前运行的应用程序列表中选择多个项目,然后退出所选的应用程序。这样的东西可以工作,但不必点击每个对话框,从列表中选择会更容易。

tell application "System Events"
repeat with p in every process
    if background only of p is false then
        display dialog "Would you like to quit " & name of p & "?" as string
    end if
end repeat
end tell

非常感谢任何和所有帮助!

谢谢!

8 个答案:

答案 0 :(得分:12)

试试这个:

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

答案 1 :(得分:5)

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

答案 2 :(得分:3)

你可以使用这个更简单的脚本

tell application "Finder"
    get the name of every process whose visible is true
end tell

答案 3 :(得分:1)

你可以试试这个

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

答案 4 :(得分:1)

tell application processName to quit可以添加到Michele Percich'sParag Bafna's解决方案中,以便按名称包含特定的菜单栏应用程序。

可以使用

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

public class MediaPlayerSingleton extends MediaPlayer{
    private static MediaPlayerSingleton mediaPlayerSingleton;

    private MediaPlayerSingleton() {}

    public static MediaPlayerSingleton getInstance() {
        synchronized (mediaPlayerSingleton)  { // if you'll be using it in moe then one thread
            if(mediaPlayerSingleton == null)
                mediaPlayerSingleton = new MediaPlayerSingleton();
        }

        return mediaPlayerSingleton;
    }

}

答案 5 :(得分:0)

如果要在Terminal中使用它,可以使用一个简单的脚本,例如quit.rb

答案 6 :(得分:0)

以下示例 AppleScript 代码非常简单,将正常退出选定的应用程序 ,只要所选的应用程序处于稳定状态:

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

当我提供一个 list 供选择时,我更喜欢按字母顺序排列,为此,我首先使用 handler 对列表进行排序,然后再进行呈现:

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

要与呈现的 code 的第一个 block 一起使用,通常在 code 的底部添加 handlers >然后使用它,请在tell application "Finder" to ¬set quitAppList to ¬ 之间添加以下示例 AppleScript 代码声明

set appList to SortList(appList)

注意:太多年前,我在 Internet 上的某个地方获得了这个特殊的 handler ,以至于忘记并不幸失去了谁。我向您表示歉意。

答案 7 :(得分:0)

几年前,我是按照AppleScript代码编写的。我认为它是“必备”产品,因为我几乎每天都在使用它。

此代码将生成“可见”和“隐藏”应用程序进程的列表,从而允许选择多个项目以终止其进程。列表中的第一项将是可见的应用程序进程(不按字母顺序排序),然后是一个空的列表项(用于将可见的进程与隐藏的进程分开),其余的列表项将是隐藏的应用程序进程(按字母顺序排序)< / p>

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

enter image description here