views:

349

answers:

1

I'm writing a standard Cocoa application, and I've just started implementing AppleScript support for it. I've defined and implemented a property called testprop for my top-level application scripting class, and accessing it works just fine: if I launch an instance of my app and run the following AppleScript in Script Editor, I get the output I expect:

tell application "MyApp"
    testprop
end tell

However if I run this very same AppleScript in the Script Editor when my app is not running, it returns the last known value for this property, and continues to return it for subsequent calls. I don't see an instance of my app getting started anywhere in the GUI.

After I noticed this, I ran "ps xawww | grep MyApp" in the shell, which told me that a process had been created using my app's main executable, with an argument that looks something like this: -psn_0_323663 (the number at the end changes each time this process is started -- I gather that it's the "process serial number" that AppleScript (among others) uses to keep track of and control processes).

What is going on here? How can I prevent this from happening (i.e. launch my app as a full, proper GUI-enabled instance when AppleScript "tell" commands for it are run)?

Edit:

The above seems to occur only on my laptop. When I try exactly the same thing on my Mac Mini (the OS version is the same on both: 10.5.8), I simply get an error message:

$ osascript -e "tell application \"MyApp\"" -e "testprop" -e "end tell"
26:40: execution error: The variable testprop is not defined. (-2753)
+2  A: 

I don't think it's running a "non-GUI" instance, it just hides the app. You could add the line "activate" to the applescript to get the app to become active, in which case you'll see the windows and menu.

Graham Lee
This was exactly what was happening; I forgot that I had pinned the app icon to the left of my dock and erroneously thought that it was running in some sort of "non-GUI" mode since I didn't see its icon appear on the right side of the dock. :) Also, the reason why it didn't work on the other computer was because I had an old "Debug" release compiled there (without the AppleScript support) that it was trying to contact.
hasseg
Yes, that thing about LaunchServices choosing the wrong version of the app to target is really annoying. Either remove all other builds of the app, or change the bundle ID during development so LS doesn't see the other builds.
Graham Lee