on newDockMenuFolder(folderAlias)
tell application "Finder" to if (class of item folderAlias) is not folder then return
-- 1 construct the |file-data| record
set folderPath to (POSIX path of folderAlias)
if (last character of folderPath) = "/" then
set folderPath to (text 1 thru -2 of folderPath)
end if
set newFileData to {|_CFURLString|:folderPath, |_CFURLStringType|:0}
--2 construct the |tile-data| record
tell application "Finder" to name of folderAlias
set newTileData to {arrangement:0, displayas:1, |file-data|:newFileData, |file-label|:the result, |file-type|:2, showas:3}
-- 3 construct the newMenuFolderItem record
set newMenuFolderItem to {|tile-data|:newTileData, |tile-type|:"directory-tile"}
-- 4 write to Dock preference file
set prefsFile to (((path to preferences from user domain) as text) & "com.apple.dock.plist") as alias
tell application "System Events"
set dockPath to (POSIX path of prefsFile)
set dockRec to (value of property list file dockPath)
(|persistent-others| of dockRec) & {newMenuFolderItem}
set |persistent-others| of dockRec to the result
set (value of property list file dockPath) to dockRec
end tell
delay 1
tell application "Dock" to quit -- calculate missing data
end newDockMenuFolder -----------------------------