for Google search...
This morning, before I had several hours cat-nap, I inadvertently posted a version of my script that was partially lifted from my test Applescript, instead of from my App, and the App version had two bug fixes.
So, for those interested, and for posterity, here’s a full Applescript, improved version of my Applescript version. Note my Application path, (item_path), includes a folder within the Applications Folder in my case.
use AppleScript version "2.4"
use framework "Foundation"
use framework "AppKit"
use scripting additions
my installDockIcon("Mail Manager")
on installDockIcon(theApp)
try
set item_path to ((path to applications folder) & "Mail Manager:" & theApp & ".app" as text) # NOTE: my App is in a folder called 'Mail Manager'
end try
try
set theInfo to (current application's NSUserDefaults's alloc()'s init()'s persistentDomainForName:"com.apple.dock") as record
set theMatches to get theInfo's |persistent-apps|
set MatchingList to {}
set x to 0
repeat with thisRecord in theMatches
set x to x + 1
set theTestMatch to get thisRecord's |tile-data|
set theMMMatch to theTestMatch's |file-label|
if theMMMatch as text is theApp then
set end of MatchingList to item x of theMatches
end if
end repeat
set existsMMDockIcon to my installDockItemsTest(theApp)
if (count of MatchingList) < 1 or not existsMMDockIcon then
try
set item_path to POSIX path of item_path
do shell script "defaults write com.apple.dock persistent-apps -array-add '<dict><key>tile-data</key><dict><key>file-data</key><dict><key>_CFURLString</key><string>" & item_path & "</string><key>_CFURLStringType</key><integer>0</integer></dict></dict></dict>'"
tell application "Dock" to quit # Automatically re-starts
end try
end if
end try
end installDockIcon
on installDockItemsTest(theApp)
set x to 1
try
tell application "System Events"
tell process "Dock"
set t to (title of UI elements of list 1)
set x to 0
repeat with theTest in t
if theTest as text = theApp then set x to x + 1
end repeat
end tell
end tell
end try
return (x > 0)
end installDockItemsTest