Getting a list of Dock Apps

Hey, I tried this, but it returns everything.

set totalData to GetEveryProcess()
return totalData

to GetEveryProcess()
	tell application "System Events"
		get name of every process
	end tell
end GetEveryProcess

I’m looking for a way to return the processes that are running so I don’t have to look at my dock. I just want the ones that would show up in my dock (or at least a trimmed down list)

Model: Mac Book Pro
AppleScript: 1.10
Browser: Firefox 2.0.0.7
Operating System: Mac OS X (10.4)

Hi scot,

is this sufficient?

set pListpath to ((path to preferences) as Unicode text) & "com.apple.dock.plist"
tell application "System Events"
	tell property list file pListpath
		tell contents
			set dockAppList to value of property list item "file-label" of property list item "tile-data" of property list items of property list item "persistent-apps"
		end tell
	end tell
end tell
set dockAppList to {"Finder" as Unicode text, "Dashboard" as Unicode text} & rest of dockAppList

Or if you just want the visible applications:

GetVisibleProcesses()

to GetVisibleProcesses()
	tell application "System Events"
		get name of every process whose visible is true
	end tell
end GetVisibleProcesses

John M

tell application "System Events" to set AppsRunning to name of processes whose visible is true and name is not "Finder" and name is not "anythingElseToExclude"

To get them the apps in the dock itself:

tell application "System Events" to tell process "Dock" to set DL to name of UI elements of list 1 whose subrole is "AXApplicationDockItem"

Or if you want all apps the are running and not daemons, including hidden ones.

getApps()

to getApps()
	tell application "System Events"
		get name of processes whose background only is false
	end tell
end getApps

I think this was new in 10.4.

John M

very :cool:

:slight_smile:

Thanks a bunch guys,

I’m currently using this with GeekTool and it’s working great!


tell application "System Events" to set totalData to name of processes whose visible is true and name is not "Finder" and name is not "SpamSieve"
set totalData to simpleReplace(",", "
", totalData)
set totalData to simpleReplace("firefox-bin", "Firefox", totalData)
return totalData
on simpleReplace(search, replace, subject)
	local search, replace, subject, ASTID
	
	set ASTID to AppleScript's text item delimiters
	set AppleScript's text item delimiters to search
	set subject to text items of subject
	
	set AppleScript's text item delimiters to replace
	set subject to "" & subject
	set AppleScript's text item delimiters to ASTID
	
	return subject
end simpleReplace

Just incase you guys want to use the script for geekTool, I just updated it to count my mail messages and show my current iTunes track. It’s pretty phat. I couldn’t figure out how to replace “iTunes” with a musical note, but I like it.

updated so that it doesn’t force Mail.app or iTunes to open


tell application "System Events" to set totalData to name of processes whose visible is true and name is not "Finder" and name is not "SpamSieve"

set totalData to simpleReplace(",", "
", totalData)
set totalData to simpleReplace("firefox-bin", "Firefox", totalData)
set totalData to simpleReplace("iTunes", "iTunes " & getTrack(), totalData)
set totalData to simpleReplace("Mail", "Mail " & countMail(), totalData)

return totalData

on getTrack()
	tell application "System Events" to set itunesRunning to (name of processes) contains "iTunes"
	if itunesRunning then
		tell application "iTunes"
			try
				set playerstate to (get player state)
			end try
			if playerstate = paused then
				set trackPaused to " (paused)"
			else
				set trackPaused to ""
			end if
			if playerstate = stopped then
				return "(stopped)"
			end if
			set trackID to the current track
			set trackName to the name of trackID
			set trackName to "'" & trackName & "'"
		end tell
		set itunesInfo to trackName & trackPaused
		return itunesInfo
	end if
end getTrack

on countMail()
	tell application "System Events" to set mailRunning to (name of processes) contains "Mail"
	if mailRunning then
		tell application "Mail" to set unreadCount to unread count of inbox
		if unreadCount = 0 then
			set unreadCount to ""
		else
			set unreadCount to "(" & unreadCount & ")"
		end if
		return unreadCount
	end if
end countMail

on simpleReplace(search, replace, subject)
	local search, replace, subject, ASTID
	
	set ASTID to AppleScript's text item delimiters
	set AppleScript's text item delimiters to search
	set subject to text items of subject
	
	set AppleScript's text item delimiters to replace
	set subject to "" & subject
	set AppleScript's text item delimiters to ASTID
	
	return subject
end simpleReplace

I updated this script to work with SpiritedAway. Before it wouldn’t show the apps that were hidden.


tell application "System Events" to tell process "Dock" to set totalData to name of UI elements of list 1 whose subrole is "AXApplicationDockItem"

set totalData to simpleReplace(",", "
", totalData)
set totalData to simpleReplace("firefox-bin", "Firefox", totalData)
set totalData to simpleReplace("iTunes", "iTunes " & getTrack(), totalData)
set totalData to simpleReplace("Mail", "Mail " & countMail(), totalData)
set totalData to totalData & checkTrash()

return totalData

on getTrack()
	tell application "System Events" to set itunesRunning to (name of processes) contains "iTunes"
	if itunesRunning then
		tell application "iTunes"
			try
				set playerstate to (get player state)
			end try
			if playerstate = paused then
				set trackPaused to " (paused)"
			else
				set trackPaused to ""
			end if
			if playerstate = stopped then
				return "(stopped)"
			end if
			set trackID to the current track
			set trackName to the name of trackID
			set trackName to "'" & trackName & "'"
		end tell
		set itunesInfo to trackName & trackPaused
		return itunesInfo
	end if
end getTrack

on countMail()
	tell application "System Events" to set mailRunning to (name of processes) contains "Mail"
	if mailRunning then
		tell application "Mail" to set unreadCount to unread count of inbox
		if unreadCount = 0 then
			set unreadCount to ""
		else
			set unreadCount to "(" & unreadCount & ")"
		end if
		return unreadCount
	end if
end countMail

on checkTrash()
	tell application "Finder"
		set trash_size to (do shell script "du -h ~/.Trash | tail -rn1 | awk '{print \"\" $1}'")
		set trash_count to (count (every item of the trash))
	end tell
	
	if trash_count > 0 then
		return "
	
trash x " & trash_count & " (" & trash_size & ")"
	else
		return ""
	end if
end checkTrash

on simpleReplace(search, replace, subject)
	local search, replace, subject, ASTID
	
	set ASTID to AppleScript's text item delimiters
	set AppleScript's text item delimiters to search
	set subject to text items of subject
	
	set AppleScript's text item delimiters to replace
	set subject to "" & subject
	set AppleScript's text item delimiters to ASTID
	
	return subject
end simpleReplace