Script refuses to work

Hi all !!!
Novice here…
Can someone tell me why this script do not work ?
Thanks a lot !

on pad_with_zero(the_number)
if the_number = 10 then
return “0” & the_number
else
return the_number as text
end if
end pad_with_zero

set dialog_reply to display dialog “How many minutes?” default answer “120” as integer
set total_time_in_minutes to text returned of dialog_reply

tell application “TextEdit”
activate
set timer_doc to make new document with properties {text:“00:00:00”}
set size of text of timer_doc to 60
set bounds of window 1 to {100, 100, 400, 250}
repeat with total_minutes_left from (total_time_in_minutes - 1) to 0 by -1
set hours_left to total_minutes_left div 60
set minutes_left to total_minutes_left mod 60
repeat with seconds_left from 59 to 0 by -1
set formatted_time to pad_with_zero(hours_left) & “:” & pad_with_zero(minutes_left) & “:” & pad_with_zero(seconds_left)
set paragraph 1 of text of timer_doc to formatted_time
delay 1
end repeat
end repeat
end tell

repeat 1 times
beep
delay 0.5
end repeat

First problem was that the script was calling its pad_with_zero handler from inside the tell application Text Edit block. That meant the handler call was being sent to Text Edit. If you put the word “my” before each handler call, it gets sent to the script itself.

Second problem, in the pad_with_zero handler it was only adding a zero if the number equaled 10. It should be < 10 (less than).

Third, when you post a script here, the system is set up so that if you put the three:
`

(to the left of the number 1 on most keyboards) followed by the word applescript (all lowercase) and return, before the script text and three more ` after the script, it will appear in a very readable format and give readers a button to open the script in their script editor.

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

on pad_with_zero(the_number)
   if the_number < 10 then
      return "0" & the_number
   else
      return the_number as text
   end if
end pad_with_zero

set dialog_reply to display dialog "How many minutes?" default answer "120" as integer
set total_time_in_minutes to text returned of dialog_reply

tell application "TextEdit"
   activate
   set timer_doc to make new document with properties {text:"00:00:00"}
   set size of text of timer_doc to 60
   set bounds of window 1 to {100, 100, 400, 250}
   repeat with total_minutes_left from (total_time_in_minutes - 1) to 0 by -1
      set hours_left to total_minutes_left div 60
      set minutes_left to total_minutes_left mod 60
      repeat with seconds_left from 59 to 0 by -1
         set formatted_time to my pad_with_zero(hours_left) & ":" & my pad_with_zero(minutes_left) & ":" & my pad_with_zero(seconds_left)
         set paragraph 1 of text of timer_doc to formatted_time
         delay 1
      end repeat
   end repeat
end tell

repeat 1 times
   beep
   delay 0.5
end repeat

Thanks for the reply Estocly !!!
Does not pad_with_zero handler external to the tell blok since it is written before it at the top?

It doesn’t matter if the handler is top or bottom. The issue is that the calls to the handler are inside the tell block, so the call is being sent to the application. The application doesn’t handle that and trips an error.

By adding the word “my” before each handler call it sends the call directly to the script.

I would also suggest to be explicit when casting values to a string as you would when concatenating a string and a number.

So instead of:

return "0" & the_number

Do:

return "0" & (the_number as text)

So what determines when a handler is inside or outside a tell block ?

It’s not the handler that’s inside the tell block, it’s the call to the handler.

Everything between these two lines is inside the tell block:

tell application "TextEdit"

end tell

When your script says:

 set formatted_time to pad_with_zero(hours_left)

That won’t work because it’s inside the tell block. Your script is telling the application:
pad_with_zero(hours_left)

But the application doesn’t recognize that.

if you change that to:
my pad_with_zero(hours_left)

The script will send the command back to itself, and the script knows what to do with it.

I think i got it !

Thanks

1 Like