0

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:

5
  • RE: "Not only is this a nuisance (you have to do it every time)" -- You should only have to do it one time for each app that you trigger it from, not every time. – user3439894 Jul 24 '20 at 17:06
  • 1
    Cmd/V will paste at current position ;-) Your actual requirement is unclear. Why do you need to do this by script? What are you actually trying to achieve? Right now, it's sounding like an XY Problem – Tetsujin Jul 24 '20 at 17:06
  • I want to be able to paste the current time stamp in UTC with a single keyboard binding. This general solution seems great, but not well connected. – Breedly Jul 24 '20 at 17:22
  • If you are just wanting to keystroke the UTC at the cursor, then these two lines of AppleScript code is all that's needed: set UTC to (current date) - (time to GMT) as string and tell application "System Events" to keystroke UTC which then can be triggered by FastScripts so it only has to be given permission once. Note: I am not affiliated with Red Sweater Software, LLC the developer of FastScripts, just a satisfied user. – user3439894 Jul 24 '20 at 18:04
  • There are apps that do this and more. Have a look at aText Text Expander – Allan Jul 25 '20 at 9:11
3

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.

3
  • It's very nice this doesn't require any security settings to be set and worked immediately without issue in the different apps I tested this on! +1 -- The only thing is the custom Image and Color do not show up for me, it's still the black gear, which I'm okay with. – user3439894 Jul 27 '20 at 0:10
  • @user3439894 Thanks. Yeah, I'm not too clear about the image and colour either. My only guess is that it's for Quick Actions that get displayed in Finder's preview panel. – CJK Jul 27 '20 at 7:03
  • Yeah, the Calendar icon shows on my Insert Date & Time.workflow bundle, in ~/Library/Services, but have no idea where the blue color setting comes in. – user3439894 Jul 27 '20 at 17:41
0

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.

You must log in to answer this question.

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