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

Macos Applescript获取正在运行的应用程序列表?,macos,applescript,osx-mountain-lion,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 li

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
任何和所有的帮助将不胜感激

谢谢

试试这个:

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
你可以试试这个

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
&(每个进程的名称,其名称为“AppName”)
可以被添加到其中,并通过名称包含特定菜单栏应用程序的解决方案

告诉应用程序processName退出
可以代替
使用shell脚本“Killall”和引用的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

您可以使用更简单的脚本

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

如果您想从终端获得它,您可以使用这样一个简单的脚本下面的示例AppleScript代码非常简单,只要选定的应用程序处于稳定状态,它就会优雅地退出选定的应用程序:

将应用程序“系统事件”告知
将appList设置为——
每一个可见的过程都是真实的
将quitAppList设置为——
从应用列表中选择
允许多选
在quitAppList中使用此应用程序重复此操作
退出此应用程序
结束重复

当我呈现一个列表以供选择时,我更喜欢按字母顺序排列,为此,我使用处理程序在呈现列表之前首先对列表进行排序:

排序列表(此列表)上的

将indexList设置为{}
将sortedList设置为{}
将计数设置为(计数此列表)
重复计数次数
将lowItem设置为“”
重复从1到计数的i
如果我不在索引列表中,那么
将thisItem设置为thisList的项目i作为文本
如果lowItem为“”,则
将lowItem设置为thisItem
将lowItemIndex设置为i
否则,如果此项位于lowItem之前,则
将lowItem设置为thisItem
将lowItemIndex设置为i
如果结束
如果结束
结束重复
将sortedList的末尾设置为lowItem
将indexList的末尾设置为lowItemIndex
结束重复
返回分类列表
末日名单
为了在显示的第一块代码中使用它,我通常在代码底部添加处理程序,然后在
告诉应用程序“Finder”To
将quitaplist设置为
语句之间添加以下示例AppleScript代码:

将appList设置为SortList(appList)


注:我在多年前在《强互联网》中找到了这个特殊的处理者,很不幸地忘记了谁来信任它。我向你的任何人道歉。

< P>我在几年后写了下面的Apple Script代码。我认为这是一个“必须有的”,因为我几乎每天都使用它。 此代码将生成可见和隐藏应用程序进程的列表,允许选择多个项目来终止其进程。列表中的第一个项目将是可见应用程序进程(不按字母顺序排序),然后是空列表项(用于将可见进程与隐藏进程分开),其余列表项将是隐藏的应用程序进程(按字母顺序排序)


工作起来很有魅力!!!我的最后一个问题是,如果没有killall命令,你会怎么做?有点像,告诉应用程序selectedprocesss退出。谢谢!请参阅上面Parag脚本的底部。编辑添加Parag脚本部分。
killall
默认情况下会向相关进程发送一个信号15,告诉进程ess尽快退出。最好先发送信号HUP,即
killall-HUP
,这通常允许进程在关闭前进行一些清理。脚本的顶部对我不起作用,但底部对我起作用了!谢谢!
tell application "Finder"
    get the name of every process whose visible is true
end tell
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