Apple - How to automatically paste today's date with keyboard shortcut?
You could do it with Automator like this:
- Create new:
Services
- Services receives no input in any application
- Use the search to find and add action:
Run Applescript
- Copy and paste the following to the field on the right:
tell application "System Events"
set _Date to (current date)
keystroke ¬
(year of _Date as text) & "_" & ¬
text -2 thru -1 of ("00" & ((month of _Date) as integer)) & "_" & ¬
(day of _Date as text)
end tell
Note that the day
will not have a leading zero like the month
does. If you want that, you could replace the line with: (day of _Date as text)
with: text -2 thru -1 of ("00" & ((day of _Date) as integer))
- Save with: Cmd + S. Give it a name you can remember in the next step.
- If you simply use the
Save
command ( Cmd + S ) instead ofSave as...
the Service file is automatically put in the right place.
- If you simply use the
The Last step would be to define a shortcut for the service you just made.
Go to mac System preferences > Keyboard > Shortcuts > Services
and then find the service you saved before. You should find it at the bottom area of the list. Double click the none
text on the right side of the service name and give it a shortcut.
A simpler and faster answer:
I wanted to use the system date format for consistency, rather than programming it into the shortcut.
It only takes about 30 seconds to set up.
IN AUTOMATOR (Applications folder)
New service
Receives no input in any application
Drag Run Applescript from the Utilities section on the left into the right pane
Check Output replaces selected text at the top of the right pane
In the Applescript window replace all the text with
on run {input, parameters}
set _date to short date string of (current date)
tell application "System Events"
keystroke _date
end tell
end run
- Save the service as Short Date workflow
IN SYSTEM PREFERENCES (Keyboard › Shortcuts)
Click Services on the left
Short Date will be listed under Text in the right panel
Add a key combination for the new Short Date service
IN SYSTEM PREFERENCES (Language & Region › Advanced)
Click on the Dates tab
Modify the Short: format as desired
Click OK
Note: this system depends on the Services menu under the Application menu. When the menu is not accessible, the service isn't either. It does not work, for example, in the open and save dialog.
The easiest way I know of to do this is with 3rd-party text expansion apps. Personally I use Typinator, with a rule that expands the word dt
(i.e. typing D then T) to {YYYY}_{MM}_{DD}
, which is Typinator's placeholder format for the current date. The program uses the local system time and OS accessibility features to fill into nearly any text field, so it would be a good fit for your use case.
There are other programs that also do this, the most well known of which is probably TextExpander. Lifehacker has a good round-up of text expansion apps from a few years ago which may be worth a look.
It is also possible to accomplish this with AppleScript and a launching application like Alfred or Quicksilver, as in this answer, but for me the convenience of being able to expand many different abbreviations with an easy-to-learn syntax and UI outweighs the cost savings of using AppleScript.