Parsing text, "line by line", with automator

Solution 1:

Since it is not totally clear what is the full scope of what you are trying to accomplish, the following example AppleScript code is meant as a proof of concept.

With a plain text document name Filename.txt in the Documents folder containing:

001
005
009
013
014
021

With having no linefeed after the last line, here is something you might be able to adapt to achieve your goal.

The example AppleScript code, shown below, was tested in Script Editor under macOS Catalina with Language & Region settings in System Preferences set to English (US) — Primary and worked for me without issue1.

  • 1 Assumes necessary and appropriate settings in System Preferences > Security & Privacy > Privacy have been set/addressed as needed.


Example AppleScript code

set theFile to ¬
    the POSIX path of ¬
        (((path to documents folder) ¬
            as string) & "Filename.txt")

set myList to ¬
    paragraphs of (read theFile)

tell application "TextEdit"
    activate
    make new document
    delay 2
end tell

tell application "System Events"
    repeat with thisItem in myList
        keystroke thisItem
        key code 36 --  # Enter key
    end repeat
end tell

Produces the following:

enter image description here

Looking in the Results pane of the Script Editor window, it shows:

tell current application
    path to documents folder
        --> alias "Macintosh HD:Users:me:Documents:"
    read "/Users/me/Documents/Filename.txt"
        --> "001
005
009
013
014
021"
end tell
tell application "TextEdit"
    activate
    make new document
        --> document "Untitled"
end tell
tell application "System Events"
    keystroke "001"
    key code 36
    keystroke "005"
    key code 36
    keystroke "009"
    key code 36
    keystroke "013"
    key code 36
    keystroke "014"
    key code 36
    keystroke "021"
    key code 36
end tell


Note: The example AppleScript code is just that and sans any included error handling does not contain any additional error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors. Additionally, the use of the delay command may be necessary between events where appropriate, e.g. delay 0.5, with the value of the delay set appropriately.