Saturday, July 4, 2020

#1 2020-05-05 12:34:40 pm

adam_w
Member
Registered: 2020-05-04
Posts: 2

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)

2. Read the first number from the file
3. Enter that number into a text input box in a web app through Safari (or Chrome if recommended) (either a keystroke or possibly paste).
4. Press Return in  Safari
5. 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

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

Offline

 

#2 2020-05-05 01:14:53 pm

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 4533

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

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

Applescript:

-- 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

Offline

 

#3 2020-05-07 09:03:51 am

adam_w
Member
Registered: 2020-05-04
Posts: 2

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

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 "<path>/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


big_smile:D:D

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)