Programming the Dock with Applescript

Hi All

Is it possible to program the dock tiles or Icons using Applescript. For example the Mail Icon in the Dock has a small red badge with a number in it when new mail arrives. Can something like this be done in Applescript or is it only done with Apple Developer code.
Any help with information on Applescript for the Dock would be great.

Thanks, PolishPrince

Had the same idea years ago when programming in vanilla applescript. But since there is nothing in the dictionary of any apps that allows control over the dock, there is nothing you can do.

However, in AppleScriptObjC, you can control the appearance of your app’s dock quite easily, thanks to cocoa. But that requires making the jump over to Xcode and AppleScriptObjC.

Model: MacBookPro8,2
Browser: Safari 534.51.22
Operating System: Mac OS X (10.7)

I didn’t try this but it seems reasonable. Since the dock icon is just the icon of the application, you could script changing the icon in the FInder and the change should show up in the dock (you may have to quit and relaunch the dock). So really all you’d have to do is get the icon from the application and setup a bunch of new icons however you want them to look in an image editing program.

For example, if you wanted to put numbers on Safari like you see with Mail, you could get Safari’s icon from its bundle. Then open that icon in an image editor and create a bunch of images, one with a red #1, one with a red #2 etc. Then you use these images in your applescript code to change the application’s icon and possibly quit/relaunch the dock after each image change.

NOTE: I have a tool you could use here to change the app’s icon with applescript.

Hi Regulus6633

Thank you for your reply and example for changing an Icon. It is what I was looking for and I will try to incorporate it into my script.

PolishPrince

Hi Regulus6633

I finally got the code that you suggested incorporated into my script. The script works now with one exception. The Icon is being changed on the script in the folder “Unixbins” and on the Dock Icon, but the Icons are alternating when I run my script. What I am saying is the script has the right Icon but the one on the dock is not right. As you can see I have icons with the “Icns” extensions, but it works the same with the “PNG” icons as well. If I right click on the Icon in the dock and select “Option, show in finder” the dock Icon will change to the correct one. Below is my script.


set exePath to (path to home folder as text) & "UnixBins:SetFileIcon"
tell application "System Preferences" to reveal anchor "output" of pane id "com.apple.preference.sound"
try
	tell application "System Events" to tell (table 1 of scroll area 1 of tab group 1 of window "Sound" of application process "System Preferences")
		set S to selected of rows
		if item 2 of S is false then
			select row 2
			
			set imagePath to (path to home folder as text) & "UnixBins:Headset Off.icns"
			set imageName to (imagePath as string)
			
		else
			select row 5
			
			set imagePath to (path to home folder as text) & "UnixBins:Headset On.icns"
			set imageName to (imagePath as string)
			
		end if
	end tell
	onerror quit
end try

set theFile to (path to home folder as text) & "UnixBins:Headset ON-OFF.app"

do shell script quoted form of POSIX path of exePath & " -image " & quoted form of POSIX path of imagePath & " -file " & quoted form of POSIX path of theFile

I found something based on your explanation about pressing the dock icon to make the change happen. This code is getting the dock icon to update, so stick this code at the end of your script and let us know if it helps.

set appName to "Headset ON-OFF"
tell application appName to quit -- only needed if your app is a stay-open application
tell application "System Events"
	tell process "Dock"
		if exists UI element appName of list 1 then
			tell UI element appName of list 1 to perform action "AXPress"
		end if
	end tell
end tell

Regulus6633

That didn’t work. The only thing that changes the Icon to match the correct one on the script is If I right click on the Icon in the dock and select “Option, show in finder”. It changes immediately when I click on “show in finder”.

OK, I had my app on the desktop so I could more easily see what was happening. That location may account for the behavior I saw.

It seems like this solution isn’t really workable. The dock updates itself when it wants, and there’s probably nothing you can do about it from applescript. If you want a reliable solution you may have to develop your own cocoa application because cocoa applications can reliably update their own dock icon. You could make that a scriptable app so you could send applescript commands to it to make the change from applescript. It’s not hard to do but requires a different skill set than normal applescript.

If I think of something else I’ll let you know. Good luck.

Just to let you know, with all of your help I finally got my script to work correctly. I had to change some of the code for doing the Dock update and the placement of the Shell Script coding. When the icon on the Dock is pressed I get the flashing of the options menu, but so far I don’t think I can stop that. The code is enclosed to show you what did work.
Thanks for all of your help

PolishPrince


-- Script to change the sound preferences from Line out to Bluetooth Headset.
set exePath to (path to home folder as text) & "UnixBins:SetFileIcon"
tell application "System Preferences" to reveal anchor "output" of pane id "com.apple.preference.sound"
try
	tell application "System Events" to tell (table 1 of scroll area 1 of tab group 1 of window "Sound" of application process "System Preferences")
		set s to selected of rows
		if item 2 of s is false then
			select row 2 -- Lineout
			
			set imagePath to (path to home folder as text) & "UnixBins:Headset Off.icns"
			set imageName to (imagePath as string)
			
		else
			select row 5 -- Bluetooth Headset
			
			set imagePath to (path to home folder as text) & "UnixBins:Headset On.icns"
			set imageName to (imagePath as string)
			
		end if
	end tell
	onerror quit
end try

set theFile to (path to home folder as text) & "UnixBins:Headset ON-OFF.app"

--To Click Show in finder of the Dock Icon: This matches the Icons on the Application with the Dock Icon.
set appName to "Headset ON-OFF"
tell application "System Events" to tell UI element appName of list 1 of process "Dock"
	perform action "AXShowMenu"
	click menu item "Options" of menu 1
	click menu item "Show in Finder" of menu 1 of menu item "Options" of menu 1
end tell

do shell script quoted form of POSIX path of exePath & " -image " & quoted form of POSIX path of imagePath & " -file " & quoted form of POSIX path of theFile

Hey congratulations. You found a workable solution for yourself. That’s good news. It’s funny that you actually click the options menu before changing the Finder icon. I would have thought the other way around would be better. Anyway, good luck.

If you want to know how to do this in cocoa applescript (asobjc) i have got the code here:

set thePath to (choose file of type “png”)
set theImage to current application’s NSImage’s alloc()'s initWithContentsOfFile:POSIX path of (thePath)
current application’s NSApp’s setApplicationIconImage:theImage

if you already have a variable set to the images path make sure it is in POSIX file not POSIX path
and use variable: “thePath”