将白名单添加到退出所有应用程序但列表不起作用

Adding a whitelist to a quit all app but list not working

所以我正在尝试退出所有应用程序,其中包含您不想退出的应用程序的白名单。虽然在您输入白名单项目后,我希望它说 "Are you sure you want to quit all applications except " 然后是白名单中的项目。但是白名单中的项目是连在一起的,我输入 finder 和 chrome,但它显示 "Are you sure you want to quit all applications except finderchrome?"。所以我在下面添加代码并在白名单中输入 finder 和 chrome。

set orginizedList to item 1 of white_list
---------------------------------------------------------------------------------------------------------------------------
repeat (length of white_list) times
    set i to 2
    set orginizedList to orginizedList & item i of white_list & ", "
    set i to i + 1
end repeat
---------------------------------------------------------------------------------------------------------------------------

现在是"Are you sure you want to quit all applications except finder, finder, finder,?"

代码如下:

    say "are you sure you want to quit all applications?"
set white_list to {""}
set doneWhitelist to ""

repeat until doneWhitelist = "Done"
    set whiteListedApps to display dialog "WhiteList" buttons {"Add More", "Done"} default answer "Finder"
    set whiteListedAppNames to text returned of whiteListedApps
    set doneWhitelist to button returned of whiteListedApps
    set white_list to white_list & whiteListedAppNames
end repeat

set orginizedList to item 1 of white_list
---------------------------------------------------------------------------------------------------------------------------
repeat (length of white_list) times
    set i to 2
    set orginizedList to orginizedList & item i of white_list & ", "
    set i to i + 1
end repeat
---------------------------------------------------------------------------------------------------------------------------
set confirmQuit to display alert "Are you sure you want to quit all applications except for " & orginizedList & "?" buttons {"Yes", "No"}
set confirmQuit to button returned of confirmQuit
if confirmQuit = "No" then
    error number -128
else
    tell application "System Events" to set the visible of every process to true

    try
        tell application "Finder"
            set process_list to the name of every application whose visible is true
        end tell
        repeat with i from 1 to (number of items in process_list)
            set this_process to item i of the process_list
            if this_process is not in white_list then
                tell application this_process
                    quit
                end tell
            end if
        end repeat
    on error
        tell the current application to display dialog "An error has occurred!" & return & "This script will now quit" buttons {"Quit"} default button 1 with icon 0
    end try
end if

请帮我解决问题!!!

白名单对话框有点混乱,不管怎么添加默认条目,所以我会使用更具描述性的提示,将默认留空,并在添加之前检查结果:

repeat -- forever
  set whiteListedApp to display dialog "Add to WhiteList:" buttons {"Add Item", "Done"} default answer ""
  set whiteListedAppName to text returned of whiteListedApp
  if button returned of whiteListedApps is "Done" then exit repeat
  if (whiteListedAppName is not "") and (whiteListedAppName is not in white_list) then set white_list to white_list & whiteListedAppName
end repeat

为了进一步清理最后的对话框,我还会将 Finder 添加到初始的 white_list(因为它总是在那里):

set white_list to {"Finder"}

然后使用类似的东西:

set orginizedList to ""
repeat with anItem in rest of white_list
  set orginizedList to orginizedList & anItem & ", "
end repeat
if (count white_list) < 2 then -- nothing added
  set orginizedList to "Finder"
else
  set orginizedList to text 1 thru -3 of orginizedList & " and Finder"
end if

这可能不是 OP 正在寻找的解决方案,但这是实现最终目标的不同方法,即一次退出多个应用程序。

对我来说,将应用程序添加到白名单变量并循环遍历这些项目等,似乎需要大量不必要的代码工作。

为什么不只从可见的应用程序进程列表中 select 项并将这些 selected 项传递给退出例程?

这是 AppleScript 代码的简化版本,它允许用户快速 select 应用程序从列表中退出,而无需额外的对话框和警报(如果需要可以很容易地添加回代码中) )

set quitList to {}
set pidList to {}

tell application "System Events" to set visibleApps to name of every application process whose visible is true

activate
set quitList to (choose from list visibleApps with title "Choose The Apps To Kill" with prompt ¬
    "Choose The Apps To Quit Running" OK button name "OK" cancel button name "CANCEL" with multiple selections allowed)

tell application "System Events"
    repeat with i from 1 to count of quitList
        set thisItem to item i of quitList
        tell application process thisItem
            set thePID to unix id
            set end of pidList to thePID
        end tell
    end repeat
end tell

repeat with i in pidList
    do shell script "kill " & i
end repeat

更新:更进一步,下面的代码允许选择要杀死的可见应用程序或不可见应用程序进程。

use AppleScript version "2.5" -- runs on 10.11 (El Capitan) and later
use framework "Foundation"
use scripting additions

global appsToKill
property NSArray : a reference to current application's NSArray

activate
set theChoice to button returned of (display dialog ¬
    "WOULD YOU LIKE TO LIST VISIBLE OR INVISIBLE APP PROCESSES?" buttons {"CANCEL", "VISIBLE", "INVISIBLE"} default button "INVISIBLE" cancel button "CANCEL" with title ¬
    "  WOULD YOU LIKE TO LIST VISIBLE OR INVISIBLE APP PROCESSES?  " with icon 2 giving up after 10)

if theChoice is "INVISIBLE" then
    listInvisibleAppProcesses(false)
else if theChoice is "VISIBLE" then
    listInvisibleAppProcesses(true)
else if theChoice is "CANCEL" then
    return
else if theChoice is "" then
    return
end if

set aList to ((NSArray's arrayWithArray:appsToKill)'s sortedArrayUsingSelector:"compare:") as list

activate
set killApp to (choose from list aList with title "Choose The App To Kill" with prompt ¬
    "Choose The App" 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

repeat with i in pidList
    do shell script "kill " & i
end repeat

on listInvisibleAppProcesses(trueOrFalse)
    tell application "System Events"
        set appsToKill to name of every application process where visible is trueOrFalse
    end tell
end listInvisibleAppProcesses