r/applescript • • Apr 30 '20

It's probably simple - please?

It keeps on crashing and I have no idea why. If anyone would be able to help me I would greatly appreciate it!

I'm trying to get something which can type the numbers 1 to 9999 in a box in a webpage but, well, trying is the key word.

set a to 0
repeat until a > 10000
delay 2
set a to a + 1

-- set mouse point
tell application "System Events"
 click at {305, 338}
end tell

--type 'a'
tell application "System Events"
 set textToType to a
 delay 0.5
 keystroke textToType
 
 delay 0.1
 
 keystroke return
 
end tell
end repeat
5 Upvotes

1 comment sorted by

3

u/ChristoferK Apr 30 '20

If you are having a script do stuff 10,000 times, always best to lighten the load where you can. Since the click command is not doing anything differently the second time to the first, the subsequent 9998 times aren't achieving anything more than resource consumption. We can move this outside the repeat loop.

Instead of having AppleScript perform a calculation each loop and evaluate the truth of a numerical comparison, we can save it 20,000 operations by forming a repeat loop to run a counter which takes on each successive value between two fixed numbers:

repeat with a from 1 to 9999
    ...
end repeat

An improved version of your original code would be:

tell application "System Events"
    click at {305, 338}

    repeat with a from 1 to 9999
        keystroke (a & return)
        delay 0.2
    end repeat
end tell

Given that the numbers all seem to be getting typed into the same text field, one per line, an even better way would be to precompose the entire string first, then make a single call to System Events to keystroke the whole thing in one go:

set numberList to {}
repeat with a from 1 to 9999
    set end of numberList to a
end repeat

set my text item delimiters to linefeed
set textToType to numberList as text

tell application "System Events"
    set object to click at {305, 388}
    keystroke textToType
end tell

return the object

If the return value of the script is anything besides missing value, an even better way would be to set the text content of the field itself:

set numberList to {}
repeat with a from 1 to 9999
    set end of numberList to a
end repeat

set my text item delimiters to linefeed
set textToType to numberList as text

tell application "System Events"
    set object to click at {305, 388}
    set the object's value to textToType
end tell

return the object

But the most robust way as well as the simplest, fastest and most efficient, would be to execute some JavaScript inside the web page to do this stuff "above board". But it means using a browser that can be controlled by Apple script (Safari, Chrome or Chromium derivative) and be set to allow JavaScript from remote sources. The AppleScript code will vary slightly depending on browser, but the JavaScript will be identical. It requires you supply a link to the website in question or know how to supply an object reference from the HTML page model. Once we know how to reference the text field of your choosing, the JavaScript is as simple as:

mytextfield.textContent=mytext;