AppleScript: insert Date and Time into your Documents

This is an AppleScript that inserts the current Date and Time in any document you are working in.


The script acts like Emacs with C-u M-! date or with vim‘s :r !date.

First of all I wanted the script to work across all my GUI apps in Leopard. So I resorted to AppleScript. After fiddling for a while with the grammar of the language I came up with a solution. The only problem was that I couldn’t launch the script from a keyboard shortcut.

Use Quicksilver to trigger Applescript

Luckily, quicksilver has triggers that you can configure to do almost anything. I mapped Shift+Command+- (hyphen) to run the AppleScript bellow. As a result, the script copies the current Date and Time to the clipboard and then asks the foremost application to paste it.

This works well and is cool to have the date on the clipboard for future pastes if I want. This is great keystroke saver for working with log files! Just use Script Editor and put it in your User Scripts Folder! A time saver!

-- Insert Date and Time into your documents
-- @2012 David Rodrigues
set date_ to ((current date) as string)
set the clipboard to the date_
tell application "System Events"
	set frontmostApplication to name of the first process whose frontmost is true
end tell
tell application frontmostApplication
	tell application "System Events"
		keystroke "v" using {command down}
	end tell
end tell