What is the easiest way to get a yyyy-mm-dd hh:mm:ss timestamp hotkey on the Mac?
Solution 1
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:
Assign keyboard shortcuts in the Keyboard preference pane in System Preferences.
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.
Solution 2
There's a bug in 10.7 and 10.8 where shortcuts for Automator services don't always work until the services menu has been shown from the menu bar. There's also a noticeable delay before services are run. Another option would be to assign a shortcut to a script like this:
set old to the clipboard as record
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.05
set the clipboard to old
keystroke
doesn't ignore held down modifier keys, so add a small delay to the start if you run the script with a shortcut that has other modifier keys than command. FastScripts waits until modifier keys are released before running scripts that contain keystroke or key code commands.
Solution 3
You can install a Freeware add-on called WordService that adds this functionality as a service, for which you can assign a keyboard shortcut in System Preferences -> Keyboard -> Shortcuts.
Here is an article about it from Mac OSX Tips - http://www.macosxtips.co.uk/index_files/automatically-insert-date-and-time.php
Download from http://www.devontechnologies.com/download/products.html or from App Store http://appstore.com/mac/wordservice
Solution 4
Lauri's solution is way more elegant than mine, but here's my AppleScript-only solution. Is it faster than running through the clipboard and using a shell script? Maybe.
Anyway: There are a lot of solutions to get it assigned to a key, but I've found that the easiest is Quicksilver SparkFastScripts. Spark can just use the scpt directly, which is what the AppleScript solution saves.
Date and Timestamp Trigger in OSX
-- yar2050 (Dan Rosenstark)'s favorite date format
-- 2017-08-03 update
on fillInZero(int)
if (int < 10) then set int to "0" & int
return int as string
end fillInZero
set theDate to (current date)
set theHour to fillInZero(hours of (theDate) as integer)
set theMinutes to fillInZero(minutes of (theDate) as integer)
tell theDate to get (its month as integer)
set theMonth to fillInZero(result)
set theDay to fillInZero(day of theDate)
tell (theDate) to get (its year as integer) & "-" & (theMonth) & "-" & theDay & " " & (theHour) & ":" & theMinutes
set date_ to (result as string)
tell application "System Events"
set frontmostApplication to name of the first process whose frontmost is true
end tell
tell application frontmostApplication
delay 0.2 -- I have no idea why this is necessary
activate
tell application "System Events"
if (frontmostApplication = "Evernote") then
keystroke "h" using {command down, shift down}
keystroke "b" using {command down}
keystroke date_
keystroke "b" using {command down}
else
keystroke date_
end if
end tell
end tell
Benoit
web/software developer, .NET, C#, WPF, PHP, have philosophy degree, love languages, run marathons my tweets: http://www.twitter.com/edward_tanguay my runs: http://www.tanguay.info/run my code: http://www.tanguay.info/web my training videos: http://partner.video2brain.com/edwardtanguay
Updated on September 17, 2022Comments
-
Benoit over 1 year
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?
-
HikeMike about 13 yearsPlease comment on my answer if it doesn't work for you.
-
-
NReilingh over 13 yearsIf 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 over 13 yearsOkay, 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.
-
HikeMike over 13 years@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).
-
HikeMike almost 13 yearsWhat's the purpose of
Cmd-C
between storing the old clipboard and setting new clipboard contents? -
Admin about 12 yearsI'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. -
Dan Rosenstark over 11 yearsUsing the clipboard is a mess, particularly if the user is using ClipMenu or something similar.
-
brevno over 11 years@Yar Alfred ignores "transient" clipboards, and
keystroke
doesn't know how to insert colons with my keyboard layout (but it's a bit of an edge case).keystroke
might be better for inserting short ASCII-only strings like this though. -
Dan Rosenstark over 11 yearsWhew just checked with Spanish, thought maybe my Applescript wouldn't work. Glad it's ok, sorry that certain languages aren't given the same precedence. This is why scripting is so much more annoying than programming ;) Will check into using AlfredApp for that. They probably deserve the $$$
-
schmunk about 9 yearsHow can I unselect the text afterwards?
-
HikeMike about 9 years@schmunk At least on OS X 10.9, the text isn't selected after running the Automator service.
-
schmunk about 9 yearsIn TextWrangler on 10.10 it is selected on my MacBook.