Check for an item in dock

Hello all, this is my first post. I’m writing an applescript that needs to check if an item is in the dock. How do I do that? I’ve tried something like

set items to (container of (applications in application "Dock"))

that I could then search through,
but that didn’t work. If the item exists, I need to find out the items absolute path. Any help would be greatly appreciated.

Everything I’ve been able to find about the Dock indicates that it isn’t scriptable. If that has changed, it has been recently. The few scripts I’ve seen that do anything at all to the Dock simply move it or resize it (KnockoutDock and Transparent Dock come to mind) and some of that is done using shell scripts.

Are you wanting to know if an item exists in the Dock, or if the process (or application) is running currently?

Let us know what you want to accomplish and perhaps we can find a way to work around the Dock’s lack of script support.

I’m developing a version of QEMU-Puppy Linux and have created an Applescript to launch it. The only way I could figure out how to determine where it was launched from is to first check if the foremost Finder window contains the Puppy.app directoy. If not, I then check the Desktop directory. If it isn’t there, I now need to check the dock. If the Puppy.app is found in the dock, I need to find out where it is on the system in order to set my shell script to launch it from there. This is first OS X application of any kind I have created, so I’m not comfortable coding up a C++ launcher since I don’t know the APIs. Applescript just seemed like the next best choice.

You should be able to use

path to (application "Puppy")

which should return the location of the application on your machine. You don’t need to specifiy “.app” since the application keyrword figures that out for you.

What you will get back is a path in the form of “Macintosh HD:Applications:Puppy folder:” (or something similar).

Thank you so much for your help. Will this return the path if the app isn’t in the /Applications directory? What I mean is, if the user calls it from the dock, will that determine the location of the app by where the dock item points to?

Yes. There is nothing really special about the /Applications directory, aside from the fact that most installers put their applications there and most users do the same. You could create a directory called “Apps” in your home directory and drag and drop programs to it and the “path to” statement would still find the app you wanted. The downside is the other users (if any) could not run those programs out of your home folder.

The system keeps a database of applications and where they are located on the disk. The “path to” statement finds apps using that lookup. “Path to” works for other items as well: system folder, home folder, me (the current script), etc. There is a whole list of locations it knows how to find, but not all of them still apply as the command was created during the OS 9 years and some of those folders don’t exist anymore.

But it still works for applications!

Do you have any idea how much time you just saved me? I am eternally grateful. I wish I knew this when I began!

You are most welcome! :smiley:

I know how you feel. I felt the same way when I needed help in the forum and someone guided me in the right direction!

If you get really interested in scripting, there are some good books on the subject. Danny Goodman (who wrote a book on Hypercard before Applescript existed) has a good one. All his books are good, and he writes on Javascript, DHTML, and Applescript. I highly recommend him.

By the way, does - path to (application “Puppy”) - work with older versions of OS X? And does this work on inserted volumes such as USB keys and CDs? Thanks again.

Yes, it should. “Path to” goes all the way back to OS 9.

Again, it should. I tried it with inserted CD’s that have applications, and running under Jaguar it found both an OS 9 and an OS X application BUT it also started those apps, which may be a side effect you don’t want.

The following is a bit late to the party, and it uses UI scripting to test if an app is in the Dock, but it does work for me. Change the variable appToClick to the name of the application you are testing to see if it is in the Dock. Note that the script will “click” the app in the Dock and run it if the app does exist in the Dock.



set appToClick to "Mail"

tell application "System Events"
	
	tell process "Dock"
		
		tell list 1
			
			try
				set dockItemTest to UI element appToClick -- test if the application is in the Dock
			on error
				beep
				tell current application to display dialog "The \"" & appToClick & "\" application is not in the Dock." buttons {"Cancel"} default button 1 with icon 0 with title "APPLICATION NOT IN THE DOCK"
			end try
			
			click UI element appToClick
			
		end tell
		
	end tell
	
end tell

Great thanks. I have to say I’m impressed with the community here. I’ve only worked with linux until now and am glad to see that if I need help, it’s available.

If you really want to know not just the path to a running app, but whether the app is in the Dock (running or not), here is a way:


set theApp to name of application "TextEdit" -- or whatever

set dockApps to do shell script "defaults read com.apple.dock persistent-apps"

if theApp is in dockApps then
	set answer to true
else
	set answer to false
end if

Johnny, that is a gem, you should post that in the Code Exchange section.

Thanks. I will.

J