Open GC window in one .applescript file and work with it in another


I’m trying to open Google Chrome in my app and run a test suite on it.

I have two .applescript files in my project:


script AppDelegate
	property parent : class "NSObject"
	-- IBOutlets
	property theWindow : missing value
        property AnotherAppleScript: missing value
	on applicationWillFinishLaunching_(aNotification)
            set newWindow to (current application's AnotherAppleScript's launchGoogleChrome())
            tell application "Google Chrome"
                set windowID to id of newWindow
                set URL of (its window id windowID)'s first tab to ""
            end tell
	end applicationWillFinishLaunching_
	on applicationShouldTerminate_(sender)
		-- Insert code here to do any housekeeping before your application quits 
		return current application's NSTerminateNow
	end applicationShouldTerminate_
end script


script AnotherAppleScript
  property parent : class "NSObject"
  on launchGoogleChrome()
      tell application "Google Chrome"
          set newWindow to make new window with properties {mode:"normal"}
          return newWindow
      end tell
  end launchGoogleChrome
end script

I receive this error:
2018-12-13 14:11:44.963858+0400 Test[52350] *** -[AppDelegate applicationWillFinishLaunching:]: Can’t get id of «class ocid» id «data optr00000000C08B2F0000600000». (error -1728)


Everything is fine if I try to do it in one file.

You can’t return AppleScript objects from methods that are called by another object in an Xcode app. You’re effectively calling an Objective-C class method on an Objective-C class (AnotherAppleScript), and the method must return a Cocoa object.

Try passing the window’s id instead; the scripting bridge can then wrap that in an NSNumber. Just remember to coerce it back to an integer in the calling code.

Oh thank you very much, it works!