14

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

任何和所有的帮助将不胜感激!

谢谢!

4

8 回答 8

20

尝试这个:

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
于 2013-08-22T05:51:24.313 回答
6
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

在此处输入图像描述

于 2013-08-22T07:17:25.687 回答
5

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

tell application "Finder"
    get the name of every process whose visible is true
end tell
于 2015-07-29T15:04:46.847 回答
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
于 2013-08-22T06:02:02.797 回答
1

& (name of every process whose (name is "AppName")可以添加到Michele PercichParag Bafna 的解决方案中,以按名称包含特定的菜单栏应用程序。

tell application processName to quit可以用来代替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
于 2015-05-09T18:05:09.470 回答
1

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

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

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

在此处输入图像描述

于 2020-11-07T19:00:59.187 回答
0

如果你想从终端获得它,你可以使用一个像这样的简单脚本quit.rb

于 2020-11-07T14:51:00.103 回答
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

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

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

要将其与呈现的第一个代码一起使用,我通常在代码底部添加处理程序,然后要使用它,请在and语句之间添加以下示例AppleScript代码 tell application "Finder" to ¬set quitAppList to ¬

set appList to SortList(appList)

注意:多年前,我在Internet上的某个地方获得了这个特殊的处理程序,以至于记不清了,不幸的是,我失去了它的功劳。无论你是谁,我都向你道歉。

于 2020-11-07T16:11:09.840 回答