2017-09-13

Replace Text With A TimeStamp - Make A macOS Service W/ AppleScript

I borrowed some work from this guy's page.

This short, simple script:

do shell script "date +%Y-%m-%d\\ %H:%M:%S\\ %z"

will replace whatever text you highlight, with this:

2017-09-13 15:27:41 -700

(That's just an example, of course)

  • I don't want the "20" from "2017" so I'll change the %Y to %y
  • I don't want dashes
  • I don't want colons
  • I don't want spaces
  • I don't want seconds
  • I don't want zone

My script ends up like this:

do shell script "date +%y%m%d%H%M"

and my replacement output is now:

1709131527

(Optionally, I've created an additional service that includes the seconds, useful for rapid renaming of multiple photos or other files I like to store chronologically/sequentially.)

Assign a keyboard shortcut by turning it into a Service in Automator.
  1. Open Automator.app
  2. Create a New > Service
  3. Set it to receive selected text in any application
  4. Check the Output replaces selected text tick-box
  5. Insert the Run AppleScript action
  6. Copy and paste the above AppleScript
  7. Press Run to make sure it works
  8. Save the .workflow file to ~/Library/Services
  9. Open the Keyboard > Keyboard Shortcuts system preference pane, select Services in the left column, then find the Service you just created.
  10. Assign a keyboard shortcut.