Inserting Date & Time on Mac OS
After spending twenty minutes Googling and setting up a simple Mac OS
service to insert the current date and time (formatted as 2018-09-03
16.36
) I thought I'd document the steps quickly.
It's very simple to set up the service using Mac's Automator:
- Launch Automator, and create a new Service.
- This particular service receives no input.
- Drag in Run AppleScript to the service.
- The script I used to insert a date is:
on run {input, parameters} set dateString to do shell script "date +'%Y-%m-%d %k.%M'" tell application "System Events" keystroke dateString end tell return input end run
- Save the service and remember the name.
- Open System Preferences > Keyboard > Shortcuts and map the service to a useful keyboard shortcut.
Some interesting sticky points:
- It's remarkably painful to format a string in pure applescript,
which is why I went with a custom
date
call. The format string is described at the date man page. - If the shortcuts overlap in any way with the application's custom
shortcuts, they won't work. I ended up choosing the relatively
unergonomic
⌥ + ⌘ + ,
. Of course, this doesn't work in Emacs which intercepts everything.
AppleScript seems at one and the same time remarkably powerful and painful: I suspect the best way to use it is to use it carefully as a glue language, dropping out to an actual programming language (say Rust, or Python) for any actual computation.