Applescript is inexplicably failing in Big Sur EDIT: I've made a much simpler script that exhibits the same problem. The previous script is moved below.
I've been using TextExpander to run this script and it recently started failing after updating to Big Sur. Thinking it was related to TextExpander, I created a similar shortcut to run it in BetterTouchTool but am having the same issue. It's not throwing a visible error, it just plays the error tone when it finishes and doesn't output anything. What's odd is that when I run it from a script editor, it performs just fine. It's only when its being called from another process that it fails.
The basic idea of the script is to take raw number input in a dialog box and output formatted time code in the frontmost app. For example, entering &quot;207&quot; outputs &quot;01:00:02:07&quot;
I've scoured the code and can't find what would possibly be causing the error since it's all very basic text manipulation. Any thoughts would be greatly helpful.
(The &quot;System Events&quot; block is to keep from bringing TextExpander to the front when the &quot;Display Dialog&quot; pops up.)
tell application &quot;System Events&quot;
 set frontProcessName to name of first process whose frontmost is true

 set test_text to text returned of (display dialog &quot;Text?&quot; default answer &quot;&quot;)

tell application frontProcessName to activate
 end tell

 return &quot;test_text&quot;

Here's the original script:
tell application &quot;System Events&quot;
--Set process that called text expander aside so it can be returned to the front
set frontProcessName to name of item 1 of (processes whose frontmost is true)

-- Get the raw numbers from the user
set raw_timecode to text returned of (display dialog &quot;Timecode&quot; default answer &quot;&quot;)

-- Set the default variables
set user_timecode to &quot;&quot; as string
set rt_length to the length of raw_timecode

--Check to see if the TC field is blank
if raw_timecode = &quot;&quot; then
    set raw_timecode to &quot;01000000&quot;
end if

--Parse the user supplied numbers and replace any &quot;.&quot; with &quot;00&quot;
repeat with n from 1 to rt_length
    if character n of raw_timecode = &quot;.&quot; then
        set user_timecode to user_timecode &amp; &quot;00&quot; as text
    else
        set user_timecode to user_timecode &amp; character n of raw_timecode as text
    end if
    
end repeat

--Set to 00:00:00:00 if only digit is 0 
if user_timecode = &quot;0&quot; then
    set base_timecode to &quot;00000000&quot;
else
    set base_timecode to &quot;01000000&quot;
end if

set x to the length of user_timecode


-- Trim extra digits off base timecode
if x = 8 and user_timecode ≠ &quot;0&quot; then
    set raw_timecode to user_timecode as string
else
    repeat while (length of base_timecode) + (length of user_timecode) &gt; 8
        try
            set base_timecode to characters 1 thru -(x + 1) of base_timecode as string
            set raw_timecode to base_timecode &amp; user_timecode as string
        on error
            display dialog &quot;Invalid timecode&quot;
            error number -128
        end try
    end repeat
end if
set new_timecode to characters 1 thru 2 of raw_timecode &amp; &quot;:&quot; &amp; characters 3 thru 4 of raw_timecode &amp; &quot;:&quot; &amp; characters 5 thru 6 of raw_timecode &amp; &quot;:&quot; &amp; characters 7 thru 8 of raw_timecode as text

end tell

-- Return the previous app that called text expander to the front
tell application frontProcessName to activate
delay 0.5

return new_timecode
I don’t have TextExpander or BetterTouchTool so I could not test exactly how you needed it to be tested.  However, I made a few small tweaks to your code and I saved the script to the Script Menu Bar and ran it from there several times while working in several different applications. I got no errors on my end.
If your only reason for using ”System Events” is to make sure the application you were working in at that moment, when running your code, remains front most after your dialog window disappears… then all you need to do is insert an activate command before your display dialog command.  This will bring the dialog window to the front then when it gets dismissed, the current application you were working in remains front most.  If so, using ”System Events” is not necessary at all.
-- Get the raw numbers from the user
activate
set raw_timecode to text returned of ¬
    (display dialog &quot;Timecode&quot; default answer &quot;&quot;)

-- Set the default variables
set user_timecode to &quot;&quot; as string
set rt_length to the length of raw_timecode

--Check to see if the TC field is blank
if raw_timecode = &quot;&quot; then set raw_timecode to &quot;01000000&quot;

--Parse the user supplied numbers and replace any &quot;.&quot; with &quot;00&quot;
repeat with n from 1 to rt_length
    if character n of raw_timecode = &quot;.&quot; then
        set user_timecode to user_timecode &amp; &quot;00&quot; as text
    else
        set user_timecode to user_timecode &amp; character n of raw_timecode as text
    end if
end repeat

--Set to 00:00:00:00 if only digit is 0 
if user_timecode = &quot;0&quot; then
    set base_timecode to &quot;00000000&quot;
else
    set base_timecode to &quot;01000000&quot;
end if

set x to the length of user_timecode

-- Trim extra digits off base timecode
if x = 8 and user_timecode ≠ &quot;0&quot; then
    set raw_timecode to user_timecode as string
else
    repeat while (length of base_timecode) + (length of user_timecode) &gt; 8
        try
            set base_timecode to characters 1 thru -(x + 1) of base_timecode as string
            set raw_timecode to base_timecode &amp; user_timecode as string
        on error
            display dialog &quot;Invalid timecode&quot;
            error number -128
        end try
    end repeat
end if
set new_timecode to characters 1 thru 2 of raw_timecode &amp; &quot;:&quot; &amp; ¬
    characters 3 thru 4 of raw_timecode &amp; &quot;:&quot; &amp; characters 5 thru 6 of ¬
    raw_timecode &amp; &quot;:&quot; &amp; characters 7 thru 8 of raw_timecode as text

--set the clipboard to new_timecode
activate
display dialog new_timecode buttons {&quot;OK&quot;} giving up after 6
return new_timecode