0

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 "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.

(The "System Events" block is to keep from bringing TextExpander to the front when the "Display Dialog" pops up.)

tell application "System Events"
 set frontProcessName to name of first process whose frontmost is true

set test_text to text returned of (display dialog "Text?" default answer "")

tell application frontProcessName to activate end tell

return "test_text"

Here's the original script:

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
  • A thought, but not a fix as I don't have Big Sur to test on, is that it's a permissions error - especially as the code is tested working from Script editor. – Tetsujin Jan 29 '21 at 19:43
  • What permissions should be adjusted? I've tried expanding the text into BBEdit, TextEdit, Chrome, and Safari and I've tried using TextExpander and BetterTouchTool as the script processor. It fails the same way in all variations. – Dennis Jan 29 '21 at 19:50
  • That's the bit I don't know, sorry. Catalina & Big Sur really expanded the nanny… ermm… consumer ;) protection areas & I don't have a Mac with either of those OSes. – Tetsujin Jan 29 '21 at 19:52
  • You might want to contact the TextExpander and BTT developers for more info. Also, check System Preferences > Security & Privacy > Privacy > Automation pane and see if adding TextExpander and BTT works. I don't run Big Sur, so I don't know if this System Preference has changed. – IconDaemon Jan 29 '21 at 20:28
  • I do not have TextExpander so can't test it fo you, however, when I have AppleScript code run by a Service/Quick Action and can always see exactly where if fails or the error is nondescript I add debugging code to the script. As an example, I'll sprinkle e.g. tell current application to say "foobar 1" and increment the number through out so I know how far it's getting. Sometimes I use a display dialog, etc. That said, the fact it works in Script Editor and not however else you are triggering the script sound like a permissions issue. – user3439894 Jan 29 '21 at 20:42
  • Also, why are you using set frontProcessName to name of item 1 of (processes whose frontmost is true) when set frontProcessName to name of first process whose frontmost is true gets it? – user3439894 Jan 29 '21 at 20:42
  • I'll modify the frontmost process line, thanks!

    Both TextExpander and BetterTouchTool are already in the Automation pane and have been so that's not the issue. Also, I seem to be getting stuck right at the first Display Dialog (which is only the second step) so a debug there won't really help me.

    I'll try running repair permissions and see if that helps anything.

    – Dennis Jan 29 '21 at 20:51
  • Repairing permissions was unsuccessful as it only repairs the home folder in Big Sur and all the apps in question live on the read only system drive. Does anyone have textexpander and can they try running my script? – Dennis Jan 29 '21 at 21:10
  • The only code that needs to be within the tell application "System Events" block is set frontProcessName to name of first process whose frontmost is true. Remove the rest of the code within the tell application "System Events" block to outside of it, and see if that helps. – user3439894 Jan 29 '21 at 21:11
  • No luck. TextExpander didn't hang up for a couple of seconds on the Display Dialog, but it still replied with an error tone, no text output, and no error message. BetterTouchTool brought itself to the front for the Display Dialog instead of leaving me in BBEdit and it, too, gave an error tone, no output, and no error message. – Dennis Jan 29 '21 at 21:25
  • I've made a much simpler version of the script that exhibits the exact same problem. Feel free to weigh in with any new suggestions. – Dennis Jan 30 '21 at 03:22
  • Try the line in this script - https://apple.stackexchange.com/a/411652/85275 I still don't know how/why it works, but it lets you give perms to System Events – Tetsujin Jan 30 '21 at 07:40
  • Give System Events Full Disk Access. ("Have you given it FDA?" is the new "Have you Repaired Permissions?") – benwiggy Jan 30 '21 at 09:37
  • @benwiggy - you can add full disk perms that way. but you can't add Automation, hence the line in the script i linked, which will trigger it. – Tetsujin Jan 30 '21 at 17:15

1 Answers1

0

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 "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"

--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

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

enter image description here

wch1zpink
  • 7,571
  • I had a couple of scripts and applets which broke after upgrading to Big Sur. I had to go back and re-grant privileges for ”System Events” and a few other apps in System Preferences. I suspect that is the cause of your issue. – wch1zpink Jan 30 '21 at 05:11
  • Unfortunately, calling the script from TextExpander still brings TextExpander to the front instead of keeping BBEdit the frontmost app, resulting in the data not filling in where it needs to. – Dennis Feb 02 '21 at 02:10
  • Thanks, I'm gonna try removing and re-adding apps that need system evens to see if that works. – Dennis Feb 02 '21 at 02:12