[英]Applescript to remove items from 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
但我想拉出當前項目的列表並對其進行迭代。 這個堆棧溢出問題似乎涵蓋了如何獲取列表。 我想做的是調整上面的代碼以在循環內操作。 我猜想在循環內引用列表的當前項目將通過“ 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
使用最新版本的macOS Mojave,此AppleScript代碼對我有用。
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
項,而不是“從停靠中全部刪除”只想從擴展塢中刪除dockAppsList
項目1至5。
作為替代方案...這是一種更直接的方法,用於刪除com.apple.dock.plist
文件的persistent-apps
項中找到的Dock上的persistent-apps
:
在Terminal中 ,執行以下操作首先備份目標文件:
cd ~/Library/Preferences
cp -a com.apple.dock.plist com.apple.dock.plist.bak
現在,要刪除持久性應用程序 ,請使用以下復合命令 :
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
如果出於某些原因需要使用AppleScript進行此操作,則可以使用do shell script
命令運行這些Shell命令 。
注意:在您的OP中,您說過“我正在嘗試從擴展塢中刪除(所有)項目。” 並且您提供的代碼僅關注存儲在persistent-apps
鍵下的persistent-apps
。 在Dock上還可以顯示其他項目 ,第一個是默認的persistent-others
,其中包含下載 堆棧以及您已添加到該部分的其他項目。 然后使用macOS Mojave ,在Dock上的上述兩個部分之間( 按鍵名)顯示了recent-apps
。 在這些鍵上也可以使用相同的前提,在defaults delete ...
復合命令中 ,將persistent-others
或recent-apps
替換為persistent-apps
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.