Use Mac Shortcuts to pass text to a shell script that selects the most recently modified text file in one local folder and appends your entry. This walkthrough uses disposable sample files and stops if the folder is empty or the newest timestamp is tied.

The task here is specific: add a short entry to an existing plain-text file without choosing that file manually each time. Apple documents support for shell scripts inside Mac Shortcuts. The workflow below combines that supported capability with documented zsh file selection and append behavior. Meet Shortcuts for macOS

Decide what “newest file” means

This article defines newest as most recently modified. The script considers only visible, regular files whose names end in lowercase .txt, directly inside the designated folder. It excludes subfolders and symbolic links. These selection rules come from zsh’s filename patterns and qualifiers. Expansion

That definition affects repeated runs. After you append to a file, its modification time changes, so it will generally remain the newest eligible file. If your intention is to follow the latest newly created document, modification time is a different rule; do not assume this workflow implements creation-date selection.

Before building the shortcut, choose the rule that matches your task:

Your intended task Does this example match?
Append to the most recently edited .txt file Yes
Append to the file with the latest date in its name No; filenames are not interpreted as dates
Append to the most recently created file No; creation time is not used
Include files inside subfolders No; selection is limited to one folder
Append to a rich-text document or PDF No; this example writes plain text

Finder provides a useful visual check. Open the folder in list view and click the Date Modified column heading; click again if necessary to put the latest item first. Apple documents both column sorting and reversing the order. Finder’s display helps you inspect the result, but the script performs its own selection. Sort and arrange items in the Finder on Mac

Create a disposable example folder

The example uses a folder named Shortcut Append Demo inside your home folder. The setup deliberately stops if that name already exists, rather than reusing its contents. It creates two synthetic text files, with a two-second interval so that the second file should have a later modification time.

Open Terminal, paste this complete setup block, and press Return. The quoted heredoc runs the commands in a separate zsh process, so a setup failure exits that process.

/bin/zsh <<'ZSH'
emulate -L zsh
setopt ERR_EXIT
folder="$HOME/Shortcut Append Demo"

if ! /bin/mkdir "$folder"; then
  printf '%s\n' 'Setup stopped: use a new folder name if this one exists.' >&2
  exit 1
fi

printf '%s\n' 'Sample entry: blue notebook.' > "$folder/older.txt"
/bin/sleep 2
printf '%s\n' 'Sample entry: green notebook.' > "$folder/newer.txt"
printf 'Example folder: %s\n' "$folder"
ZSH

The setup uses output redirection to create files only after the new folder has been created successfully. Apple distinguishes ordinary output redirection, which writes command output to a file, from append redirection, which adds output at the end. Redirect Terminal input and output on Mac

If setup reports that the folder already exists, choose another disposable name and change the folder assignment in both the setup and the shortcut script below. Do not rerun individual file-writing lines against an existing folder.

Build the shortcut and preserve the text input

Create a new shortcut in the Shortcuts app and name it Append to Newest Text File. Use the action search to add the actions below. Apple documents searching the action library and checking each action’s accepted input and resulting output through Show Info. Navigate the action list in Shortcuts on Mac

  1. Add a Text action containing the synthetic entry Sample entry: orange notebook.
  2. Add Run Shell Script immediately after it.
  3. Set the shell to /bin/zsh.
  4. Set the script’s input to the preceding Text action’s output.
  5. Set Pass Input to as arguments.
  6. Replace the script field with the complete script in the next section.

The intended connection is one Text value into one script argument. The script reads that argument as $1. Its successful output is a text confirmation containing the destination path; that path is not a Shortcuts File object, and the output is not the file’s complete contents.

Keep the input connected to the Text output. Passing a file, a list of filenames, or the shortcut’s unrelated input would change what reaches the script. The script checks the argument count so that an unexpected collection of inputs does not become several appended entries.

If scripts are disabled, open Shortcuts > Settings, select Advanced, and enable Allow Running Scripts. These are Apple’s documented controls for allowing scripts in shortcuts. Apple’s script setting instructions

Add the script that selects and appends

Paste this entire program into Run Shell Script. It needs no downloaded tools or separate script file. The folder assignment matches the disposable setup above.

emulate -L zsh
unsetopt CLOBBER APPEND_CREATE

fail() {
  printf '%s\n' "$1" >&2
  exit 1
}

(( $# == 1 )) || fail 'Expected exactly one text input.'
[[ -n "$1" ]] || fail 'The text input is empty.'

folder="$HOME/Shortcut Append Demo"
[[ -d "$folder" ]] || fail 'The example folder does not exist.'

files=( "$folder"/*.txt(N.om) )
(( ${#files} > 0 )) || fail 'No eligible .txt files were found.'

newest="${files[1]}"

if (( ${#files} > 1 )); then
  [[ "$newest" -nt "${files[2]}" ]] ||
    fail 'The newest timestamp is tied or the folder changed. Check the files.'
fi

[[ -f "$newest" && ! -L "$newest" ]] ||
  fail 'The selected file is no longer an eligible regular file.'
[[ -w "$newest" ]] || fail 'The selected file is not writable.'

if ! printf '\n%s\n' "$1" >> "$newest"; then
  fail 'Append failed; inspect the selected file before retrying.'
fi

printf 'Appended to: %s\n' "$newest"

The selection expression builds an array of matching file paths. In (N.om), N permits an empty match, . restricts matches to regular files, and om sorts by modification time with the youngest first. Keeping paths in an array avoids splitting filenames at spaces. Expansion

The next check requires the first file to be strictly newer than the second. If they have equal modification times, the shortcut stops instead of silently choosing one. zsh documents -nt for comparing file age, along with the regular-file, symbolic-link and writable-file tests used here. Conditional Expressions

The append command uses a fixed printf format and supplies your entry as its %s argument. Percent signs and backslashes in the entry are therefore treated as text rather than additional formatting instructions. The initial newline separates the entry from existing text; the final newline terminates it. If the file already ends with a newline, this produces a blank line between entries. Shell Builtin Commands

The script also unsets CLOBBER and APPEND_CREATE. With both options unset, zsh documents that append redirection fails when the destination does not exist. This matters if the selected file disappears before the append: the script should report a failure rather than create a replacement at the old path. Redirection

Check the result with the sample files

Run the shortcut once. With the untouched sample folder, the expected destination is newer.txt. Open that file and check for the following content. This is the expected result derived from the example, not a claim of a completed Mac UI test.

Sample entry: green notebook.

Sample entry: orange notebook.

Open older.txt as well. It should still contain only Sample entry: blue notebook. Checking both files verifies the two parts of the task: selecting one destination and preserving the other file.

Try a second synthetic entry by changing the Text action to Sample entry: yellow notebook. and running again. Expect another separated entry in newer.txt, provided no other eligible file has become newer. Running the shortcut twice with the same input appends that input twice; this program does not detect duplicate entries.

These examples were inspected against the documentation but were not executed here: the available environment has neither macOS Shortcuts nor zsh. Complete the disposable-file check on your Mac before adapting the folder assignment.

Understand empty folders, collisions and changing files

The workflow requires an existing destination. An absent folder, an empty folder, or a folder containing only ineligible items produces an error before the append. It does not create a default file, choose a subfolder, or fall back to a differently named document.

Name collisions are handled by avoiding destination creation entirely. The setup stops when its folder name already exists. During normal shortcut runs, the selected file keeps its current name: the script neither renames it nor saves a second copy. The separate missing-file behavior is controlled by the documented zsh append options. Redirection

Use this small diagnostic sequence if the result differs from your expectation:

  1. Check the folder assignment. It must match the folder created during setup.
  2. Check eligibility. The sample pattern includes visible regular files ending in lowercase .txt at the folder’s top level.
  3. Check modification order. Inspect Date Modified in Finder, remembering that editing a file can make it the next destination.
  4. Check tied timestamps. Edit and save the intended sample file later, then inspect the order again.
  5. Check the action connection. One text value must reach the shell as one argument.

The script is intended for a folder that stays unchanged during a run. Selection and writing are separate operations: another process could edit, replace or add a file between them. The checks do not turn the workflow into a transaction, and a write failure can leave a partial appended entry. Inspect the file before retrying after an append error.

Where native file filters fit

Apple documents Filter Files as an action that accepts content, narrows it using filters and passes the matching results onward. Its general filter controls can sort results and limit their count. Those operations help select a file; they do not themselves append text. Intro to Find and Filter actions, Add filter parameters

This walkthrough keeps selection and writing together in the shell script so that the file pattern, tie behavior and existing-file requirement are explicit. If you build a different chain using file actions, inspect each action’s Show Info first: a File result, file contents converted to Text and a path represented as Text are different values. Apple provides those action descriptions specifically to identify input and output types. Navigate the action list in Shortcuts on Mac

Once the sample behaves as expected, keep the same clear contract when adapting it: one text entry, one designated local folder and one strictly newest eligible file. That makes each run predictable and gives you a concrete place to investigate whenever the shortcut stops.

Sources