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

时间:2019-02-21 15:56:57

标签: list applescript

因此,我正在尝试退出所有包含您不想退出的应用程序的白名单的应用程序。尽管输入白名单项目后,我希望它说“确定要退出所有应用程序,但要退出”,然后说白名单中的项目。但是白名单中的项目是结合在一起的,我输入了finder和chrome,但是它说:“您确定要退出finderchrome以外的所有应用程序吗?”。因此,我在下面添加了代码,并在白名单中输入了finder和chrome。

 function ariaChecker(el) {
   return Object.entries(el.attributes).filter((property) => {
     var newarr = []
       if (property.includes('aria')) newarr.push(property.toString())
         return newarr
       }).join(' ')
   }

现在它显示“您确定要退出除取景器,取景器,取景器之外的所有应用程序吗?”

代码如下:

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
---------------------------------------------------------------------------------------------------------------------------

请帮助我解决问题!!!

2 个答案:

答案 0 :(得分:0)

白名单对话框有点混乱,无论如何添加默认条目,所以我将使用更具描述性的提示,将默认保留为空白,并在添加之前检查结果:

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

要多清理最后一个对话框,我还将在首个white_list中添加 Finder (因为它总是存在于其中):

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

答案 1 :(得分:0)

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

在我看来,要进行大量不必要的代码才能将应用程序添加到白名单变量并遍历这些项目等。

为什么不只是从可见的应用程序进程列表中选择项目,然后将这些选定的项目传递给退出例程?

这是AppleScript代码的简化版本,可快速使用户从列表中选择要退出的应用程序,而无需其他对话框和警报(可以根据需要轻松添加回代码中)

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

enter image description here

更新:更进一步,以下这段代码允许从可见的应用程序或不可见的应用程序进程中进行选择。

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

enter image description here enter image description here