Often, in examples in my books encounter: i in a script. As in this example:
set this_password to “Frogger” considering case
repeat with i from 1 to 3
display dialog (“Enter password:”) ¬
default answer “” with hidden answer
if the text returned of the result is this_password then exit repeat
end if
if i is 3 then return “access denied” end repeat
end considering
“i” is just the name of a variable that will be used for the repeat. You can choose any valid variable name you like for that. In some situations, choosing a name might make it clearer.
set theColors to {"red", "orange", "yellow"}
repeat with aColor in theColors
display dialog aColor
end repeat
Here I’m using “aColor” where the other script used “i”
The “repeat” goes through each element of the list “theColor” and automatically substitutes one of the list elements in for the variable “aColor” one by one.
Running my sample script should make the behavior of the repeat variable (“aColor” in this case) clear.
Also, please use the “Applescript” tags at the top of the editor window for the code you post.
It was originally a mathematical convention, but I gather in programming it started with Fortran, where the letters i…n were reserved for the names of integers.
That said, and I might be off in my history here, but Algol was a contemporary of Fortan, which also used i, as can be seen in the example code in the wikipedia article here:
set this_password to "Frogger"
repeat with i from 1 to 3
display dialog ("Enter password:") default answer "" with hidden answer
considering case
if the text returned of the result is this_password then exit repeat
end considering
if i is 3 then return "access denied"
end repeat
repeat loop changes the value of variable i step by step: 1,2,3
end repeat in Applescript is equivalent to 2 instructions of some other languages (as Fortran, Basic,…) :
increment i by 1 (i=i+1)
go to begin of loop if i<3 (here, begin is display dialog code line)
Now, still wandering how to use this: [apple script] [/apple script] when i want to paste a script ?
Same thing for:
Is it explained somewhere ? What is the difference between the one with / ?
Thanks again !
Pascal
Waiting, this one does not compile:
set dialog_reply to display dialog “How many minutes?” default answer “120”
set total_time_in_minutes to text returned of dialog_reply as integer
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}
end tell
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 10 times
beep
delay 0.5
end repeat
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
You have 1 unneeded end tell and 2 wrong characters for “minus” in the repeat with total_minutes… code line. And need if the_number < 10 then. This is correct:
set total_time_in_minutes to ¬
text returned of (display dialog "How many minutes?" default answer "120") as integer
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, 300}
end tell
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
repeat 10 times
beep
delay 0.5
end repeat
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
NOTE: when you edit post, select in this edit window the text, which you want wrap with some tag. Then, click the appropriate tag button on the top of edit window. For example, tag “applescript”. The webpage will automatically add tags for you. “applescript” marks the begin of script, and “/applescript” marks its end. The same is with “quote” tags.
IMHO, the biggest thing to learn about programming is debugging.
When you try to compile what you posted, Script Editor jumps to that first incorrect “–” sign and highlights it. Once you fix it, it jumps to the next one. Once you fix that, it jumps to highlighting the “end tell” - which should prompt you to look back and see if you’re still inside a “tell” to end. Once you fix that it highlights the number “10,” letting you know there’s an error around there.
While attempting to compile doesn’t tell you exactly what’s wrong or how to fix it, it shows you where the problem is, and which point you should be exploring fixes yourself.
I can see how the wrong character for minus sign might be confusing, because what you see visually on screen appears to be correct. But the extra “end tell,” and the missing “>,” once highlighted, should be easy to diagnose, especially when you’ve got a book with printed, correct code to compare the highlighted section to.
The script is a copy/paste from the book. So, i assume there should not be an error and should compile. That is why i posted the script. Since i am learning apple scripting since only 2 or 3 weeks,
i do not have the skill (yet) to debug it. Although i have already noticed that the script editor point toward the first problem it encounters trying to compile.