11

I am on El Capitan OS X.

I want to be able to hit some keyboard shortcut and then have today's date be automatically outputted.

Example:

When I press Option + D, "2016_06_13" is pasted into the text field.

The date would be automatically calculated based on my time zone.

Ideally, I'd like to be able to do this universally. So in Finder, Safari, TextEdit, etc. — not just exclusive to one program.

Having this shortcut would streamline my workflow.

Is this possible?

16

You could do it with Automator like this:

  1. Create new: Services
  2. Services receives no input in any application
  3. 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))


  1. 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 of Save as... the Service file is automatically put in the right place.

enter image description here

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.

7
  • Would it be possible to learn how to do this without clipboard interference? Aug 9 '16 at 4:46
  • @rubik'ssphere Updated the answer.
    – Joonas
    Aug 9 '16 at 7:56
  • 1
    This now throws an error: “System Events got an error: com.automator.runner.xpc is not allowed to send keystrokes.” related: forums.developer.apple.com/thread/106949
    – SimplGy
    Jul 21 '19 at 19:25
  • @SimplGy, not sure which answer this person apple.stackexchange.com/a/348669/126929 meant he said "above", but he talks about this particular issue. However, I'm still of the mind that this was the best answer: apple.stackexchange.com/a/242552/126929 or at least the middle paragraph that talks about text expander. I'd highly recommend aText. Makes this whole process simpler and also makes other things possible too.
    – Joonas
    Jul 21 '19 at 21:13
  • In case you want YYYYMMDDHHmmss: 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)) & ¬ text -2 thru -1 of ("00" & ((day of _Date) as integer)) & ¬ text -2 thru -1 of ("00" & ((hours of _Date) as integer)) & ¬ text -2 thru -1 of ("00" & ((minutes of _Date) as integer)) & ¬ text -2 thru -1 of ("00" & ((seconds of _Date) as integer)) end tell Mar 5 '20 at 20:47
15

I wanted to use the system date format for consistency, rather than programming it into the shortcut.

It takes about 30 seconds to set up.


In Automator (Applications folder)

  1. New service

  2. Receives no input in any application

  3. Drag Run Applescript from the Utilities section on the left into the right pane

  4. Check Output replaces selected text at the top of the right pane

  5. 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
  1. Save the service as Short Date workflow

enter image description here

In System Preferences > Keyboard › Shortcuts

  1. Click Services on the left

  2. Short Date will be listed under Text in the right panel

  3. Add a key combination for the new Short Date service

enter image description here

In System Preferences > Lanuage & Region › Advanced

  1. Click on the Dates tab

  2. Modify the Short: format as desired

  3. Click OK

enter image description here

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.

3
  • What could I do to find out why exactly this doesn't work for me? :/
    – Bloke
    Aug 10 '18 at 10:51
  • You should see "short date" under the services menu, under the menu for the application you're using. I'm in Chrome right now, and under "Chrome › Services" I see "Short Date". There are some contexts where it won't work, like the open and save dialog. I should have mentioned that in my answer, and will modify it. Aug 10 '18 at 15:00
  • Automator is not allowed to send keystrokes
    – vaughan
    Aug 31 at 18:05
5
  1. Create a Quick Action with Automator. Screenshot below.
  2. Set the Accessibility settings in System Preferences > Security & Privacy > Privacy.
  3. Assign a keyboard shortcut to the Quick Action. (I mapped mine to ⌃⇧⌘D for Shortdate ((D)ate) and ⌃⇧⌘T for Longdate (date + (T)ime), I even made one for 10 dashes ⌃⇧⌘-.)

I did it this way to avoid using System Events Keystrokes because it will ask for permission in each application you use. Also, I avoided using the system settings for the date formatting. With this, you can just edit the output format as part of the script and can further tweak the output beyond just time/date.

Examples:

date +"%Y_%m_%d"
-- Your Desired Result: 2021_07_11
date +"%Y%m%d %H:%M"
-- Result: 20210711 11:51
date +"%Y%m%d"
-- Result: 20210711
date +"Today's Date: %Y_%m_%d"
-- Result: Today's Date: 2021_07_31
date -u +"%Y-%m-%dT%H:%M:%SZ"
-- Result: ISO 8601 Date - 2021-07-11T14:49:17Z
date "+DATE: %y/%m/%d%nTIME: %H:%M:%S"
-- Result: DATE: 21/07/11
           TIME: 22:48:10
date +"%Y%m%d"
-- Result: 20210711 (My preferred Shortdate format)
date +"%Y%m%d %H:%M"
-- Result: 20210711 22:58 (My preferred Longdate format)

SDate Quick Action Sample

2
  • 1
    Love the simplicity and versatility!
    – Patrick
    Aug 18 at 19:22
  • 2
    Important is to enable “Output replaces selected text” in the workflow settings (see screenshot). Otherwise the date won’t be inserted. Aug 19 at 15:16
4

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.

0

The above options work well until Mojave. I was trying to insert the current date into the Reminders app with a keystroke and I get an error that suggests that the automator does not have permissions to perform a keystroke command. To allow this go to "System Settings -> Security & Privacy -> Privacy -> Accessibility -> Reminders" It is annoying that it only permits the action on the specific app but that seems to be the case.

0

I realize this thread is kind of old, however, I seem to have solved the issue with automator, in this instance at least. if you look under Security & Privacy you'll see input monitoring. I took my script and drug it in to add it from the services folder. Once done I stopped receiving the workflow error.

So:

open finder press option and open library open services

you'll see the short date script

open settings ==> security and privacy ==> privacy ===> input monitoring

unlock

drag your script from the services folder to the list.

As near as I can tell it is like with virtualbox, virtualbox has to have permission to monitor the keystrokes in order to use them in the system. It looks like for this script that is the same idea.

0

Spark app can do that. and its free

https://www.macupdate.com/app/mac/14352/spark

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .