Droplet for How Long an App has been Running

The Robservatory blog featured a method for finding out how long an app has been running. Here’s the source for an AppleScript application that lets you drag and drop your running app’s icon to see that information. Put this script into an AppleScript Editor (currently known as “Script Editor“) document, then save it as an application. Don’t copy the line numbers. Be sure your app is running, then drag that app’s icon onto the new app you created.

Warning: If the running process of the app doesn’t have the same name as the app’s icon (excluding the “.app” extension, of course), the droplet won’t work.

Previously, I discovered a problem with some apps with longer names, and provided a revised version. Now I’ve added the ability to select an app icon in the Finder and launch this app from the Dock or the Script menu for those of you who might prefer not to drag your app icons.

on open the_item
	my build_answer(the_item)
end open

on run
	tell application "Finder"
		--activate
		set the_item to get selection as string
	end tell
	set the_item to get the_item as alias
	my build_answer(the_item)
end run

on build_answer(the_item)
	--	tell me to activate
	set item_info to get info for the_item
	set _choice to characters 1 thru -5 of (displayed name of item_info) as string
	try
		set _pids to paragraphs of (do shell script "/bin/ps -acxwwl")
		set ls to {}
		set ps to _choice
		repeat with _line in _pids
			if _line is "" or _line contains "TIME COMMAND" then
			else
				set _pid to 2nd word of _line
				set s to last text item of _line
				set n to seventh word of _line
				if s is "t" then set s to "init"
				tid("     ")
				set _process to s
				set _ful to _pid & tab & n & tab & _process
				if _ful ends with ps then
					set end of ls to (_pid & tab & n & tab & _process) as string
				end if
			end if
		end repeat
		tid(tab)
		set _pit to item 1 of ls
		set _pid to word 1 of _pit
		set _uptime to (do shell script "ps -o etime= -p" & space & quoted form of _pid)
		set time_val to count words of _uptime
		set _secs to word time_val of _uptime
		set _mins to word (time_val - 1) of _uptime
		if (time_val - 2) > 0 then
			set _hours_string to ((time_val - 2) as string) & space & "hour(s)," & space
		else
			set _hours_string to ""
		end if
		if (time_val - 3) > 0 then
			set _days_string to ((time_val - 3) as string) & space & "day(s)," & space
		else
			set _days_string to ""
		end if
		display dialog "The" & space & _choice & space & "app has been running for" & space & _days_string & _hours_string & _mins & space & "minute(s)" & space & "and" & space & _secs & space & "second(s)." buttons {"OK"} default button 1
	on error
		display dialog "Something went wrong. Are you sure the app of the icon you dropped is really running?." buttons {"OK"} default button 1
	end try
end build_answer

on tid(s)
	set my text item delimiters to s
end tid

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s