1

I'm on MacOS 11.2.1 . I have Automator-based AppleScript code for reading text from a file and then "typing" it into the application which is currently active. This works fine, but I need something faster. I have a 2 Meg file of text that I want to send into an app, and it takes a few hours using this Applescript code. Can anyone suggest a way to simulate typing faster than this?

on readFile(unixPath)
    set fhandle to (open for access (POSIX file unixPath))
    set txt to (read fhandle for (get eof fhandle) as «class utf8»)
    close access fhandle
    return txt
end readFile

set texttowrite to readFile("/path/to/file/containing/text")
tell application "[Target Application]" to activate
tell application "System Events"
    repeat with i from 1 to count characters of texttowrite
        keystroke (character i of texttowrite)
    end repeat
end tell

... where [Target Application] is the application into which the text will be "typed".

I need to do this instead of simply a "cp" command, because the target application is a gateway to a service on a different machine. That particular application offers no file transfer capabilities, which is why I need to do this via simulated typing.

Any suggestions about how to do this simulated typing faster?

Thank you in advance.

PS: This slowness has nothing to do with the fact that the target app manages a remote connection. If I use this code to send the data to an application on the same machine on which this AppleScript is running, the "typing" is just as slow.

2
  • 1
    Could you not use the pasteboard to copy and paste the text? – benwiggy Feb 20 at 10:36
  • Unfortunately, the remote application into which I want to send the text does not recognize the clipboard. I have to send keystrokes. – HippoMan Feb 23 at 2:18
0

Read the file, then extract the paragraph
Very simplified...

set theFile to open for access myPath 
set fileContents to (read theFile)
close access theFile
set myParas to count paragraphs of fileContents 
repeat with i from 1 to myParas 
    set myText to myText & "\n" & paragraph i of fileContents
end repeat
  
tell application "app" to activate
tell application "System Events" to keystroke myText 
3
  • Yes, that works a lot faster. Thank you! And when looping through all the paragraphs in the file, I need to do a "keystroke return" between each one that is "typed". – HippoMan Feb 19 at 20:03
  • You can concatenate if you know how many paragraphs. I think you'll still have to for-loop that. I updated the answer - completely untested, you might have to juggle it a bit ;) – Tetsujin Feb 20 at 9:16
  • Yes, after seeing the original version of your app, I wrote my own loop that is very similar to yours. – HippoMan Feb 22 at 21:23
0

This following AppleScript code should serve the same function as you are looking for but the results will be almost instantaneous because you will not have to key stroke each individual letter.

— Save Anything That Is Currently On Your Clipboard
copy (the clipboard) to oldClipboard

-- Choose File Whose Contents You Want To Read
activate
set theFile to POSIX path of (choose file)

—-Copies Contents Of The Chosen File To Your Clipboard
set theText to do shell script ¬
    "pbcopy < " & quoted form of theFile

tell application "Target Application" to activate

—- Pastes Contents Of The Chosen File Into The "Target Application"
tell application "System Events" to keystroke ¬
    "v" using {command down}

-- Restores The Clipboard To Its Original State
delay .1
set the clipboard to oldClipboard

This works fine, but I need something faster. I have a 2 Meg file of text that I want to send into an app, and it takes a few hours using this Applescript code. Can anyone suggest a way to simulate typing faster than this?

I just tried my code using a 6 MB .txt file and it took less than one second.

1
  • That is indeed extremely fast. Unfortunately, the remote application in which I want to paste the text does not recognize the clipboard. I have to paste keystrokes directly. – HippoMan Feb 22 at 21:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .