Applescript delay not working well on Apple M2 for web page snippet script


Describe the bug
This script is a simple one for use saving web page text snippets w/ the URL in a browser. You go to a web page in Chrome, highlight some text on the page that you want to save, and then run the BTT shortcut/trigger. It is supposed to copy the text to the clipboard, move the mouse pointer to the search / URL box, click, select the URL, and copy the URL to the clipboard as well. It then sets the clipboard equal to the original text snippet plus the associated web page URL.

It worked on an Intel based Mac okay. It too was a little unreliable; worked like 90% of the time. I had to add all of these delays in different places to even get it working somewhat reliably at all on Intel Mac.

But, after switching to ARM, “delay” function in AppleScript is just about completely broken, and barely ever works. Or, at least, it is breaking my scripts. I have tried to modify delay timings, but doesn't seem to make any difference.
It works on the first invocation after a long period of inactivity, or moving to a new app, and then back to the browser.

Otherwise, what happens is I end up with only the text snippet and no URL, or the text snippet duplicated twice and no URL in the clipboard.

On multiple runs, it is like the delays are cached out and no longer run. Or, maybe, parts of the script are run async. I know that should be impossible. The behavior doesn't make sense. Grasping at straws here.

Next part is way out there speculation that is just a wild thought. Realize this is probably completely wrong, but perhaps it might spark some insight.
Perhaps the applescript gets interpreted directly first correctly (and run), then reduced to byte code and cached, but the byte code version doesn't work properly. Not sure if applescript works similar to java.

Thanks for Bettertouchtool. Long-time user. Any ideas to address this?


Affected input device (e.g. MacBook Trackpad, Magic Mouse/Trackpad, Touch Bar, etc.):

Keyboard shortcut


Screenshots


Device information:

  • Type of Mac: MacBook-Pro M2, 16", 32GB
  • macOS version: Ventura 13.5
  • BetterTouchTool version: 4.185 (2404) alpha

Additional information (e.g. crash logs, related issues, etc.):

Attaching shortcut JSON below.

