Automation Applescript从dock中删除项目

Automation Applescript从dock中删除项目,automation,applescript,dock,Automation,Applescript,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 但是我想提取当前项目的列

我正试图从码头移除(所有)物品。我可以按名称删除它们,如下所示:

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
但是我想提取当前项目的列表并对其进行迭代。 似乎涵盖了如何获取列表。我想做的是调整上面的代码,使其在循环中运行。我猜在循环中引用列表的当前项将使用“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  

最好先备份“com.apple.dock.plist”文件。以下两行AppleScript代码将把您当前的com.apple.dock.plist文件复制到您的桌面上。如果您想将Dock图标恢复到运行本文第二个脚本之前的状态,这将非常有用

set plistpath to (path to preferences folder as text) & "com.apple.dock.plist"
tell application "Finder" to duplicate alias plistpath to desktop

这个AppleScript代码适用于我使用最新版本的macOS Mojave

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 {}
-- Gets app names and adds them to dockAppsList
repeat with i from 1 to count of persistentAppsList
    set thisItem to item i of persistentAppsList
    set appName to |file-label| of |tile-data| of thisItem
    set end of dockAppsList to appName
end repeat

-- Loops through each app in dockAppsList and removes each app from Dock
repeat with thisRecord in dockAppsList
    tell application "System Events"
        tell UI element thisRecord of list 1 of process "Dock"
            try
                perform action "AXShowMenu"
                click menu item "Options" of menu 1
                click menu item "Remove from Dock" of menu 1 of menu item "Options" of menu 1
            on error
                try
                    perform action "AXShowMenu"
                    click menu item "Remove from Dock" of menu 1
                end try
            end try
        end tell
    end tell
end repeat

我意识到我可以把所有的东西都包含在一个大的重复循环中。我认为,出于本脚本的目的,最好将两个循环事件分开,以防脚本中的其他地方可能需要引用
dockAppsList
的项,而不是“从dock中删除所有内容”您可能只想从dock中删除
dockAppsList
中的项目1到5。

作为替代。。。以下是一种更直接的方法,用于删除
com.apple.Dock.plist
文件的
persistent apps
键中的Dock上的持久应用:

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

  • cd~/Library/Preferences
  • cp-a com.apple.dock.plist com.apple.dock.plist.bak
  • 现在,要删除持久应用程序,请使用以下复合命令:

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

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



    注意:在您的OP中,您声明“我正在尝试从dock中删除(所有)项目。”并且您提供的代码只关注存储在
    持久应用
    键下的应用。还可以在Dock上显示其他项目,第一个是默认的
    持久性其他项目
    ,其中包含下载堆栈和您添加到该部分的其他项目。然后在macOS Mojave上有
    最近的应用程序
    ,它显示在Dock上上述两个部分(按键名)之间。同样的前提也可以用在这些键上,在
    默认删除…
    复合命令中用
    持久化其他
    最近的应用
    替换
    持久化应用

    如果你问我,这个代码似乎有点破坏性。首先备份您的“com.apple.dock.plist”文件可能是明智的做法。^谢谢,您能提供一个语法示例吗?我知道我要求的是applescript,但我真的很喜欢两行答案的简洁和简单。@jorgus,正如我在回答中提到的。。。如果出于某种原因需要使用AppleScript执行此操作,可以使用
    do shell script
    命令来运行这些shell命令
    defaults delete com.apple.dock persistent-apps; killall Dock
    
     cd ~/Library/Preferences; rm com.apple.dock.plist; cp -a com.apple.dock.plist.bak com.apple.dock.plist; killall Dock