Add to dock from list of apps

Hello,

I am trying to do the following:

For each item in a given list of applications:

¢ see if the application exists (may be in /Applications, /Applications/Utilities, ~/Applications, or any subfolder thereof)
¢ if it does, get the path to the application
¢ use that path to write an entry in com.apple.dock.plist with ‘defaults write’ shell script

I’ve cobbled this together which totally doesn’t work :smiley: :

on run
	
	set appList to {"Stuffit Expander", "DropStuff"}
	
	-- the list of applications I want to add to the dock, if they exist
	
	repeat with i from 1 to number of items of appList
		set myApp to item i of appList
		if myApp exists then
			set AppPath to path to application myApp
			set appPosix to POSIX path of AppPath
			
			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>" & appPosix & "</string><key>_CFURLStringType</key><integer>0</integer></dict></dict></dict>'"
			
			-- run defaults shell script to write path to plist
		end if
	end repeat
	
	do shell script "kill_pid=`ps ax | grep Dock | grep -v grep | awk '{ print $1 }'`; kill $kill_pid"
	
	-- kill dock to read in new plist
	
end run

What doesn’t work:

¢ if the app doesn’t exist, the script asks you to locate it (running it within Script Editor at least). I guess I could make it a try clause instead, but I am sure there’s a correct way to say ‘if the app exists, then add it, if not skip…’?

¢ as the script gets the path, it also launches the application it’s getting the path of.

Followup question: Is there anyway to organize the entries in com.apple.dock, for instance according to a master list, using a script (e.g. put my main apps to the left, my utilities to the right…)

Thanks much for your help,

Michael

Bump.

Can anyone help?

Thanks.

Michael

You can use the ‘application file id’ which uses the creator type of the app to get a reference. Something like this:

set ct_list to {“SITx”, “DStf”, “bozo”}
repeat with this_ct in ct_list
tell application “Finder”
try
set app_ref to (application file id this_ct) as alias
– do something
on error – app doesn’t exist
display dialog (this_ct & " does not exist")
– do something else
end try
end tell
end repeat

You can get creator type with something like this:

set app_list to {“Stuffit Expander”, “DropStuff”}
set fcs to {}
repeat with this_app in app_list
set app_ref to (path to application this_app)
set app_info to (info for app_ref)
set fc to file creator of app_info
set end of fcs to fc
end repeat
fcs

Look at the return.

gl,

Thanks, Kel.

I’ll play with this tomorrow and see how it goes.

Michael

As seperate scripts, they do work. And I think that’s what you intended, Kev.

However, I would prefer for a user of the script to be able to input (either by editing the beginning of the script, or by editing an accompanying text or plist file) the name of the app and not the creator type.

So I tried joining them together:

set app_list to {"Stuffit Expander", "DropStuff"}
set ct_list to {}
repeat with this_app in app_list
	set app_ref to (path to application this_app)
	set app_info to (info for app_ref)
	set fc to file creator of app_info
	set end of ct_list to fc
end repeat

repeat with this_ct in ct_list
	tell application "Finder"
		try
			set app_ref to (application file id this_ct) as alias
			set appPosix to POSIX path of app_ref
			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>" & appPosix & "</string><key>_CFURLStringType</key><integer>0</integer></dict></dict></dict>'"
		on error -- app doesn't exist
			--display dialog (this_ct & " does not exist")
		end try
	end tell
end repeat

do shell script "kill_pid=`ps ax | grep Dock | grep -v grep | awk '{ print $1 }'`; kill $kill_pid" -- kill dock to read in new plist

This script doesn’t work very well, however. If the apps play nicely and do exist, it works. But if they apps either don’t understand the ‘path to message’ or if they don’t exist, the script breaks.

Another problem is that the part of the script that gets the creator type also launches the apps.

I commented out the dialog about an app not existing. I prefer the script to remain silent on error.

Any comments or help appreciated.

Thanks!

Michael

Hi Michael,

I don’t like the idea of the user typing in the name of the app. An easy way for the user, but more work for you would be to have the user chose from a list. You could use two lists as in the following:

property app_list : {“1 - Stuffit Expander”, “2 - DropStuff”}
property app_cts : {“SITx”, “DStf”}
set user_choice to item 1 of (choose from list app_list)
set i to (word 1 of user_choice) as integer
set ct to item i of app_cts
tell application “Finder”
try
set app_ref to (application file id ct) as alias
set ct_found to true
on error – creator type not found
set ct_found to false
end try
end tell
if ct_found then
set unix_path to quoted form of (POSIX path of app_ref)
– do more
end if – else do nothing

I didn’t want to loop through the list, so I added the index within the list.

I don’t know any other way of getting a reference to an app (short of searching through the Applications folder maybe with unix) without launching the app.

gl,

Hi Kel,

Actually, I wouldn’t have the user do this. The script will be used as part of a post install setup where I work. A tech may want to tweak the script every once in a while, that’s all.

What I am toying with right now is to add in all conceivable apps. You script elegantly ignores the ones that aren’t present so there’s no harm done in having more apps listed that will be added. I’ve prefaced it with a defaults delete shell script command to clear the dock. The script can then add them in an appropriate order.

Here’s what I’ve got right now. It’s seems to work fine. Two apps (Sherlock and an Applescript applet don’t have unique creator codes, so they get added in the end by a hard path. The downside is, if they aren’t present, you get the question marks in the dock. I am sure it can be done more elegantly but I am not a script wiz (yet :-).

-Michael

do shell script "defaults delete com.apple.dock persistent-apps" -- Clear the dock of existing icons

set ct_list to {"InDn", "8BIM", "ART5", "XPR3", "SPIF", "Cal*", "FMP5", "AOp3", "fez!", "sfri", "MOZB", "OPIM", "MSWD", "PPT3", "XCEL", "FRBR", "SITx", "DStf"}

repeat with this_ct in ct_list
	
	tell application "Finder"
		
		try
			set app_ref to (application file id this_ct) as alias
			set appPosix to POSIX path of app_ref
			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>" & appPosix & "</string><key>_CFURLStringType</key><integer>0</integer></dict></dict></dict>'"
			
		on error -- app doesn't exist
			-- display dialog (this_ct & " does not exist") -- commented out because I don't want feedback about missing apps
		end try
		
	end tell
end repeat

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>/Applications/Sherlock.app</string><key>_CFURLStringType</key><integer>0</integer></dict></dict></dict>'" -- add Sherlock, doesn't have a creator code

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>/Applications/Start Outlook.app</string><key>_CFURLStringType</key><integer>0</integer></dict></dict></dict>'" -- add Start Outlook, doesn't have a creator code

do shell script "kill_pid=`ps ax | grep Dock | grep -v grep | awk '{ print $1 }'`; kill $kill_pid" -- kill dock to read in new plist