Applescript 从 Dock 中删除项目

jor*_*fus 3 applescript automation dock

我正在尝试从坞站中删除(所有)项目。我可以按名称删除它们,如下所示:

tell application "System Events"
    tell UI element "Launchpad" of list 1 of process "Dock"
        perform action "AXShowMenu"
        click menu item "Remove from Dock" of menu 1
    end tell
end tell
Run Code Online (Sandbox Code Playgroud)

但我想拉出当前项目的列表并迭代它们。 这个堆栈溢出问题似乎涵盖了如何获取列表。我想做的是调整上面的代码以在循环内运行。我猜想在循环内引用列表的当前项目将使用“thisRecord”完成。我想我误解了如何将“thisRecord”转换为我可以在系统事件中引用的内容。

set plistpath to (path to preferences folder as text) & "com.apple.dock.plist"

tell application "System Events"
    set plistContents to contents of property list file plistpath
    set pListItems to value of plistContents
end tell
set persistentAppsList to |persistent-apps| of pListItems

set dockAppsList to {}
repeat with thisRecord in persistentAppsList
    set end of dockAppsList to |file-label| of |tile-data| of thisRecord
    tell application "System Events"
        tell UI element application thisRecord
            perform action "AXShowMenu"
            click menu item "Remove from Dock" of menu 1
        end tell
    end tell
end repeat  
Run Code Online (Sandbox Code Playgroud)

use*_*894 8

作为替代...这里是一个更直接转发办法,消除持久性应用程序的停靠在发现persistent-apps 关键的的com.apple.dock.plist文件:

终端中,执行以下操作以首先备份目标文件:

  1. cd ~/Library/Preferences
  2. cp -a com.apple.dock.plist com.apple.dock.plist.bak

现在要删除持久应用程序,请使用以下复合命令

defaults delete com.apple.dock persistent-apps; killall Dock
Run Code Online (Sandbox Code Playgroud)

如果以后要恢复备份,请使用以下复合命令

 cd ~/Library/Preferences; rm com.apple.dock.plist; cp -a com.apple.dock.plist.bak com.apple.dock.plist; killall Dock
Run Code Online (Sandbox Code Playgroud)

如果出于某种原因需要使用AppleScript执行此操作,则可以使用该do shell script 命令来运行这些shell 命令


注意:在您的 OP 中,您说“我正在尝试从坞站中移除(所有)项目”。并且您提供的代码仅关注存储在persistent-apps key下的应用程序。还有其他项目可以显示在Dock 上,第一个是 default persistent-others,其中包含下载 堆栈和您添加到该部分的其他项目。然后用MACOS莫哈韦recent-apps其上述两个区段(由间显示上的名称)底座。相同的前提下可以在这些使用按键以及,替代persistent-othersrecent-appspersistent-appsdefaults delete ... 复合命令

  • 我知道我要求使用 applescript,但我真的很喜欢 2 行答案的简洁和简单。 (2认同)