Using SD's external debugging with .scpt files in same script bundle

After spending several hours on trying to figure out how to use and debug external scripts in AppleScript bundle with Script Debugger (SD), I continue to have trouble. I have read through all of the following:
Using Script Debugger for libraries
Property values not being globally available
errOSAInternalTableOverflow Error: tips on Using Script Libraries
External debugging does not work
Script Debugger debugging-mode limitations
[Tutorial] Scripting Libraries
SD’s help page on debugging external libraries
And other posts

I am running MacOS 10.11.6 and SD 7.0.10 on an old iMac that I use for iTunes. I also tested on MacOS 10.14.6 on MBP 2016 and got same results.

Using SD, I created a new Apple Script Bundle with two files

main.scpt

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

-- The 4 statements below won't compile with main or Lib 1 saved in or out of debug mode:--
-- use Lib1 : script "Lib1" -- error "Can't get script "Lib1""  -1,728
-- use Lib1 : script "Lib1.scpt" -- error "Can't get script "Lib1.scpt""  -1,728
-- use Lib1 : script ((path to resource "Lib1.scpt" in directory "Scripts") as string) -- error "Resource not found" -192
-- property Lib1 : load script file ((path to resource "Lib1.scpt" in directory "Scripts") as string) -- error "Resource not found" -192

-- The following statement compiles and at runtime assigns value "document id "5204…"
-- but subsequently errors at /set Lib1's pNumber to 1/ in run handler unless /set Lib1 to load script file …/ is used to overwrite Lib1
-- with error "Can't make pNumber of document id … into type reference. -1,700
-- property Lib1 : document "Lib1.scpt" -- assigns value "document id "5204…"

-- this entire handler executes as expected, if Lib1.scpt is saved in not-debug mode
on run
	-- the following load statement works, when "Lib1.scpt" is saved in not-debug mode
	-- it does not load the script into Lib1, when "Lib1.scpt" is saved in debug mode
	set Lib1 to load script file ((path to resource "Lib1.scpt" in directory "Scripts") as string)
	
	-- the following line will compile but errors out with "Can't get the document…" -1,728, whether or not Lib1.scpt is saved in debug mode
	-- set Lib1 to document ((path to resource "Lib1.scpt" in directory "Scripts") as string)
	
	-- when Lib1.scpt is saved in debug mode and the /set Lib1 to load script file …/ above is used, 
	-- the next statement produces "AppleScript Execution Error" "Stack overflow." -2,706
	set Lib1's pNumber to 1
	
	-- when Lib1.scpt is saved in debug mode and /set Lib1's pNumber to 1/ immediately above is commented out, 
	-- the next statement produces "AppleScript Execution Error" "Can’t make pNumber of «data scpt4D617259332E3030…" -1,700	
	display alert "main.scpt" message "Lib1's pNumber = " & Lib1's pNumber buttons {"Cancel", "OK"} cancel button 1
	set Lib1's pText to "set L1's pText"
	
	Lib1's aHandler()
	set Lib1's pNumber to (Lib1's pNumber) + 1
	Lib1's aHandler()
end run

Lib1.scpt

property pText : missing value
property pNumber : 1
on aHandler()
    	display alert theDialog message "Lib1's pNumber = " & pNumber buttons {"Cancel", "OK"} cancel button 1
end aHandler

I don’t know why the “use” statement is not working. If I can’t get help on this, I can live with the work around of set Lib1 to load the script file …

I don’t know why I can’t use the code described in the below post to debug the library.
Using Script Debugger for libraries

I also get that Shane and others don’t like to call other libraries. I just want to get this application out. It looks like I need to break up the 2,200 lines of code to use SD. Debugging was so tedious in Script Editor. There are some other reasons that I would like to break up the code too.

Thanks,
Jonathan

Here is what works for me.

In ~/Library/Script Libraries I have 2 Library Scripts, Utilities and Loqqing. These were originally saved as script bundles, but I have discovered that a script also works. I am careful to compile and save the Library Scripts with Script Editor, not Script Debugger, to avoid any Script Debugger dependencies in the Script Library files.

