On Windows I use the PSPad editor which has a nice ALT-D timestamp which you can edit the format of, e.g. yyyy-mm-dd hh:mm:ss.

When working outside an editor, e.g. Google Docs, I have Autohotkey which I have programmed CTRL-D to insert a yyyy-mm-dd hh:mm:ss timestamp.

I am now working on a Mac mostly using TextWrangler as my editor but I can't find a timestamp hotkey in its features.

What is the easiest way to get a yyyy-mm-dd hh:mm:ss hotkey on Mac, either in a (free) text editor or a (free) autohotkey equivalent?

link|improve this question

69% accept rate
Please comment on my answer if it doesn't work for you. – Daniel Beck Apr 24 '11 at 9:00
feedback

2 Answers

One option is to use a shell script or Python/Perl/Ruby script.

One option, using Python:

#!/usr/bin/env python
import time
t = time.localtime()
# yyyy-mm-dd hh:mm:ss
print '%d-%02d-%02d %02d:%02d:%02d' % (t.tm_year, t.tm_mon, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec)

Another, shorter, by @NReilingh, using date (shell script):

date "+%Y-%m-%d %T"

Use /Applications/Automator.app to create a Service that executes this script. Add the Run Shell Script Automator action and insert the code above. Select no input in any application and replaces selected text. Then save.

It will be placed in the Services menu which is accessible from any application's menu bar by selecting the menu with the application's name. It might look something like this when you use it:

alt text

Assign keyboard shortcuts in the Keyboard preference pane in System Preferences.

alt text


The no longer free TextExpander has a feature similar to what you want. It's an application designed for snippet insertion, e.g. for partial email templates.


TextMate is an extensible, commercial editor that allows you to easily define custom commands, again in shell or scripting languages, and assign keyboard shortcuts to them.

link|improve this answer
If I do say so myself, $ date "+%Y-%m-%d %T" is a bit more compact... but I can't get either of these to work the way I want. How is one supposed to get an automator action to insert text at the current text input? – NReilingh Dec 30 '10 at 9:09
Okay, figured it out: the "Replaces selected text" up at the top MUST be checked for raw shell script output to be inserted. Damn thing took me half an hour to figure out--I was about to post an answer using AppleScript keystroke instead. Also, make sure to define "Service receives no input in any application" up at the top if you want the service to be universally accessible. – NReilingh Dec 30 '10 at 9:40
@NReilingh Sorry about that, but I was in a hurry as I posted this, with the intent to improve it once I had the chance (which is in a few minutes now). – Daniel Beck Dec 30 '10 at 10:42
I'd like to add that you must select usr/bin/env Python or something like that (the one that has Python) from the drop down menu, otherwise whatever application calls the Service script won't know to recognize that its Python, I think. At least it didn't work for me until I did that. – Lucas Spangher Feb 21 at 21:29
feedback
-- delay 0.3 -- time for releasing modifier keys used in a shortcut
set old to the clipboard
set the clipboard to (do shell script "date +'%Y-%m-%d %H:%M:%S'")
tell application "System Events" to keystroke "v" using {command down}
delay 0.03
set the clipboard to old

You can assign a shortcut for running an AppleScript with for example FastScripts, NuKit, Quicksilver or Keyboard Maestro.

link|improve this answer
What's the purpose of Cmd-C between storing the old clipboard and setting new clipboard contents? – Daniel Beck Jun 18 '11 at 17:36
@DanielBeck Nothing, thanks for pointing it out. – Lri Jun 19 '11 at 8:47
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.