简体   繁体   中英

Unable to manipulate text from Clipboard in Applescript

I am forced to use a piece of software that requires a lot of data entry into simple web forms, so I have designed an AppleScript to take a text file as input and then keystroke out the contents into Safari, so that tabs in the text file are used to advance to the next field in the web form. I am doing some string replacement so I can treat newlines in the text file as tabs, for readability of the text file.

I want to streamline this by keystroking from the clipboard, which works, but the string replacement is not happening the way it should, and I am unable to determine why. My workaround at this point is to manually find/replace newlines with tabs before copying, but that's basically just as slow as saving the file and pointing the script at it.

tell application "Safari"
    activate
end tell

set sourceString to (the clipboard) as text

set ASTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to "\n"
set the lineList to every text item of sourceString
set AppleScript's text item delimiters to "\t"
set keystrokeString to the lineList as string
set AppleScript's text item delimiters to ASTID

tell application "System Events"
    keystroke keystrokeString as text
end tell

\\t and \\n will compile down to invisibles if you put this into AppleScript Editor. The string editing (middle) part works as advertised when I read from a file using:

set theFile to (choose file with prompt "Select a file to read:" of type {"txt"})
open for access theFile
set fileContents to (read theFile)
close access theFile

Any ideas why the string replacement might not be working when I get the text from the clipboard?

Using the ASCII Character function when setting the delimiters should work. A linefeed is ASCII character 10. So ie: set AppleScript's text item delimiters to ASCII character 10 Should work.

Tabbing through forms on a page is often error prone. You should identify each form by id and populate the form with javascript.

tell application "Safari"
    set URL of document 1 to "http://www.google.com/"
    delay 3
    do JavaScript "document.getElementById('gbqfq').value = 'My Text'" in document 1
end tell

It appears as though linefeeds are translated into carriage returns upon copying text to the clipboard, and \\n will not match them.

\\r will, however, and they both compile down to invisible characters, which is a bit of an annoyance.

After a little bit of testing, it looks like when you save a text file to disk and then read it with AppleScript, you must match the line endings used in the file, LF or CR with \\n or \\r . However, when reading text copied to the clipboard, you must always match \\r .

Edit:

Many of these characters have AppleScript keywords. tab , linefeed , and return can all be used as is. My script now looks like this:

set backspace to ASCII character 8

get the clipboard
set keystrokeString to (replacement of return by tab for the result)
set keystrokeString to (replacement of linefeed by tab for the result)
set keystrokeString to (replacement of tab by tab & backspace for the result)

tell application "System Events"
    keystroke keystrokeString as text
end tell

on replacement of oldDelim by newDelim for sourceString
    set oldTIDs to text item delimiters of AppleScript
    set text item delimiters of AppleScript to oldDelim
    set strtoks to text items of sourceString
    set text item delimiters of AppleScript to newDelim
    set joinedString to strtoks as string
    set text item delimiters of AppleScript to oldTIDs
    joinedString
end replacement

It also now includes backspace characters that are hit after every tab, so if text is not entered into an already filled field, it is deleted after being advanced to.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM