0

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 "207" outputs "01:00:02:07"

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.

tell application "System Events"
--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 "Timecode" default answer "")

-- Set the default variables
set user_timecode to "" as string
set rt_length to the length of raw_timecode

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

--Parse the user supplied numbers and replace any "." with "00"
repeat with n from 1 to rt_length
    if character n of raw_timecode = "." then
        set user_timecode to user_timecode & "00" as text
    else
        set user_timecode to user_timecode & 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 = "0" then
    set base_timecode to "00000000"
else
    set base_timecode to "01000000"
end if

set x to the length of user_timecode


-- Trim extra digits off base timecode
if x = 8 and user_timecode ≠ "0" then
    set raw_timecode to user_timecode as string
else
    repeat while (length of base_timecode) + (length of user_timecode) > 8
        try
            set base_timecode to characters 1 thru -(x + 1) of base_timecode as string
            set raw_timecode to base_timecode & user_timecode as string
        on error
            display dialog "Invalid timecode"
            error number -128
        end try
    end repeat
end if
set new_timecode to characters 1 thru 2 of raw_timecode & ":" & characters 3 thru 4 of raw_timecode & ":" & characters 5 thru 6 of raw_timecode & ":" & 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
Dennis
  • 1
  • It would help if you have some logging in your code, to see exactly where it is failing. What I normally do is I have a debug log before each chunk of operation, written to a file, which I tail, so I have visibility of what's going on with all my applescripts. – user3579815 Jan 28 '21 at 03:22
  • It also may be beneficial if you post this issue in [Ask Different](https://apple.stackexchange.com) where the Mac experts are. – karolus Jan 28 '21 at 04:04
  • You are using `System Events` - have these other applications been given accessibility permissions? – red_menace Jan 28 '21 at 15:13
  • red_menace - TextExpander (the one running the script) has accessibility permissions and I've tested it with BBEdit with accessibility permissions and still no good. user3579815 - The script seems to hit a problem with Display Dialog, refusing to accept input. Seeing as that's the second step, there's nothing that should be causing an error there. Also, as I said, I ran it inside a script editor and it runs just fine. It's when it gets called from another app that it fails. And it's only manipulating text. It's very easy to follow. – Dennis Jan 29 '21 at 19:32

0 Answers0