Using ScriptingBridge to access Finder windows changed in Mojave, need help

I need help with using the ScriptingBridge in my ObjC code.

I am scripting the Finder. In particular, I need to get the frontmost window’s path or URL.

I have created the Finder’s scripting header files using this cmd:

sdef /System/Library/CoreServices/Finder.app | sdp -fh --basename "Finder" --bundleid "com.apple.finder"

I then use this code to access the Finder’s windows, which works fine up until macOS 10.13.x:

FinderApplication *finder = [SBApplication applicationWithBundleIdentifier:@"com.apple.finder"];
SBElementArray *windows = [finder FinderWindows];
for (FinderWindow *window in windows) {
	NSString *className = NSStringFromClass(window.class);
	if ([className isEqualToString:@"FinderFinderWindow"]) {
		// now I can access the window's properties.

The problem is that in Mojave, I do not get objects of type FinderWindow any more. Instead, I get SBObject items. How do I turn them into FinderWindow objects?

Huh, I figured it out:

While 10.13 would actually give me the “proper” class, which I can then inspect in the Xcode debugger, 10.14 only gives us a proxy object (i.e. SBObject), which I can still cast to the expected type and then invoke its member functions.

So, all I have to do is remove the check for the className and blindly assume that I’m getting objects of type FinderWindow, it seems. I just hope I won’t get any other types that I don’t expect.

Also, found that if I get the target and then try to access its URL, this will leads to an ObjC exception if the Finder window shows the Root folder ("/"), at least in 10.13.6.

As a work-around, I use this code now:

for (FinderFinderWindow *window in windows) {
	FinderItem *target = [window.target get];
	if (!target) continue;
	NSDictionary *p = [target properties];
	if (!p) continue;
	NSString *url = p[@"URL"];
	if (url) return url; // we're done
}