LSOF Shell command not running in macOS Sandbox

I have found a solution that works using NSUserUnixTaskMBS. It will run any Script shell (MBS have a similar class for running AppleScripts too). It runs these outside of the App Sandbox, meaning you can break the normal rules.

Note that: your Shell Script must have the extension .sh, must be set with executable bit on via chmod +x, must have the #!/bin/bash shebang prefix and linefeeds for EndOfLine.

You have to manually place your Script file (myScript.sh) into the special read-only (to your app) Application Scripts (user drag or Open Folder). Set the output to a text file (normally not on Desktop!) — I had to send the output to a text file as I was unable to return the text Results in the executeFinished Event. Then Execute the Script (the cog icon will spin in the menu bar). I have created a ClassNSUserUnixTaskMBS that sets a Boolean isFinished to True when Event executeFinished runs (you could put your extra action code here instead).

Here is the code:

If Not NSUserScriptTaskMBS.Available Then
  Return
End If

'Var f As FolderItem = NSUserUnixTaskMBS.ScriptFolder 'this should work, but always returns Nil
Var f As FolderItem = SpecialFolder.UserLibrary.Child("Application Scripts").Child(NSBundleMBS.mainBundle.bundleIdentifier).Child("myScript.sh") 'it needs executable bit on ie chmod +x /pathtofile/
If f = Nil Or Not f.Exists Then
  Return
End If

Var tempNSErrorMBS As NSErrorMBS
Var tempNSUserUnixTaskMBS As ClassNSUserUnixTaskMBS

Try
  tempNSUserUnixTaskMBS = New ClassNSUserUnixTaskMBS(f, tempNSErrorMBS)
  
Catch Error
  Var ErrorMessage As String = Error.Message
  Return
End Try

f = SpecialFolder.Desktop.Child("myResults.txt") 'the output file must exist
f.Remove
If f <> Nil And Not f.Exists Then
  Var tempTextOutputStream As TextOutputStream = TextOutputStream.Create(f)
  
  If tempTextOutputStream <> Nil Then
    tempTextOutputStream.Close
  End If
End If
Var tempNSFileHandleMBS As NSFileHandleMBS = NSFileHandleMBS.fileHandleForWritingToFile(f, tempNSErrorMBS)

If tempNSErrorMBS <> Nil Then
  Var ErrorMessage As String = tempNSErrorMBS.Description
  Return
End If

tempNSUserUnixTaskMBS.standardOutput = tempNSFileHandleMBS
tempNSUserUnixTaskMBS.execute

Do
  DelayMBS 3.0
  Thread.YieldToNext
Loop Until tempNSUserUnixTaskMBS.isFinished