Add items to dock

Hey, I’m trying to create a script that’ll let me add items to the dock. A few hours of searching google haven’t led me any closer to an answer so i was wondering if anyone here had an idea. Basically i need to be able to add and remove items from the dock via a script.
Thanks

Operating System: Mac OS X (10.4)

Mac OS X Hints:
http://www.macosxhints.com/article.php?story=20040819170450489
Here:
http://bbs.applescript.net/viewtopic.php?id=3296

The tip from MacOSXHints.com works. The only caveat is that you need to quit the Dock before writing the prefs. You could write the pref and then force quit the Dock using the “kill” command but then other changes made to the Dock by the user will be discarded. So, quit the Dock (getting it to save any changes the user may have made), write the pref and then relaunch the Dock. The Dock will relaunch itself so the last step may not be strictly necessary but it can’t hurt. The following script works but I can imagine in some instances that maybe the script won’t be fast enough and the Dock will re-spawn before the prefs are changed (this is probably not very probable but it’s something to consider if the script fails):

my add_item_to_dock(choose file of type {"APPL"} with prompt "Choose an application to add to the Dock:")

on add_item_to_dock(item_path)
	try
		get item_path as alias -- you need a POSIX path so this coerces the path in case it's an HFS path, alias, file ref, etc.
		set item_path to POSIX path of item_path
	end try
	try
		tell application "Dock" to quit
	end try
	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>'"
	try
		tell application "Dock" to activate
	end try
end add_item_to_dock

Jon

Thanks, that works great. I have one more question, is it possible to make the script remove all of the dock items first (perhaps by emptying the contencts of the com.apple.dock.plist file). Basically this script needs to be able to dump all dock items and add specific new ones.
-Thanks

This script should work for you, rebelcommander. You should also able to add multiple apps/files at once.
I found that quitting the dock afterwards is fine for me (panther). I think I heard that in tiger, if you quit the dock it doesn’t relaunch so I have forced the dock to open again (by using the repeat, try, and exit repeat statements).

– Edit: added ‘add folder’

set the_choice to (choose from list {"Add Application", "Add File", "Add Folder", "Remove All"} with prompt "Dock actions:") as string
if the_choice is "false" then error number -128
try
	if word 1 of the_choice is "Add" then
		if word 2 of the_choice is "Application" then
			set these_files to choose file of type "APPL" with prompt "Choose an application to add to the Dock:" with multiple selections allowed without invisibles
			set persistent_what to "persistent-apps"
		else if word 2 of the_choice is "File" then
			set these_files to choose file with prompt "Choose a file to add to the Dock:" with multiple selections allowed without invisibles
			set persistent_what to "persistent-others"
		else
			set these_files to choose folder with prompt "Choose a folder to add to the Dock:" with multiple selections allowed without invisibles
			set persistent_what to "persistent-others"
		end if
		
		repeat with i from 1 to the count of these_files
			set this_file to POSIX path of (item i of these_files as alias)
			do shell script "defaults write com.apple.dock " & persistent_what & " -array-add '<dict><key>tile-data</key><dict><key>file-data</key><dict><key>_CFURLString</key><string>" & this_file & "</string><key>_CFURLStringType</key><integer>0</integer></dict></dict></dict>'"
		end repeat
	else
		set button_returned to button returned of (display dialog "Are you sure you want to remove everything from your Dock?" buttons {"Cancel", "OK"} default button "OK")
		do shell script "defaults delete com.apple.dock persistent-apps"
		do shell script "defaults delete com.apple.dock persistent-others"
	end if
	
	tell application "Dock"
		quit
		repeat
			try
				activate
				exit repeat
			end try
		end repeat
	end tell
on error error_message number error_number
	if error_number is not -128 then -- not user cancelled
		display dialog "Error: " & error_number & return & error_message
	end if
end try

You can add applications to the file section of the dock too! :stuck_out_tongue:

I like the script! But, how do you set it so when the dock quits the minimised files will stay? Or when the dock reopens the files return to their minimised state?

Hi! is it possible to access the dock files from within a hidden folder and just remove them?

Great script. 10.6 Snow Leopard users beware, you need to add bolded section (at least for files): “defaults write com.apple.dock persistent-others -array-add ‘tile-datafile-data_CFURLString/Users/user/Filepath/_CFURLStringType0tile-typefile-tile’; killall Dock”

The addition doesn’t break it in Mavericks, so i’m inclined to keep it, but it does butcher Dock in 10.6 without it - and crash system in fact!! !:wink: To fix, if you have already done it, go to user/Library/Preferences/com.apple.dock and neatly delete the you just added, end to end. Save, then killall Dock and it’ll be right as rain again! :wink:

T

Wonderful scripts. I’m running them in Sierra but it’s allowing to select 1 application instead of multiple Applications.:frowning:

Model: iMac
AppleScript: 2.9 (191)
Browser: Safari 602.4.8
Operating System: Mac OS X (10.10)

Hi.

It’s the same in El Capitan. If of type “APPL” (or of type {“APPL”}) is specified with ‘choose file’, then with multiple selections allowed is ignored for files of that type. With a mixture of types specified, you can choose either one application OR one or more files of the other type(s). With no types specified, you can select multiple application files.

Things work as expected if the ‘type identifier’ is used instead of the ‘file type’:

set these_files to choose file of type {"com.apple.application-bundle"} with prompt "Choose an application to add to the Dock:" with multiple selections allowed without invisibles