It is important prevent a circular call sequence sequence between Library Scripts.
–Handlers in Utilities do not use any other Script Library, and and do not refer back to objects the main Script
–Handlers in Loqqing use some scripts in Utilities, and also refer back to some variables and handlers in the main script

The beginning of Utilities script has no special content.
The beginning of Loqqing script has this:

use AppleScript version “2.5”
use scripting additions
use U : script “Utilities_1215”
property M : missing value

(sorry, can’t seem to find the function to quote code in this poorly documented editor)

The beginning of the main script has this code

use AppleScript version “2.5”
use scripting additions
use U : script “Utilities_1215”
use L : script “Loqqing_1221”
set L’s M to me

A handler in Loqqing may refer to
U’s joinListToString()
(get name of M)
M’s finalCleanup()
M’s Loqqing’s debugLogLevel

Neither Script Editor nor Script Debugger offer any useful debugging of a Library Script.

The best proceedure I have found is to copy the Library Script to the end of the main script, and
make a some simple changes.

For a Script like Utilities, all that is necessary is to comment out this line at the top of the main script
use U : script “Utilities_1215”
and add the line
set U to me

For a Script like Loqqing, comment out these lines at the top of the main script
use L : script “Loqqing_1221”
set L’s M to me

and add the lines
property M : missing value
set M to me
set L to me

At the beginning of the copied Library script Loqqing, comment out these lines as they are now redundant and cause errors
use AppleScript version “2.5”
use scripting additions
use U : script “Utilities_1215”
property M : missing value

Eric,
Thanks for the thoughtful reply concerning loading scripts located in Library and appending them to another script. However, your post does not appear to address my issues of loading scripts in a script bundle and debugging them using SD. (Did I miss something?) As you can see from the code I posted, the use command is not working to load bundle scripts. Thanks anyway though.

You’re not putting them in the correct folder, by the look of things. They need to be in the bundle’s Contents/Resources/Script Libraries folder, which you will have to create.

1 Like

LOL. They were in the folder “Contents/Resources/Scripts”. I created and put Lib1 in “Script Libraries”. Use works great now. However, I can’t get debugging features to work. I saved both scripts in debug mode. I tried setting breaks. No dice.

I suspect you’re asking too much of external debugging.

@ShaneStanley I know you don’t like external libraries. :slight_smile:

I tried Jim Underwood’s @JMichaelTX use of

property Lib1 : document "Lib1.scpt" 

as described in Using Script Debugger for libraries

But this still didn’t allow debugging.

Hi Jonathan

I relaize that my solution doesn’t precisely apply to your problem.

However the problems are very similar as I am working on a Script Library that is stored in a fixed location MacOS folder, and you are working on a a Script Library that is stored in as script bundle.

I mentioned several techniques that should be applicable to your problem as well

  1. Compile the Script Library with Script Editor; I have found that compiling a script with Script Debugger (especially in Debug mode) leads to a dependency on Script Debugger to be running when the the script or handler in it runs - not something you want in a Library script

  2. I have tried the debugging technique described by Sane Stanley here, but found it awkward if I was transferring any information between the Main and Library scripts as global variables.

  3. A workable manner of debugging with Script Debuggers full features set - for debugging purposes copy the script’s handlers to the main script; debug with Script Debugger and then copy the working code back to the script file. Changing only the USE stament as I showed means the code does not have to be modified when copying back to the handler.

@EricNepean. Yes. I agree and concede that SD is not a seamless solution for debugging. Your tips are very helpful. I will switch to this approach.

I’m still having trouble getting globals to work between libraries. I’ll spend a bit more time on that.

The benefit to all this is that I have a clearer idea how to use preferences, globals, and handler parameters that should help with code maintenance. I’ll focus refactoring for management of parameters. I hope there is not a big performance hit to pass parameters versus using globals.