problem with sub routines

I have a sub-routine:


on getZeroFormattedNumber from numberToFormat
	if numberToFormat is less than 10 then
		return "0" & (numberToFormat as string)
	else
		return numberToFormat
	end if
end getZeroFormattedNumber

However, when I call it:


set creationDate to creation date of sourceFile
set creationMonth to getZeroFormattedNumber from (month of creationDate as integer)

I get an error saying: “Finder got an error: can’t continue getZeroFormattedNumber”

Why is this error happening and how do I avoid it?

Thanks!

Hi,

this is a very common error.
You are calling the subroutine from within an application tell block, and the Finder doesn’t know, what a handler is.
In this case put the keyword my in front of the handler call (with your syntax AppleScript will change it to subroutine of me).

By the way: your handler returns a string if the number is less than 10 otherwise an integer.
A formatting handler should always return the same class to avoid problems.
This is an easier version of the handler

on getZeroFormattedNumber from numberToFormat
	return text -2 thru -1 of ("0" & numberToFormat as text)
end getZeroFormattedNumber

Thanks Stefan!

I tried this:


		set creationMonth to getZeroFormattedNumber from month of creationDate of me

however, this yielded in the same error as before. If I put me in front like so:


		set creationMonth to me getZeroFormattedNumber from month of creationDate

there’s a compile error.

Can you clarify your guidance?

sorry, it’s my fault.
my not me

It’s either

my getZeroFormattedNumber

or

getZeroFormattedNumber of me

Thanks for the suggestions Stefan!

I tried this:


set creationMonth to my getZeroFormattedNumber from month of creationDate

and it was converted to


set creationMonth to getZeroFormattedNumber of me from month of creationDate

:wink: