📝Renaming Noteplan files with the correct titles

A great feature in Noteplan is that the filename of the note gets the filename that is equal to the title of the note

What is not so great, as a note evolves and changes and maybe your title gets changed, the filename does not. Which can be a hassle if you do a files search on your disk to find a particular file. I recently learned that you can work together with Obsidian. But for this to work correctly, the files need to have nice describing names.

I don’t use Obsidian, but I like to have my filenames aligned with the notes’ title. Therefore, I wrote the bash script below.

You can set your directories to be scanned, so that the files within them get renamed if needed.

#!/bin/bash

# Directories to process
directories=(
  "/Users/<user>/Library/Containers/co.noteplan.NotePlan-setapp/Data/Library/Application Support/co.noteplan.NotePlan-setapp/Notes/personal"
  "/Users/<user>/Library/Containers/co.noteplan.NotePlan-setapp/Data/Library/Application Support/co.noteplan.NotePlan-setapp/Notes/work"
)

# Loop through directories and *.txt files
for root_dir in "${directories[@]}"; do
  echo
  echo
  echo "Processing directory: $root_dir"
  # Find and process *.txt files
  find "$root_dir" -name "*.txt" -type f -print0 | while IFS= read -r -d '' file; do

  title=$(head -n 1 "$file" | tr -cd '[:alnum:][:space:]._-' | xargs)
    path=$(dirname "$file")
    new_filename="${path}/${title}.txt"
    
    if [[ "$file" != "$new_filename" ]]; then
      mv "$file" "$new_filename"
      echo "File renamed to: $new_filename"
    else
      echo "File already has correct title: $new_filename"
    fi
  done
done

I have Noteplan via Setapp, so I included the Setapp paths to be used. Set <user> to your username and mutate the path, so it resembled your folder structure in Noteplan.