[
  {
    "BTTLastUpdatedAt" : 1692025598.3944101,
    "BTTTriggerType" : 0,
    "BTTTriggerClass" : "BTTTriggerTypeKeyboardShortcut",
    "BTTLayoutIndependentActionChar" : "c",
    "BTTAutoAdaptActionToKeyboardLayout" : true,
    "BTTGenericActionConfig" : "autoAdapt",
    "BTTShortcutToSend" : "55,8",
    "BTTAdditionalConfiguration" : "786432",
    "BTTKeyboardShortcutKeyboardType" : 26364,
    "BTTUUID" : "CB7D3F75-0244-4987-87AB-47B97D5CA591",
    "BTTTriggerOnDown" : 1,
    "BTTLayoutIndependentChar" : "c",
    "BTTEnabled" : 1,
    "BTTEnabled2" : 1,
    "BTTShortcutKeyCode" : 8,
    "BTTShortcutModifierKeys" : 786432,
    "BTTOrder" : 106,
    "BTTAutoAdaptToKeyboardLayout" : 0,
    "BTTAdditionalActions" : [
      {
        "BTTLastUpdatedAt" : 1692025598.3964481,
        "BTTTriggerType" : -1,
        "BTTTriggerClass" : "BTTTriggerTypeOtherTriggers",
        "BTTPredefinedActionType" : 129,
        "BTTPredefinedActionName" : "Delay Next Action (blocking)",
        "BTTGenericActionConfig" : "0.35",
        "BTTDelayNextActionBy" : "0.35",
        "BTTUUID" : "DC3866C5-4573-429C-8287-4A0E5953D336",
        "BTTTriggerOnDown" : 1,
        "BTTEnabled" : 1,
        "BTTEnabled2" : 1
      },
      {
        "BTTLastUpdatedAt" : 1692025598.3969378,
        "BTTTriggerType" : 643,
        "BTTTriggerTypeDescription" : "Named Trigger: safari",
        "BTTTriggerClass" : "BTTTriggerTypeOtherTriggers",
        "BTTPredefinedActionType" : 153,
        "BTTPredefinedActionName" : "Move Mouse To Position",
        "BTTGenericActionConfig" : "{442, 55}",
        "BTTMoveMouseToPosition" : "{442, 55}",
        "BTTMoveMouseRelative" : "1",
        "BTTTriggerName" : "safari",
        "BTTUUID" : "2E0AD608-86E1-45BA-BA4A-F2FB8172B6A2",
        "BTTTriggerOnDown" : 1,
        "BTTEnabled" : 1,
        "BTTEnabled2" : 1,
        "BTTOrder" : 1,
        "BTTGestureNotes" : "Named Trigger: safari"
      },
      {
        "BTTLastUpdatedAt" : 1692025598.397604,
        "BTTTriggerType" : -1,
        "BTTTriggerClass" : "BTTTriggerTypeOtherTriggers",
        "BTTPredefinedActionType" : 3,
        "BTTPredefinedActionName" : "Left Click",
        "BTTUUID" : "4BBEE57F-5ECD-4349-8C69-0939455AC42D",
        "BTTTriggerOnDown" : 1,
        "BTTEnabled" : 1,
        "BTTEnabled2" : 1,
        "BTTOrder" : 2
      },
      {
        "BTTLastUpdatedAt" : 1692029026.4682851,
        "BTTTriggerType" : -1,
        "BTTTriggerClass" : "BTTTriggerTypeOtherTriggers",
        "BTTPredefinedActionType" : 172,
        "BTTPredefinedActionName" : "Run Apple Script (blocking)",
        "BTTAdditionalActionData" : {
          "BTTScriptType" : 0,
          "BTTAppleScriptRunInBackground" : false,
          "BTTTouchBarAppleScriptString" : "-- get selected text\rset savedPB to the clipboard\r\rdelay 0.3\r\r-- get browser URL\rtell application \"System Events\"\r\tkeystroke \"c\" using {command down}\r\tkey code 53\rend tell\r\r\rdelay 0.35 -- i read that this is necessary, you may try without\rset bURL to the clipboard\r\r-- combine them\r\rset the clipboard to savedPB & \" - \" & bURL",
          "BTTTouchBarAppleScriptUsePath" : false
        },
        "BTTGenericActionConfig" : "-- get selected text\rset savedPB to the clipboard\r\rdelay 0.3\r\r-- get browser URL\rtell application \"System Events\"\r\tkeystroke \"c\" using {command down}\r\tkey code 53\rend tell\r\r\rdelay 0.35 -- i read that this is necessary, you may try without\rset bURL to the clipboard\r\r-- combine them\r\rset the clipboard to savedPB & \" - \" & bURL",
        "BTTInlineAppleScript" : "-- get selected text\rset savedPB to the clipboard\r\rdelay 0.3\r\r-- get browser URL\rtell application \"System Events\"\r\tkeystroke \"c\" using {command down}\r\tkey code 53\rend tell\r\r\rdelay 0.35 -- i read that this is necessary, you may try without\rset bURL to the clipboard\r\r-- combine them\r\rset the clipboard to savedPB & \" - \" & bURL",
        "BTTUUID" : "74EA8E0A-20BB-44AA-8907-39ABE008E5FC",
        "BTTTriggerOnDown" : 1,
        "BTTEnabled" : 1,
        "BTTEnabled2" : 1,
        "BTTOrder" : 4
      }
    ]
  }
]

Can you explain your use case? Maybe this can be done in an easier way.

The delay function in Apple Script has been broken multiple times during the last 10 years. Usually one viable workaround was to use

do shell script "/bin/sleep 5"

instead of calling "delay 5"

I tried your suggestion. Same problem.

This is the use case:
This shortcut is a simple one for use saving web page text snippets w/ the URL in a browser. You go to a web page in Chrome, highlight some text on the page that you want to save, and then run the BTT shortcut/trigger. It is supposed to copy the text to the clipboard, move the mouse pointer to the search / URL box, click, select the URL, and copy the URL to the clipboard as well. It then sets the clipboard equal to the original text snippet plus the associated web page URL.

Instead of what is expected, I end up with only the text snippet and no URL, or the text snippet duplicated twice and no URL in the clipboard.