Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Can I write a script to output current date without either 1) having to select text or 2) having to save to clipboard?

Can I write a script to output current date without either

1) having to select text or

2) having to save to clipboard?


I don't understand why "Output Replaces Selected Text" is an option to check when scripts do not work when it is unchecked.


I don't want to first have to select text when all I want is to hotkey the current date to text.


The other option is to copy current date to clipboard 'tell' application by invoking control v. This is so 'not elegant'.


I do not want the date to replace what I have copied to the clipboard. I still need to use that.


I tried

on run

set r to short date string of (current date)

tell application "System Events"

r

end tell

end run


but I have to first type some random text, select the random text and then my shortcut keys.


Does someone have a more elegant way?

MacBook Air, iOS 11.1

Posted on Nov 11, 2017 12:17 AM

Reply
Question marked as Best reply

Posted on Nov 11, 2017 5:55 AM

Here is an Automator service (named DateStamp) that simply outputs the designated datestring into a document. You will notice that it does not expect any input (selection of text) from any application, and it needs that Output replaces selected text to be checked so that it has permission to write the date string out. In this example, it simply runs the UNIX date command with a specific format string to control how the date is shown. From the application's Service menu → DateStamp.


As shown the date string is output as 2017-11-11. If you want the same as the AppleScript short date string, change the format to +"%m/%d/%y" for a two-digit year, or %Y for the full year.


User uploaded file

22 replies
Question marked as Best reply

Nov 11, 2017 5:55 AM in response to vswhk

Here is an Automator service (named DateStamp) that simply outputs the designated datestring into a document. You will notice that it does not expect any input (selection of text) from any application, and it needs that Output replaces selected text to be checked so that it has permission to write the date string out. In this example, it simply runs the UNIX date command with a specific format string to control how the date is shown. From the application's Service menu → DateStamp.


As shown the date string is output as 2017-11-11. If you want the same as the AppleScript short date string, change the format to +"%m/%d/%y" for a two-digit year, or %Y for the full year.


User uploaded file

Nov 13, 2017 12:14 AM in response to vswhk

Hi,


If you want to see my script run in Automator, comment out the run handler and run the script (not the Service) using the action's run button:



User uploaded file


It will type the date at the end of the script. Revert the script to my original before saving.


Next, in System Prefs, Security & Privacy, are Automator and Accessibility Inspector allowed to control your computer:


User uploaded file




Next, could you check what happens if you run the Service from the Services menu within Notes, rather than using a shortcut?


Also, if you use your shortcut in Notes, do you see the Automator cogwheel turning briefly in your menu bar?


Cheers,


H

Nov 11, 2017 11:09 PM in response to VikingOSX

VikingOSX, first thank you for your reply.


This is precisely what I am talking about. I've tried this script before and it requires that I first type some random text, then select the text that I typed before I use hotkeys to invoke this script in order to insert the DateStamp. I can't just hotkey to insert the DateStamp.


I need to get rid of the steps... type text, select text...


The other option suggested by other threads copies the date to the clipboard and the script invokes a 'v' command to paste. I don't want to affect what I have in the clipboard.

Nov 12, 2017 8:39 PM in response to HD

HD, I really appreciate you guys' input.


I tried your script and couldn't get it to print date to Notes. Not sure what I'm doing wrong.

Here are my steps

1. Launch Automator

2. Edit my "InsertDate.workflow" script

3. delete what I had previously and clicked "Run Applescript"

4. Pasted your script.

5. Ran the script and couldn't get it to Result the date.

6. Saved it anyway -

7. and invoked with my shortcut keys... Opt Command ;

8. Nothing happened.

9. I even tried to change setting to "Output replaces selected text'. Saved and tested.

10. did not work without first adding text, select text and invoke. AND just invoking.

11. No joy.


What am I doing wrong?

Nov 12, 2017 8:45 PM in response to VikingOSX

VikingOSX, I'd thought I was following your instructions, but I couldn't get any result trying to insertDate to Notes nor MS Word.


Here's what I did.

1. I had previously created a script "insertDate.workflow" and added a shortcut key in systems Preference/ services/ text.../

2. I edited insertDate with your script - as Run Shell Script.

3. Saved and then tested in Notes and MS Word.


The only way i could get it to work is by first typing some garbage, copy the text and invoke.


Do you see what I did wrong?

Nov 13, 2017 4:30 AM in response to VikingOSX

VikingOSX wrote:


Re-read my post. Actually implement and test this Automator service. It very clearly states that it does not accept text from the source application, no text needs to be selected beforehand, and it writes the date string into the current document. I actually test things here before I share them.


I can't get it to work either, but it's not your script, something is borked with my system.

I get "No Services Apply" when I try this is TextEdit.

(Trying to fix my system now…)

Nov 13, 2017 11:50 AM in response to VikingOSX

VikingOSX, I did as you had suggested by starting afresh and without file extension.


It works now in notes! Thank you.


It also works in WORD, but you have to type some junk and then select the junk. When I tried to run from services (without shortcut keys) and not selecting text, the service does not appear. Once I select text, a bunch of services became available and I could click on dateStamp and it replaced text with the date.


i tried different things and couldn't get it add to the service without first highlighting text.


BTW, the workflow extension was added automatically.


Thanks - it worked - for the most part.


I just couldn't believe the OS do not already have this shortcut available and pre-configured.


Also - what is a flux capacitor? Are you talking about an electronic component or system related?

Nov 13, 2017 12:21 PM in response to vswhk

Apple Services may behave differently in MS Word than elsewhere. This Service does not work in LibreOffice at all, because LibreOffice is based on a different development kit. Not the fault of the Service.


By default, a regular Service expects selected text, which as you have observed, lights up a bunch of additional Services. My particular service explicitly says that no input from any application, so it does not require text selection in Cocoa text based applications (TextEdit, Pages, Notes, etc.)


Flux capacitor, as in Back to the Future movie. It is what powered the DeLorean car, and my usage of it here was a joke to Tony.

Nov 13, 2017 12:46 PM in response to vswhk

Just out of curiosity, vswhk, would you mind trying the shortcut to my service (or a brand new creation thereof) in Pages, or TextEdit, or Mail, or in a Safari text input window?


These work infallibly for me, but in Notes I'm finding I have to at least edit some text, or change the cursor position, before the service will run (although I always see the spinning cogwheel). No idea why, but I'd be interested to see if you get the same result.


Cheers,


H

Nov 13, 2017 1:29 PM in response to HD

HD, your script didn't work for me.


I altered it to my original script and now it works.


on run {input, parameters}

set r to short date string of (current date)

tell application "System Events"

r

end tell

end run


both in notes and textedit.


I think the trick was to create a brand new script. I must have saved my original script incorrectly.


Try mine and use a brand new script.

Nov 13, 2017 1:58 PM in response to vswhk

My original script (the one that works for me infallibly, except in Notes, where it's erratic, and in some third-party applications) is as follows:


on run {input, parameters}

set r to short date string of (current date)

tell application "System Events"

keystroke r

end tell

end run


I have tried yours in a brand new Automator action and I'm afraid it doesn't work for me.


I'm dropping out of this. My flux capacitor needs recharging.

Can I write a script to output current date without either 1) having to select text or 2) having to save to clipboard?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.