A very simple script that doesn’t work no matter how I rework it… I suspect I’m misunderstanding how “repeat until” works, but I was hoping you champions would be able to help out a novice.
This script is designed to move down a simple text document and check whether the line begins with a number or not (it’s part of a larger script, but this is the part I can’t get to work).
tell application "System Events"
delay 5
set variable to ""
repeat until variable is 0
key code 124 using shift down
delay 1
keystroke "c" using command down
delay 1
set variable to the clipboard
key code 125
delay 1
end repeat
end tell
The script would move down something like this (with the cursor sitting before the H of Home):
Home
Town
City
Area
002
Bacon
Eggs
Cheese
003
…etc
What I expect to happen using the above script is for it to stop when it reaches 002. This doesn’t happen though, and instead the script carries on indefinitely. It also doesn’t work if I change “repeat until variable is number”.
Amazing!!! Thank you so much. This leads on to another problem:
Let’s say I have no way of knowing whether the number will start with “0” or “3” or “9” etc. etc.
The novice that I am instinct is to the following:
tell application "System Events"
delay 5
set variable to ""
repeat until variable starts with "0" or "1" or "2" or "3" or "4" or "5" or "6" or "7" or "8" or "9"
key code 124 using shift down
delay 1
keystroke "c" using command down
delay 1
set variable to the clipboard
key code 125
delay 1
end repeat
end tell
I’m sure you’ll see instantly that this does not work, giving me the error “Cannot make “1” into type boolean”. I hope you can see what I am going for and offer me further guidance.
then I recommend to use a infinite repeat loop and leave the loop when the variable can be coerced to integer
tell application "System Events"
delay 5
repeat
key code 124 using shift down
delay 1
keystroke "c" using command down
delay 1
set variable to the clipboard
key code 125
delay 1
try
variable as integer
exit repeat -- will be skipped if variable cannot be coerced to integer
end try
end repeat
end tell
tell application "System Events"
delay 5
set variable to ""
repeat until character 1 of variable is in {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"}
key code 124 using shift down
delay 1
keystroke "c" using command down
delay 1
set variable to the clipboard
key code 125
delay 1
end repeat
end tell
Yvan KOENIG (VALLAURIS, France) lundi 27 octobre 2014 14:16:04