Can Applescript list all the applications placed in the dock?

﹥>﹥吖頭↗ 提交于 2019-12-13 18:08:56

问题


I can't work out how to list all the Applications that a user has placed in the dock.

Is this possible?


回答1:


Try this. This is a list of the apps a person has that are persistent in the dock. What I've basically done is use system events to read the plist file into an applescript record in the pListItems variable. Then I can use applescript techniques to access the lists and records inside of pListItems.

There's lots of information in com.apple.dock so you can look at the pListItems variable and work your way through it to pull out whatever you need. For example you might want the "|bundle-identifier|" instead of the "|file-label|". Good luck.

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

return dockAppsList



回答2:


Adding on to the response by regulus6633 As suggested, using |bundle-identifier| does make for more reliable results in this script. For example, Evernote will not correctly identify in all AppleScript uses by using the |file-label| property due to both Evernote.app and EvernoteHelper.app having the same short name (CFBundleName).

Additional idea I used this script as a basis for one that start all applications that are permanently placed in the dock ('Keep in dock' option). I removed the dockAppsList array and replaced the second loop to activate all these applications. To avoid having windows splattered all over my screen, I maintain appName and use it to hide them right after activating the application.

To adjust, replace code after the end tell statement with the following:

repeat with thisRecord in |persistent-apps| of pListItems
set appName to |file-label| of |tile-data| of thisRecord
set appID to |bundle-identifier| of |tile-data| of thisRecord
tell application id appID to activate
tell application "Finder" to set visible of process appName to false
end repeat


来源:https://stackoverflow.com/questions/14245301/can-applescript-list-all-the-applications-placed-in-the-dock

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!