BUG REPORT: KM Fails with Script That uses BridgePlus Script Library

BUG REPORT

Running Keyboard Maestro 9.1 on macOS 10.14.6 (Mojave)

@peternlewis, the following script runs fine from both FastScripts and Script Debugger 7.
But when run from a KM Execute AppleScript, with either a script file or script text, I get this error:

/var/folders/hb/6xgg0y8j4g530m81rd1f9mpc0000gp/T/Keyboard-Maestro-Script-B3C7577B-D5D6-4B32-BDED-15DB9BDBEC60:2014:2039: execution error: The bundle “BridgePlus” couldn’t be loaded. (-4960)

/Users/Shared/Dropbox/SW/DEV/LINKED_FOLDERS/Scripts/@KM -- Select KM Macro Created Last.scpt: execution error: The bundle “BridgePlus” couldn’t be loaded. (-4960)

property ptyScriptName : "Select Macro Created Last"
property ptyScriptVer : "1.0"
property ptyScriptDate : "2020-11-23"
property ptyScriptAuthor : "JMichaelTX"

use AppleScript version "2.5"
use scripting additions
use framework "Foundation" -- Required for all ASObjC commands
use BPLib : script "BridgePlus" -- required for sortMultiLists() handler

(*
REQUIRES:
  use framework "Foundation"
  use BPLib : script "BridgePlus"
  
  REF:  BridgePlus 1.3.3 Script Library, Shane Stanley
          https://latenightsw.com/support/freeware/
*)
--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


tell application "Keyboard Maestro"
  
  activate
  
  set {macroUUIDList, macroCreationDateList} to {id, creation date} of every macro
  set {macroCreationDateList, macroUUIDList} to my sortMultiLists({macroCreationDateList, macroUUIDList}, {"DESC", "ASC"})
  
  set macroUUID to (item 1 of macroUUIDList)
  set oMacro to macro id macroUUID
  select oMacro
  set macroName to name of oMacro
  
  set msgStr to "Macro: " & macroName
  display notification msgStr with title ptyScriptName sound name "Tink"
  return macroName
  
end tell

--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
on sortMultiLists(pListOfLists, pSortDirList) -- @Lists @Sort @BridgePlus @ASObjC
  --–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
  (*  VER: 1.0    2020-11-23
    PURPOSE:  
    PARAMETERS:
      • pListOfLists    | list |  List that contains two or more lists to be sorted
                                                    • Lists will be sorted by each sub-list in the order entered.
      • pSortDirList        | list |    List of text items indicating the sort direction for each list
                                                    • Must be "ASC" (for ascending) or
                                                    • "DESC" (for descending)

    RETURNS:  List of Lists in Sorted Order

    AUTHOR:  JMichaelTX
  REQUIRES:
    use framework "Foundation"
    use BPLib : script "BridgePlus"
  
    REF:  BridgePlus 1.3.3 Script Library, Shane Stanley
            https://latenightsw.com/support/freeware/
  --–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
  *)
  local rowsList, listCount, sortByList, iL, oSort
  
  --- Setup the Sort ---
  set rowsList to BPLib's colsToRowsIn:pListOfLists
  
  set listCount to count of pListOfLists
  set sortByList to {}
  
  --- Sort Order by List as Passed in pListOfLists ---
  
  repeat with iL from 1 to listCount
    set end of sortByList to iL
  end repeat
  
  --- Convert Text Sort Direction to Boolean ---
  --   (true means ascending)
  
  repeat with oSort in pSortDirList
    set contents of oSort to ((oSort as text) starts with "ASC")
  end repeat
  
  --- Do the Sort
  set rowsList to BPLib's sublistsIn:rowsList sortedByIndexes:sortByList ascending:pSortDirList sortTypes:{}
  
  --- Get Sort Results ---
  set pListOfLists to BPLib's colsToRowsIn:rowsList
  
  return pListOfLists
end sortMultiLists
--~~~~~~~~~~~~~~~~~~~~ END of Handler ~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Oddly enough this macro runs fine on my macOS 10.12.6 system...

-Chris

Sounds like this:

Error -4960 is "unknown error" so that isn't very useful.

Does it work in Script Editor? I'm guessing maybe not, given the comment in that topic “which can no longer load third-party frameworks”.

It might be because Keyboard Maestro is has hardened runtime enabled, and maybe BridgePlus doesn’t, or something like that…?

You are correct. I get the same error in Script Editor.

I wonder why it runs fine in Script Debugger 7 and FastScripts?

Looks like it is because osascript fails with this.
One more reason to change KM to NOT us osascript, to use the method the FastScripts uses.
(BTW, I never get any AppleScript crashes).

I cannot do this. AppleScript is single threaded, AppleScripts would block the Keyboard Maestro Engine from all other facilities while it was stuck processing an AppleScript.

FYI

@JMichaelTX's BridgePlus AppleScript macro runs fine on Mojave if SIP is turned OFF.

As do AppleScript requiring AppleScript Osaxen.

Not that any "rational" person would want to leave SIP turned off.

-Chris

Are they running the hardened runtime? That would be one possibility. A possible test for that would be trying it in a version of Keyboard Maestro before 9.0, which was the first to use the hardened runtime.

I believe you can check whether a signed bundle is hardened or not with:

codesign -v -d "/Applications/Keyboard Maestro 8.4.2.app"

If it shows flags=0x0(none) it is not hardened. If it shows flags=0x10000(runtime) it is hardened.

My guess is those other apps are not hardened, and the BridgePlus plugin is not hardened, and that resolving the former would cause the issue for those apps, and resolving the latter would solve the issue for Keyboard Maestro.

If this is accurate, then “One more reason to change KM to NOT us osascript” would also be incorrect because it still would not work.

Although in my test, /usr/bin/osascript is not hardened, so it may be that osascript is inheriting the hardened status.

But you implied osascript is also failing, so maybe my theory is entirely incorrect.