Applescript: Read numbers from text file and post in Safari/Chrome app

Hi

Newbie to AppleScript here but basic programming knowledge.

Many I please ask for some help to use AppleScript to:

  1. Open a local text file (containing a list of numbers (1-9999)…up to a thousand numbers)
    eg /users/aw/documents/automate/data.txt

eg:

37
53
207
4087

(Could use a csv or any other delimiter if it’s easier)

  1. Read the first number from the file
  2. Enter that number into a text input box in a web app through Safari (or Chrome if recommended) (either a keystroke or possibly paste).
  3. Press Return in Safari
  4. Repeat until end of file.

Hopefully this is a pretty basic ‘read … keystroke/paste’. Don’t need anything too fancy.

Any help much appreciated !

Thanks

Adam

:slight_smile:

Model: iMac 3.4 GHz Quad-Core Intel Core i7, 8Gb RAM
AppleScript: Applescript 2.7, Script Editor 2.11
Browser: Safari 605.1.15
Operating System: macOS 10.14

Nothing fancy, just a question missing a lot of useful informations.

-- Define a path to a text file containing a list of numbers separated by linefeed or return character
set sourceFile to ((path to desktop as string) & "myNumbers.txt") as «class furl»
set theNumbers to paragraphs of sourceFile
repeat with aNumber in theNumbers
	tell application "System Events" to tell process "Safari"
		set frontmost to true
		-- Now I make assumptions about the content of the web page
		tell window 1 to tell scroll area 1
			set value of text field 3 to aNumber
			keystroke return
			-- or click button (the button which switch to the page supposed to receive an other number
		end tell -- window 1
	end tell -- System Events
end repeat

(*
Maybe there is no scroll area, maybe there is an other level in the hierarchy of the GUI.
Maybe the text field is not numbered 3.
Maybe it's not a text field but a combo box.
Maybe the button allowing to change of page has the shortcut <return>.
Maybe it hasn't.
Maybe passing to the field supposed to receive the next number requiores more actions.

I apologize, I'm not a sooth sayer.
*)

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mardi 5 mai 2020 21:14:11

Hi Yvan

Thank you very very much.

It turned out to be quite simple. I didn’t need to control the target screen in any way, because I can set the cursor manually before running.

The final solution:

set sourceFile to “/testrun.txt”
set theNumbers to paragraphs of (read sourceFile)
repeat with aNumber in theNumbers

tell application "System Events" to tell process "Safari"
	set frontmost to true
	-- focus already manually set to the correct box on page
	keystroke aNumber
	keystroke return
	delay 1
	-- handle any invalid entry below
	key code 51
	key code 51
	key code 51
	key code 51
	delay 2
	-- or click button (the button which switch to the page supposed to receive an other number
	
	
end tell -- System Events

end repeat

It is a ‘quick and dirty’ solution … but will save literally hundreds of hours of work !

Many many thanks

Adam

:D:D:D