Search code examples
macosshellautomationapplescriptautomator

Add text to text document via automator


I automated a lot of my tasks to save time with help of Hazel or Automator.

However I am super stuck right now with a simple problem: I can't figure out how to add text to an existing text document via Automator.

So far I tried to figure this out with various shell / apple scripts that I adapted from sources from here or other forums. – None of them were working so far, that's why I am hoping to get some help here.

Specifically this would be the ideal solution:

  1. Automator copies an existing text template to a pre-defined location.
  2. I am prompted to insert/paste some text into a field.
  3. Automator pastes this text on top of the text file that just has been copied to the new location.

Everything worked so far except the third step.

Here's the automator workflow. I disabled the Apple Scripts down there, because they were just copy-pasted from other forums and not working. My Automator workflow

Thanks so much. Looking forward to hearing your ideas.


Solution

  • There are probably a handful of ways to approach this but here is one that is relatively straightforward and depends upon neither applescript nor variables. It does require a destination folder ahead of time (which is where your template will be copied to).

    • Get Specified Finder Items (from folder A, i.e. folder containing your original template)

    • Get Folder Contents

    • Filter Finder Items

    • Copy Finder Items (to folder B, i.e. destination folder, check Replacing…)

    • Open Finder Items (Open with: TextEdit — opens copied template document — this will become the finished document)

    • Store Disk Item References (store copied template file reference)

    • Ask for Text (while testing, I used 'get specified text' instead but I guess that's irrelevant now)

    • New Text File (contents are your promo blurb)

    • Retrieve Disk Item References (gets promo blurb file reference AND copied template file reference)

    • Combine Text Files (prepends promo blurb to template text)

    • Set Contents of Textedit Document (By: Replacing — Replaces copied template document's text with new combined text)

    • Retrieve Disk Item References (gets file reference of updated copied template)

    • Rename Finder Items: Name Single Item (to whatever)

    • Quit Application (TextEdit, check Ask to save changes)

    In essence, the workflow puts the text from your template and your promo blurb into textedit documents and grabs each text and substitutes the combined text. When the workflow completes its run, you should have a text file containing both texts with the promo blurb at the top. If you would like to choose where to save the text file, remove the last three actions and you will be left with the open TextEdit document containing the combined text.