How to Correctly Return an Object?

I have a stay open script application (named “Foo”) that uses the following handler:

on makeFinderWindow()
    tell application "Finder"       
        set bar to make new Finder window       
        return bar      
    end tell    
end makeFinderWindow

When I call this handler from another script it returns

Finder window id 47 of application "Foo"

to the calling script. Now if I try to do anything with that Finder window using handlers in the calling script, it won’t work because AppleScript thinks the Finder window belongs to the application “Foo” instead of the Finder.

How can I get “Foo” to return a ref to a Finder window which belongs to the Finder and not “Foo”?

I know this can be done by loading the script containing this handler as a script object in the calling script, but I’m interested in how this can be done with a stay open AppleScript app.

Any AppleScript geniuses out there know the answer to this one. I’ve been working on it for three days. :frowning:

Thanks

Hi,

What if you return the window id.

on makeFinderWindow()
tell application “Finder”
set bar to (make new Finder window)
return (id of bar)
end tell
end makeFinderWindow

Then you can use the id.

tell application “StayOpenLib”
set the_id to makeFinderWindow()
end tell
tell application “Finder”
position of window id the_id
end tell

gl,

Here’s another idea that uses a constructor function to return a script initialized with the window reference. This is the stay open script app:

on MakeFinderWindow()
tell application “Finder”
set win_ref to (make Finder window)
end tell
return MakeScript(win_ref)
end MakeFinderWindow

on MakeScript(window_ref)
script WindowRef
property the_ref : window_ref
end script
end MakeScript

I called this script StayOpenMakeScript2 and the next script uses its MakeFinderWindow() subroutine:

tell application “StayOpenMakeScript2”
set the_script to MakeFinderWindow()
end tell
the_ref of the_script

I just started fooling around with this idea so there is probably a better way to use this.

gl,

Thanks for the ideas! I’ll probably use the script object technique.

I knew there were some AppleScript geniuses out there. :slight_smile:


Brett

Hi Brett,

I’m not a genius but thanks for the compliment anyway. I must have read the AppleScriptLanguageGuide at least a hundred times and more for that section on script objects.

Anyway, I kind of like that script object idea also. You can make it generic like for returning lists of Finder references also and you’ll have only subroutines in your stay open script library.

gl,

I don’t see any “foo” window here, but only correct Finder window reference…


set theScript to load script alias "HARD_DISK:Users:123:Desktop:foo.app"

set finderWindow to makeFinderWindow() of theScript
-- result: Finder window id 2809