Hi,
I encountered some issues when loading libraries and I wonder if anyone here can help me understand what happened. I’ll explain with a few scripts.
this is the library code. Simplified for clarity. This is saved as a script bundle in the ~/Library/Script Libraries/ folder.
use AppleScript version "2.5"
use framework "Foundation"
use scripting additions
property testProperty : 1
property NSCurrentApplication : a reference to current application
property NSURLreference : a reference to NSCurrentApplication's class "NSURL"
on NSURL_fileURLWithPath(finderobject)
try
NSURLreference's fileURLWithPath:finderobject
on error errorText number errornumber partial result errorResults from errorObject to errorExpectedType --Unhandled errors in the handler are caught here.
error "NSURL_fileURLWithPath handler" & errorText number errornumber partial result errorResults from errorObject to errorExpectedType --Declare the library's handler as the source of the error.
end try
end NSURL_fileURLWithPath
This script loads that library and can demonstrate the error I was getting.
use AppleScript version "2.5"
use framework "Foundation"
--property testProperty : 1
property NSCurrentApplication : a reference to current application
property NSURLreference : a reference to NSCurrentApplication's class "NSURL"
use testLibrary : script "testLibrary"
property NSURL_fileURLWithPath : testLibrary's NSURL_fileURLWithPath
NSURL_fileURLWithPath("/Users/paulskinner/Desktop/targetFolder")
Running the test script results in an error:
NSURL_fileURLWithPath handler*** -[BAGenericObjectNoDeleteOSAID fileURLWithPath:]: unrecognized selector sent to object <BAGenericObjectNoDeleteOSAID @0x600000de67e0: OSAID(2) ComponentInstance(0x85007a)>
Weird things that I discovered while testing:
-
Un-commenting the testProperty line ( “–property testProperty : 1” ) allows this script to correctly return an NSURL. If ALL properties in the two scripts are matched by properties in the other script this somehow allows proper function.
-
Using the loaded handler directly does not result in an error. This only occurs when setting a property to the handler and then calling that property.
-
properties required in the loaded handler must be present in the script that uses the loaded library.
-
Moving the loading of the library to above the property declarations resolves this issue.
I suppose that this is all as expected, but I am unclear on the scoping issues.
Questions I’m left with :
-
Why does this script succeed when all properties are present in both scripts even when some of those properties are unused?
-
Why does loading the handler into a property CRUCIALLY affect the requirements for calling this handler? Why does this script succeed when the handler is called directly and fail when the property holding the handler is called?
If anyone is still around at this point I’ll explain that this isn’t just me futzing around with a strange construct looking for trouble. I have multiple libraries that successfully utilize this construct and found only this particular function ( “fileURLWithPath:finderobject” ) causes this strange error code to bubble up from the depths of ASObjC.