4

How to wrap a text selection to a certain column width using AppleScript?

For e.g., let's say the following text is selected:

AppleScript is a scripting language created by Apple Inc. that facilitates automated control over scriptable Mac applications. First introduced in System 7, it is currently included in all versions of macOS as part of a package of system automation tools.

I'd like to run AppleScript (prefereably via some keyboard shortcut) on this selected text, which should wrap the lines to a specific width by inserting line breaks at the specific width. The width can be hard-coded in the script itself.

For e.g., if the width is 72, then, the above text should be changed to:

AppleScript is a scripting language created by Apple Inc. that
facilitates automated control over scriptable Mac applications. First
introduced in System 7, it is currently included in all versions of
macOS as part of a package of system automation tools.

Formatting the text to 72-chars is doable using fmt utilty as fmt -w 72. So, the individual steps would be:

  1. Get the selected text into AppleScript
  2. Run fmt -w 72 on the text
  3. Replace the selected text with output of fmt

My primary use case is to wrap the emails that I compose to 72-char width on Apple Mail on macOS. Any other ideas (outside of AppleScript) to achive this goal are welcome too.

Thanks!

5
  • 1
    Why, please? What's special about 72-char width? Sep 25 at 20:36
  • @RobbieGoodwin, 72-column wrapping has evolved as a de-facto standard for text-only email. Wrapping on the sending side ensures that the receiver doesn't need to re-flow the text, possibly breaking quotations.
    – Mark
    Sep 26 at 3:28
  • Are you really saying 72-column wrapping has evolved as a de-facto standard for text-only email, so long as it's not composed in AppleScript? Doesn't wrapping on the sending side ensure rather that the receiver can't easily re-flow the text, for whatever reason? Sep 26 at 19:38
  • Yes, 72-char width is recommended for plain text emails. I'd rather have the email client deal with re-flow (both on sender and receiver), but Apple Mail doesn't do that (for e.g., I don't know a way to configure to reflow the incoming/sent emails with a 72-char width).
    – bfxfer
    Sep 27 at 6:59
  • FYI, to run a shell script from within applescript, use do shell script "echo " & srcTxt & " | fmt -w 72 ", assuming that srcTxt is your string to break. In the terminal, it would look like this: echo AppleScript is a… | fmt -w 72. By the way… you could probably add a keyboard shortcut to your service. And, you can accept your own answer although you probably won't gain any rep for it. But others may find the solution more easily if it is accepted.
    – Mockman
    Sep 30 at 2:58

1 Answer 1

4

After a bit of searching around, the following seems to work. This was on macOS 13.5.2 and Automator 2.10 (521.1).

  1. Open Automator
  2. File -> New -> Quick Action
  3. Search for "shell script" in the search box on left top with "Action" selected
  4. Drag "Run Shell Script" action in to the workflow area on the right, and:
    • fmt -w 72 in the shell script contents
    • Pass input: stdin
  5. Select the following in the top area of the workflow area
    • Wokflow receives current in
    • Check "Output replaces selected text"
  6. File -> Save -> Use any suitable name (I used WrapText)

See attached screenshot.

Now, select any text in any application (TextEdit, Mail, etc.,), right click, Services -> WrapText will wrap the text at 72 characters. See attached screen recording.

Automator Screenshot WrapText Demo

You must log in to answer this question.

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