OSX TextEdit save with the first line

1

1

Is it possible to make TextEdit save an unnamed opened file with the first line (or first word) of the text of that document?

eg: I create a new text document in TextEdit; write the first line as I want the file name also to be, lets say:"Test"; I close the document and I get the file saved as "Test.xxx"

thanks

poupou

Posted 2015-06-27T09:16:23.553

Reputation: 13

I solved (in a way) this by using Spark (app for Mac) I recorded my keyboard keys to select the first line and rename (File > Rename). All with a shortcut. I hope it helps someone else.. – poupou – 2015-06-28T10:01:05.937

Answers

0

Solution that uses a shell script or an Automator workflow

Open Automator and create a workflow. When asked to choose a workflow type, choose one that fits your preference.

Drag the template Utilities » Run Shell Script into the empty workflow; this opens a large text field. Paste the following code into the text field:

#!/bin/bash
EXTENSION=xxx
SCRIPT_NAME='Create a file whose first line determines the file name'

TMP_FILE="$(mktemp -dt "create_file_whose_first_line_determines_file_name.XXXXXXXXXX")/New file whose first line determines the file name.txt"
echo 'untitled.txt' > "${TMP_FILE}" && open -neW "${TMP_FILE}"

TARGET_FILE="$(head -n 1 "${TMP_FILE}").${EXTENSION}"
if tail -n +2 "${TMP_FILE}" >> "${TARGET_FILE}"
then
  osascript -e "display notification \"${TARGET_FILE}\" with title \"File created\""
  open -R "${TARGET_FILE}"
else
  osascript -e "display notification \"${TARGET_FILE}\" with title \"Unable to create file\""
  open -R "${TMP_FILE}"
fi

(Note: Change EXTENSION=xxx to whatever extension you need.)

Save your workflow to any location that is convenient. Run the workflow without arguments; it will open a temporary file in a new TextEdit instance and wait for you to save and close TextEdit. The script will then create the file as per your requirements.

If successful, the script will reveal the newly created file in the Finder.

If the script is unable to create a file, it will reveal the temporary file in the Finder.

Synoli

Posted 2015-06-27T09:16:23.553

Reputation: 101

PS. The script doesn’t need Automator to run. Just save the script with a .command extension. It can then be launched directly from the Finder. – Synoli – 2015-06-29T17:16:04.537

Thanks Synoli! I'm having issues with Automator: it asks to put another action "take specified elements from finder" paraphrasing (I'm on another language). I add and run it. It opens a new doc in TextEdit with the content: "untitled.txt" and the filename: "New file whose first line determines the file name.txt" I try to edit the content to see what it does and then close the document, but it closes and I can't find. As for the .command I tried that but it says that I don't have the privileges to run it. I hope it makes sense. Thank you very much the help – poupou – 2015-06-30T10:22:37.577