Scriptable Application Problems

Hello all!

I followed the directions in this post to make an ASOC application scriptable. I used Sdef Editor to make the Sdef.

All I had in the dictionary (beside the standard suite) was an application class and a property called “theText”.
I changed the

application_delegateHandlesKey_(sender, theKey)

to handle a key of theText. The script was

 on application_delegateHandlesKey_(sender, theKey)
        if theKey = "theText" then
            return true
        end if
        return false
    end application_delegateHandlesKey_

When I try to run this in AppleScript Editor:

tell application "TestApp" to set p to theText

I get the following error:

error “TestApp got an error: AppleEvent handler failed.” number -10000

The Editor can read the dictionary, so I know that it is not a dictionary problem.

Any suggestions?

The code from the Sdef (not including the Standard Suite):

<suite name="Da Suite" code="daSu" description="This is a suite that DOES NOT WORK FOR SOME REASON!!!"> <class name="Application Class" code="appC" inherits="application"> <cocoa class="NSApplication"/> <property name="theText" code="theT" description="some text." type="text"> <cocoa key="theText"/> </property> </class> </suite> </dictionary>

If type = “text”, you shouldn’t be returning a boolean.

Well, that didn’t work either. This is what I have in the App Delegate:

  property theProp : "Hello"
    on application_delegateHandlesKey_(sender, theKey)
        if theKey = "theText" then
            return theProp
        end if
        return "error"
    end application_delegateHandlesKey_

This gets the same error. :frowning:

I haven’t had much success with the inherit business in sdefs. Try putting the property in a standard app definition:

<class name="application" code="capp" description="Our simple application class.">
		<cocoa class="NSApplication"/>

Nope, it didn’t work.

Well, thats the thing. Look at this tutorial. They return more than just a boolean, but in the application delegateHandlesKey they return a boolean.

Just when all this seemed to be possible… :slight_smile:

Are you restarting your script editor after making changes? This stuff gets cached, which can make trouble-shooting a bit tricky.

Yes, I did. And it didn’t work.

What I am having trouble understanding is the tutorial mentioned in my last post. It’s in Objective-C, but look at the application delegateHandlesKey section. They return a boolean even though the information is more than a boolean.

I will keep trying different things.

Edit: Here is the Developer Documentation for this.

I think the problem is a simple one:

if theKey = “theText” then

should be:

if theKey as string = “theText” then

I pasted your code into a new app, and this change made it work. I used the code you posted at the top (returning true is correct).

Ric

When you said it worked, what did it return in AppleScript Editor? I don’t know how it would get the variable I want it to return.

I did this at the top of my app

property theText:“This is my text”

In the applescript editor result window I got “This is my text”

Ric

What was the AppleScript command you used to get the variable? It is still not working. I might have to open up a new app and paste my code from this website in…

tell application "ScriptingTest"
	activate
	set p to theText
end tell

That’s the whole script.

This is the whole script in the Xcode project:

script ScriptingTestAppDelegate
	property parent : class "NSObject"
	property theText : "This is my text"
	
	on application_delegateHandlesKey_(sender, theKey)
		if theKey as string = "theText" then
			return true
		end if
		return false
	end application_delegateHandlesKey_
	
end script

And, this is the sdef:

<?xml version="1.0" encoding="UTF-8"?>

Ric

Well, I made a new application and pasted the code from this webpage (besides the sdef code) in it and IT WORKED!!! Thank you rdelmar!

Now I am trying to be able so set theText. When I do “set theText to” and then put a string It tells me it can’t set theText to the string. In the sdef the permission is read/write, but I know I’m doing something wrong. Does anyone know how to set a property, not just get one?

Did you change the .sdef from what you posted? There isn’t any r/w permission set in there. But when I run it with the sdef you posted above, it lets me set theText. I changed the applescript to:

 tell application "ScriptingTest"
	activate
	set p to theText
	delay 5
	set theText to "New Text"
	set q to theText
	delay 5
end tell

I get this in the log:

tell application “ScriptingTest”
activate
get theText
→ “This is my text”
set theText to “New Text”
get theText
→ “New Text”
end tell

Ric

But your sdef didn’t have read/write either.

Yes, I know, that’s why I started that sentence with “but” – perhaps r/w access is the default. In any case, it works in my hands, so I don’t know what’s going on with your script.

Ric

I did a test with another sdef and yes, r/w is the default. However, I still can’t get my app to work. I will try making another application. Sigh. :slight_smile:

rdelmar, could you send your Xcode project to me via e-mail? That would be good so I can compare. :slight_smile:

I sent you a private message.