String variable trouble! [Handler; scope]

I have a string variable that is declared in the first line of my program, and then it is set to something in function like this:

set procTime to "0:00.79" as string

getProcessTime("iTunes", procTime)

display dialog procTime -- ****

on getProcessTime(proc, procTime)
	try
		tell application "System Events" to set pid to the unix id of process proc as string
		set procTime to paragraph 2 of (do shell script "ps -p " & quoted form of pid & " | awk '{ print $4 }'") as string
		display dialog procTime -- ****
	end try
end getProcessTime

The problem is that in the second “display dialog”, I am getting the string I want, but in the first one it is always “0:00.79” like when it is initiated. I’ve tried changing the first line to just “set procTime to string” but then the first “display dialog” gives me the answer of “TEXT”

Anyone know why this variable would be doing that?

Thanks!

Please create your posts in the right area. Moving to OS X…

Side note: Both of those values are already strings, so they don’t need to be coerced to strings. (Actually, do shell script should be returning Unicode text, which doesn’t cleanly coerce to text [p55375].)

You set that variable to “0:00.79” on the first line of your script, which is working correctly. A variable like that is not modified by a variable with the same name in a handler.

Try something like this:

set procTime to getProcessTime("iTunes")

display dialog procTime


on getProcessTime(proc)
	try
		tell application "System Events" to set pid to the unix id of process proc as Unicode text
		return paragraph 2 of (do shell script "/bin/ps -p " & quoted form of pid & " | /usr/bin/awk '{ print $4 }'")
	end try
end getProcessTime

See also: Handlers