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

Question:

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

Reply
Question marked as Solved
Answer:
Answer:

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

Posted on

Page content loaded

Question marked as Solved

Nov 11, 2017 5:55 AM in response to vswhk 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 11, 2017 5:55 AM

Reply Helpful

Nov 11, 2017 11:09 PM in response to VikingOSX 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 11, 2017 11:09 PM

Reply Helpful

Nov 12, 2017 2:55 AM in response to vswhk In response to vswhk

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.

Nov 12, 2017 2:55 AM

Reply Helpful

Nov 12, 2017 8:39 PM in response to HD 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:39 PM

Reply Helpful

Nov 12, 2017 8:45 PM in response to VikingOSX 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 12, 2017 8:45 PM

Reply Helpful

Nov 13, 2017 12:14 AM in response to vswhk 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 13, 2017 12:14 AM

Reply Helpful

Nov 13, 2017 4:30 AM in response to VikingOSX 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 4:30 AM

Reply Helpful

Nov 13, 2017 5:37 AM in response to vswhk In response to vswhk

I recommend that you create a new Service, not alter an existing one that doesn't work. Same settings as I have shown previously.


I have tested my one-liner date stamp Service on El Capitan 10.11.6, and on High Sierra 10.13.1, and it inserts a date into Notes, TextEdit, Pages, etc. I don't have MS Word.

Nov 13, 2017 5:37 AM

Reply Helpful

Nov 13, 2017 7:01 AM in response to Tony T1 In response to Tony T1

Can't get this resolved.

(tried: /System/Library/CoreServices/pbs)


User uploaded file


Service name is "Untitled":

User uploaded file

User uploaded file

Nov 13, 2017 7:01 AM

Reply Helpful
User profile for user: vswhk

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