AppleScriptObjC in Xcode Part 1

One thing I would check is which blue cube did you drag to. When I first wrote the tutorial I did not notice that the class instantiation, blue cube, was already created. If you look at the tutorial now you will see I have removed that section.

If you have two blue cubes named the same thing then remove one of them and make sure all your connections are set up correctly.

hth,

Craig

Only one blue cube here :confused:

Oh well, I can just ctrl-drag from the title bar of the window to the text field and select "initial first responder’ that way, then I don’t have to write any code.

One more thing though, sorry :slight_smile:

In Applescript Studio I used to do stuff like:

on awakeFromNib(theObject)
bog()
end awakeFromNIb

to bog()
beep
end bog

In this example, my app beeps when you open it, but I dont’ have to duplicate code if I want it to beep when you click a button, I can just put ‘bog()’.

I hope it’s clear what I mean by that. How can I do this in AppleScriptObjC?

Joe

PS: AppleScriptObjC apps seem to execute much faster! This is great!

As for the window make sure when you control + drag from the blue cube you are connecting it to the Window and not the View of the Window. Drag to the menu bar.

Same way. Is that not working for you? Anywhere in your code you call “bog()” you should get a beep.

Craig,

Suppose you change your handler to do something more time-consuming, like a delay or a large repeat loop (shock horror, even some work :wink: ). Are you seeing the button remain highlighted until the handler’s script has finished?

Yes, but it does the same thing in a Cocoa app. I will send you an example.

By the way, in your final code, am I right in saying you don’t need the line:

property aTableView : missing value

That is correct. I will update the tutorial and the source code. Thanks for catching that!

@Craig
Hi, I got only one blue cube (it´s a controller?) but it doesn´t react when I control drag from the button.

OK, I think I got it.
My project is named ‘secondTry’.
Unfortunately I copied the code from your example and that is called ‘PartOne’ and in the first line of my project I had PartOneDelegate.
After I changed it to the correct name it worked.

Thanks for this example.

Greetings, Ronald

One mor question please.

the to lines (are they still Handlers?)

on applicationWillFinishLaunching_(aNotification)
      and
on applicationShouldTerminate_(sender)

where are they from? Are they user defined? Looks like since I don´t find them in the Doc.

Greetings, Ronald

some more remarks:

I wondered why
textField’s setStringValue_(“Default text”)

didn´t work inside awakeFromNib()

But it does work when I commet this line out
textView’s setTextContainerInset_({10.0, 10.0})

Debugging is not working, any clues?

Greetings, Ronald

You can find them in the documentation under “NSApplicationDelegate Protocol Reference.”

They are handlers but you do not call them directly, they get called automatically by your application.

Things to try.

  1. Reversing the order. Put setStringValue before textView.
  2. Make sure textView is hooked up inside interface builder. If you are having trouble understanding how to hook things up watch the video on GUI building in Part 2.

Debugging is not working I know. The way I debug is with “log” and “try” statements in my code.
Also, try to focus on getting one thing working at a time and build from there.

Do you have the “Console” open in Xcode when you “Build and Run?” There are error messages there that help in debugging as well.

eg.


log "Setting default value in textField"
textField's setStringValue_("Default text")

log "Setting inset in textView"
try
  textView's setTextContainerInset_({10.0, 10.0})
on error e
  log "Error setting inset: " & e
end try

hth,

Craig Williams

Hi Craig,
thanks a lot for your explanations.
Well I´ve already done some Objective-C this year and are pretty familiar with connecting elements to the controllers.
I´ll give it one more critical look :wink:

I think ASS-OBJC is really thrilling.

Greetings, Ronald

Hi Ronald,

I have noticed that I have to do a “Clean All” more than I would in an Objective-C project.
Maybe doing that will help.

Regards,

Craig

I just read a few topics here today because I was very thrown off by the new Xcode and the changes in Applescript under OS X.6.

I ran into this topic and I will be looking at it thoroughly, and hopefully this will get me started with ApplescriptObjC (which really freaked me out for a second there, going “how the hell am I going to get into that!”).

Thank you Craig, thank you for your work and your contribution.

BS0D

Hi BSOD,

Thank you for the kind words.

I know this is a big change for everyone. I think learning some Objective-C will help in getting the most out of AppleScriptObjC. I mean, just look at the name. Objective-C is part of the name.

I have listed out a few resources for learning Objective-C here.

I believe with just a small amount of Objective-C understanding, writing applications in AppleScriptObjC will be much easier.

Regards,

Craig Williams

Thank you for the link, anything I can gather will most likely be useful.

Yes, when you know where to start !

I’m still very afraid that I’ll be totally incapable to develop any useful, meaningful applications now.
I was thinking of rewriting my first ASS application in ApplescritpObjC but surely it will take some time for me to be able to do that : all the resources I’ve found and looked at so far have confused the hell out of me.

Haven’t looked at part 2 yet, but ALREADY I wonder : how about when there are 3 or 4 textFields on the same window ? How would you make sure you refer to the right one in the code ?
Don’t you have to name them or define them like you would in ASS ?

How would that work ?

I CANNOT WAIT TO LEARN MORE. Despite the difficulty of learning, if not a new programming language, at least its basics, it looks really awesome!

Every object in your window needs a reference to a property in your AppleScript file. Each text field will have its own property. You link the two together in Interface Builder by control dragging from the Application Instance (*blue cube) to the text field and clicking on the corresponding property. Watch the GUI building video from Part 2 for a demonstration on how to do this.

I am currently working on a tutorial to demonstrate how to lookup Objective-C methods in the documentation, understand what you are seeing there and how to convert that to AppleScriptObjC.

“A journey of a thousand miles begins with a single step” - Lao-tzu

I know it is hard learning new languages. It is hard for all of us. You just have to begin. I started by reading Aaron Hillegass’ book. I must have read the first 100 pages at least five times before it started sinking in. I went to the Big Nerd Ranch for a week of training. I spent hours each day going through tutorials, writing code, reading, experimenting, etc. I wrote lots of “throw-away” programs just for the sake of learning.

I did pretty much the same thing to learn FileMaker Pro, AppleScript, HTML, JavaScript, PHP, Ruby, you get the idea. I invest time, money and energy into the process and after a period of time it pays off.

Give yourself time. You can do this!

Regards,

Craig

*blue cube

.
.
.

Yeah, sure it takes time. You know how it is though, pretty much everyone I know who wanted to learn programming also wanted to know it all in a day and be able to code actual applications in no time.
I can’t wait for more of your tutorials, they make me a bit more enthusiastic about this whole change in Applescript programming!

(However, Part 2 is waaay too vague for me, after reading 3 times I’m still not getting all of it – in comparison to part 1 you seem to assume that all your readers have gotten the hang of Objective-C classes etc which is def not the case!)

I struggle more with writing than I do with coding. :slight_smile:

Is there any part that is particularly vague or is the whole thing difficult to understand?
I don’t mind re-writing or going into more detail but I would like to work on the areas that
are the most difficult to grasp.