1

I'm very new to programming so pardon me if this is a very simple question, but I'm trying to create an automator workflow where the variable would change with each loop, pulling the inputs from a textedit document.

(and also, I'm creating the text edit document, so if formatting it differently would make this easier let me know)

For example, I have a text edit document with several numbers, all 3 digits, separated by line breaks, like this:

001
005
009
013
014
021

I'd like automator to run a workflow the first time with the variable as "001", then again with variable as "005", then a 3rd time as "009" and so on. And if it makes things easier I can format it to be all on one line separated by commas, surrounded by brackets, whatever is best.

I know a little bit of javascript and AppleScript, so if this is something solved with a run Javascript or AppleScript box, that is totally cool. Thanks!

4
  • 1
    Automator might not be the best tool for this. What do you want to do with each line afterwards?
    – nohillside
    Jul 16 '21 at 6:59
  • I know this is clunky but I plan to build a workflow of key-presses. Probably something like Ctrl + F, typetext "001", press enter, Ctrl+X, Ctrl+V, loop. Jul 16 '21 at 7:05
  • Automator can't do key presses, so AppleScript might be better
    – nohillside
    Jul 16 '21 at 7:18
  • Yeah, when I need key presses I tell automator to 'run AppleScript" or "run Javascript". So if there's a solution that involves one of them, that would also work for me. Appreciate it! Jul 16 '21 at 7:20
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.

You must log in to answer this question.

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