OSAScriptView/OSAScriptController

Hey Community,
I’m working at a project through ApplescriptObjC that will allow the user to choose certain interface option and preferences and then will write the Applescript for them. Rather than passing the code messily over to Applescript Editor, I decided I might as well just us an OSAScriptView and the OSAScriptController to fake my own Applescript Editor where i could control things a little better. The OSAScriptView inherits from NSTextView, so I can get around some of my problems there, the only issue is that there is virtually NO documentation on the OSAScriptController. I have everything compiling and running just fine, but there are a few things that I’m still lacking. I need to know whether or not the Controller is running a script so that I can enable/disable the run and stop buttons. After a lucky guess, I’ve found that “isRunning” is a viable function with the OSAScriptController, but I can’t exactly keep a function running that’s constantly checking to see if the OSAScriptController is running. Is there a way to specify a selector for when the OSAScriptController finishes running? Furthermore, does anyone know where I can find any documentation on it? Thank you,

~Josh

I doubt you’ll find any documentation for it. I started out using it for AppleScriptObjC Explorer, but gave up because it’s buggy (undo-ing through a compile was disastrous). I switched to subclassing NSTextView – more code, but a better result, IMO.

FWIW…

Subclassing the NSTextView?! So then all of the code for compiling and coloring the text is your own?! I guess having the run and stop buttons always enabled doesn’t seem too bad anymore…

No, it’s not that bad. You get the contents and make an OSAScript using alloc and initWithSource:, compile it using compileAndReturnError:, then call richTextSource:, which returns the compiled script as an NSAttributedString. Then shove that into the text view.

Oh wow, thanks! I had thought about using NSApplescript but I was unaware of “richTextSource,” so I had no way of getting the reformatted text back to me. So I guess now I don’t even really need to use an OSAScriptView anymore, just a plain old NSTextView should work just fine now, right? I really cannot thank you enough, this is going to make this project and any one where I need this in the future a million times easier! Thanks again!

~Josh

Actually, one more question. I’m looking through the NSApplescript class reference and I don’t see anything on stopping a running script. Do you know what the function would be for that? Thanks again,

~Josh

Actually you can use NSAppleScript instead of OSAScript; I needed OSAScript for something else. NSAppleScript has richTextSource, but it’s in a category so the documentation is separate.

Either way, there’s no way of stopping once you tell a script to execute.

Actually, come to think of it, you could just run the Applescript code in Applescript with “run script” as opposed to using “OSAScript” or NSApplseScript in Objective-C. Wouldn’t that also make things easier–running the Applescript in its native language? Of course, you’d need a string, so you would have to reconvert the compiled script back first…

So then the only way to stop it would be to run it through the OSAScriptController? Geez, that’s obnoxious. How’d you accomplish it in your AppleScriptObjC Explorer?

it depends where you’re getting it from. And run script won’t return an error dictionary that gives you the info you need to highlight where errors occur.

I don’t, except when logging – and that involves some serious jiggery-pokery.

I’ve never really thought about how to stop a script…

Ahh, I’d forgotten about that. So I guess I’ll be sticking with NSAppleScript for this one. Well thank you so much for all the help, once again, I can’t thank you enough. If you ever have any epiphanies on stopping scripts, be sure to share ;). Thanks again,

~Josh

Not an epiphany, but I suspect you have to send the script statement-by-statement, using AESend. But something like this might work: use OSAScript’s scriptDataDescriptorWithContentsOfURL: to get the script as a single NSAppleEventDescriptor, step through the constituent descriptors using descriptorAtIndex:, and then somehow convince OSAScript to run them, perhaps using initWithScriptDataDescriptor:::: or maybe executeAppleEvent::.

Good luck – let me know how you get on.

Ok, I’ll be sure to give that a try! Could you just point me in the direction of the OSAScript documentation? I can’t seem to find it anywhere, I’m only seeing the bash function…

There is none that I know of, other than the header files.

But I had a look at some code, and I’m not sure you’re going to get anywhere. The problem is that for AS to be run as ASObjC, it needs to be run from an ASObjC class. In other words, it needs to be passed to an AS class as a script object, or loaded using load script. Once that class has it, it can only run it by calling its handlers (or run/open/etc). If it runs it any other way, like telling NSAppleScript or OSAScript to execute it, it just runs as normal AppleScript, without ASObjC’s magical powers.

The header files…is that where you’re finding these functions?

That’s a shame. I guess I’ll just stick with a stop button that’s always enabled, which is not as bad as it could be. Thanks again for all the help!

Yep.