Your code certainly does work. I’ve worked 15 hours per day for the last 4 days, and in my (delirious) state, I forgot to add an on error trap. Turned out my perennial problem with Accessibility was stopping my app from accessing your script.
I’ve fixed the problem, and now my app, using yours and Shanes guidance, creates a fixed icon even when the previous icon has been dragged off the Dock.
on installDockIcon()
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|
tell current application to say (count of theMatches) -- returns '61'
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 is "Mail Manager" then
set end of MatchingList to item x of theMatches
end if
end repeat
set existsMMDockIcon to my installDockItemsTest()
if (count of MatchingList) = 0 or not existsMMDockIcon then
do shell script ("sleep 0.2")
try
set item_path to ((path to applications folder) & "Mail Manager:Mail Manager.app" as text) end try
do shell script ("sleep 0.2")
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
end try
end if
set theInfo to (current application's NSUserDefaults's alloc()'s init()'s persistentDomainForName:"com.apple.dock") as record
end try
end installDockIcon
on installDockItemsTest()
set x to 0
try
tell application "System Events"
tell process "Dock"
set t to (title of UI elements of list 1)
repeat with theTest in t
if theTest as text = "Mail Manager" then set x to x + 1
end repeat
end tell
end tell
end try
return (x > 0)
end installDockItemsTest