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

Applescript Date and Time

I've created a quick action with the following script, and it works perfectly as a service. What I want, however, is just the current date, NOT the time appended at the end. Can anyone tell me the syntax for this little script:


on run {input, parameters}

set thedate to (current date) as string

tell application "System Events"

keystroke thedate

end tell

end run


Also, how can I attach a keyboard shortcut to this quick action?


Many thanks!!

MacBook Pro (2020 and later)

Posted on Jan 8, 2022 5:29 AM

Reply

Similar questions

6 replies

Jan 8, 2022 7:13 AM in response to BocaBoy

Here is how you build just the current (formatted) date. See MacScripter / Dates & Times in AppleScripts.


set {year:y, month:m, day:d} to (current date)
# pad the day and month if single digit
set day_str to text -1 thru -2 of ("00" & d)
set mon_str to text -1 thru -2 of ("00" & (m * 1))
# make ISO8601 date string without time
set theDate to (y & "-" & mon_str & "-" & day_str) as string



The variable theDate now appears as "2022-01-08".


You can assign a keyboard shortcut to the Quick Action via System Preferences > Keyboard > Shortcuts > Services panel. Scroll down until you encounter your Quick Action (service) name, and on the right side, you will see a faint gray none. Click on that and it will change to an Add Shortcut button. Click that again, and enter the non-conflicting keyboard shortcut, finishing by pressing return.


That keyboard shortcut may or may not work the first time you invoke it, and you may need to prime the pump by invoking it as an application menu > Services > Your Quick Action name. Thereafter, if there are no other application or keyboard shortcut conflicts, it should then function as a proper keyboard shortcut.



Jan 8, 2022 8:26 AM in response to VikingOSX

And the UNIX way to parse the AppleScript date/time string is the following:


use scripting additions

set theDate to ((current date) as string)'s quoted form
# strftime man page: https://www.freebsd.org/cgi/man.cgi?query=strftime&sektion=3
set YYYY_MM_DD to (do shell script "date -j -f '%A, %B %e, %Y at %T %p' " & theDate & " +%Y-%m-%d") as text


2022-01-08

Jan 8, 2022 3:54 PM in response to BocaBoy

A Quick Action or Service requires a specific document type (or No Input), and a specific application name (or any application) to work. Something needs to be selected for the QA/Service to run unless you chose the No Input option.


What does your Quick Action header look like:



On Monterey 12,1 , I built this Run AppleScript-based Quick Action.



I opened the Terminal application and from its Service menu, I selected the Quick Action. The following output resulted:




The issue with your last post was that timestamp is the entire AppleScript date and time response. If you just wanted the date in m/d/yy format then:


set timestamp to short date string of (current date) as string



will do the job. Any more involved in the returned date format and you need to either adopt my previous solution or review the MacScripter link.


Jan 8, 2022 10:41 AM in response to VikingOSX

Viking,


Thanks for the suggestions, but this isn't working in Monterey, at least not for me. I run the script in Automator and it seems to complie just fine, but after I save it and then try and access it via the Services menu in Text Edit, Notes, etc., nothing happens. It shows in Services, it just does nothing.


For the record, Accessibility for these apps has already been set and checked.


Lastly, I cant believe it's this hard or complicated to do something that should be dead simple, part of the OS itself. Anyway, just venting. Im not a programmer and don't understand why I have to be one to do something simple like enter today's date.



Jan 8, 2022 3:04 PM in response to VikingOSX

Just wanted to let you know that the following script works fine and is mapped to a shortcut key. Unfortunately, it returns a date stamp with the time appended to the end. Any suggestion as to what needs to be done to eliminate the time and be left with Saturday, January 8, 2022? I tried the script you were kind enough to send, but the output was the same. Here's the script I am currently using in Automator:


on run {input, parameters}

set timestamp to (current date) as string

tell application "System Events"

keystroke timestamp

end tell

end run


Thanks again for all of your time in helping to resolve this problem.

Applescript Date and Time

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