【问题标题】:Applescript get list of running apps?Applescript获取正在运行的应用程序列表?
【发布时间】:2013-08-24 16:54:41
【问题描述】:

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

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

谢谢!

【问题讨论】:

    标签: macos applescript osx-mountain-lion


    【解决方案1】:

    试试这个:

    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
    

    【讨论】:

    • 像魅力一样工作!!!我的最后一个问题是,如果没有 killall 命令,您将如何做到这一点?有点像,告诉应用程序 selectedProcesses 退出。谢谢!
    • 参见上面 Parag 脚本的底部。
    • 编辑添加了 Parag 的脚本部分。
    • killall 默认情况下会向相关进程发送信号 15,告知进程尽快退出。最好先发送信号 HUP,即killall -HUP,这通常允许进程在关闭之前进行一些清理。
    【解决方案2】:

    你可以试试这个

    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
    

    【讨论】:

      【解决方案3】:
      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
      

      【讨论】:

      • 脚本的上半部分不适合我,但下半部分可以!谢谢!
      【解决方案4】:

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

      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
      

      【讨论】:

        【解决方案5】:

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

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

        【讨论】:

          【解决方案6】:

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

          【讨论】:

            【解决方案7】:

            以下示例 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
            

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

            set appList to SortList(appList)
            

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

            【讨论】:

              【解决方案8】:

              几年前我根据 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
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2019-07-16
                • 2019-06-28
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多