TransWikia.com

Applescript to output UTC Datetime at cursor without SystemEvents

Ask Different Asked by Breedly on December 31, 2021

It would be enormously helpful if I could bind a script that outputs the current UTC timestamp to keybindings and have the output magically appear at my cursor in whichever application I was using. This is pretty easy to do with Automator using a script like this:

on run {input, parameters}
    tell application "System Events"
        keystroke input
    end tell
end run

However, macOS Catalina’s security permissions trigger the following alert for any application I use this in for the first time.

System Events Permission Message

Not only is this a nuisance (you have to do it every time), the security implications seem to be pretty wide ranging.

My best alternative is to just copy my input to my clipboard and manually paste each time, but I would like a single keybinding to just dump my script output at the current cursor in any application.

Is this wildly insecure? Is there a way I can just paste at current position without a security pop up and be safe?

Related:

2 Answers

Extending @CJK's excellent answer, neither the ISO-8601 format nor the default Applescript one works for me, and I'm more comfortable programming in Javascript, so I came up with this:

enter image description here

If I were programming an app, I would momentjs to format the date, but for this personal Quick Action, it seemed more expedient to brute-force it. Anyway, I don't even know if you can import modules in JXA, seems unlikely.

Answered by chetstone on December 31, 2021

Don't use System Events to do this. As well as the difficulties you've discovered, it's also a pretty unreliable way to do this, a potential security risk, and—thankfully—not at all necessary.

Here's an outline of how to use Automator to insert the date at the cursor's location in the currently active application:

  1. Create a new Automator workflow, selecting Quick Action as the type. Enter the name you wish it to go by, but try and be sure because changing it later in this particular instance could be irksome.

  2. When you're presented with the newly-created, blank workflow, there are options at the top that allow you to dictate what sort of input you can pass to the workflow, and in which applications the service will be available. Set these to the following:

    • Workflow receives: no input

    • in: any application

  3. The third option you need to select is this one:

    Screenshot of Automator's Service workflow input options

    This—as the name suggests (in a slightly misleading way)—causes the Quick Action to run as normal, and at the end of its run, the final result—assuming it can be represented as text—will replace the currently active text selection, much like when one pastes the contents of the clipboard into their document. If a text object permits highlighting of its text content, then it will always contain a text selection object, regardless of whether or anything text is highlighted. In this instance, i.e. most of the time, it is located wherever the cursor is, and contains zero characters.

  4. Create a Run AppleScript action. Delete any default script text that it contains so you have a blank editing field, into which you can paste the following code:

    tell ((current date) - (time to GMT)) as ¬
        «class isot» as string to return the ¬
        contents of [text 1 thru 10, space, ¬
        text 12 thru -1] as text
    

    This produces an formatted date and time derived from the ISO-8601 international standard, and looks like this: "2020-07-26 14:21:11". It's basically the order that date and time parameters should have been dictated from the very start, but most countries unfortunately didn't think hard enough, and ended up with the year in third position, and the date and month in some arbitrary choice between the first two positions. How ridiculous.

  5. Save. Quit. Assign a keyboard shortcut in System Preferences. Done. Here's the finished workflow:

Screenshot of Automator Service workflow in macOS Catalina


(Almost done) Whilst the keyboard shortcut you set via System Preferences > Keyboard > Shortcuts > Services should work in all appropriate situations, it seems to be buggy in web page text entry fields. The menu option appears in the services menu, but without a shortcut seemingly assigned to trigger it. Therefore, create an additional shortcut via System Preferences > Keyboard > Shortcuts > App Shortcuts > All Applications and assign it the same hotkey as the other. Now it works.

Usually, editing a workflow and saving it will effect the changes more-or-less immediately so that it works outside the Automator environment, as you'd expect. This isn't necessarily the case with the option to replace selected text, which can take quite some time or require a system reboot to work outside Automator. Therefore, two things I advise you get correct to help avoid starting all over again are: 1) The name you wish your Quick Action/Service to have; and **2)**That you check the box to allow output to replace selected text. If something doesn't work, delete it all and start again.

Answered by CJK on December 31, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP