AppleScriptObjC in Xcode Part 1

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.

I know exactly what you mean, when something is “natural” to you, it’s difficult to put yourself back in a position where everything is new, where you’re just discovering.
It’s nothing personal and I hope I didn’t offend you – sorry for the way I wrote that. Don’t get me wrong, you’re doing an amazing job here, and needless to say you are a pioneer at this right now.

Well for one thing, my C experience dates back to a few years and I’ve forgotten it all. “Classes”, “Parent classes”, etc… English not being my native language, are confusing terms as I cannot remember exactly what they are, and what we need them for.
I guess it’s nothing in particular, just the whole thing is a little blurry to me.

I guess because the comments seem less clear than explaining exactly what you’re about to do, what you need to refer to and THEN showing the code for it, like you did in tutorial 1. Am I making any sense ?
Oh, a few confusing bits to me so far : what’s the difference between Cocoa and Objective-C ? Ojb-C is a programming language, so what about Cocoa ? is it a framework or does it have something to do more with the GUI side of programming ? I see both these terms used in documentation, seems like one could be used instead of the other sometimes…

I am not offended in the least. I appreciate the feedback and want to make these articles as understandable and user friendly as possible.
If they are not, I want to correct that.

The best place to start is “Introduction to The Objective-C 2.0 Programming Language.

Complete sense. Once I get through with the tutorial I am working on I will go back and make the necessary adjustments.

I found a great explanation here.
.
.
.

okay, so I follow along with the very first part, draw my connections as instructed to do in Interface Builder, and…nothing. type in the text field, click the button, nothing happens. When I look at the downloaded project, I noticed that in the downloaded project, the textView field has neither a referencing outlet or a nextKeyView, whereas in my project, it does have a referencing outlet. The text field in your project has both a nextKeyView and a referencing outlet, whereas my version has neither.

I copied the code out of the example, so I’m pretty sure I don’t have a typo, but I think there’s a step or two I’m missing. Ideas?

Zip it up and email to craigw@macscripter.net.

Thanks for the help craig, i’d have never guessed about the differences in the drag targeting.

Oh, one thing. in the initial part of setting up awake from nib you have the border and default text code as:

myTextView’s setTextContainerInset_({10.0, 10.0})
myTextField’s setStringValue_(“Default text”)

if you don’t initially notice that the ‘my’ shouldn’t be there, you can have a couple minutes of da head scratchin’

Hi Craig,

I tried to modify our small example.

I tried two things:

I inserted a beep and I tried to append the next textField value to the end of the textView like this:
Value 1
Value 2
.
.
Value n

Unfortunately I´m doing things wrong and get neither one to work.

I did this:

1 on setTextViewFromTextField_(sender)
2 beep # Why is this, it´s AppleScript, no beep comes out of the Speaker
3 set textFieldValue to textField’s stringValue()
4 set tempViewValue to textView’s stringValue() # I also tried getStringValue() and getValue()
5 textView’s setString_(tempValue & textFieldValue)
6 end setTextViewFromTextField_

The console says: -[NSTextView stringValue]: unrecognized selector sent to instance 0x200277360

Greetings, Ronald

The “beep” should be working. Try “beep 2” and see if you get different results.

This is your clue to where the issue lies. If NSTextView does not recognize the selector (method)
“stringValue” then we know that NSTextView does not have a method called “stringValue.” The
next step is to look for a method in NSTextView that we can use.

Unfortunately, there is no method in NSTextView. We have to look to its parent for a method.
NSText has a method called “setString:” and “string.” Those are the methods you want to use
for a NSTextView.

Also, if you look at the code above, we use “setString_()” to set the text of the text view.
The way key-value coding works is to remove the “set” from the beginning and lowercase
the first letter so “setString_()” becomes “string().” You never use “get” in Objective-C.

hth,

Craig

Sorry for bothering you again Craig.

I tried both:

set textViewValue to textView’s string_()
#which returns: -[NSTextView string:]: unrecognized selector sent to instance 0x200252b40

If I don’t use the underscore XCode changes string() to string {} automatically.

and

set textViewValue to textView’s stringValue_()
#which returns: -[NSTextView stringValue:]: unrecognized selector sent to instance 0x200224600

This is what makes ObjC newcomers a real hardtime. It´s almost impossible to find out where things are described in the apple documentation. It always seems to be a matter of incident to find the right place.

Greetings, Ronald

Sorry, I had forgotten that when you use a method that corresponds to an
AppleScript key word you have to surround it with “|” symbols.

The following code will take text from a text field and combine it
with the current text in a text view using a new line separator.


set theText to textField's stringValue()
set curText to textView's |string|()

tell class "NSString" of current application
	set newText to its stringWithFormat_("%@%@%@", theText, return, curText)
end tell

textView's setString_(newText)

BTW, when you first start using Objective-C, reading the documentation is
difficult but it does get easier. Start with the class, say NSTextView, and
look at all its methods. If you do not see one that sounds like what you
want then look to its parent class. Repeat until you are at NSObject or
the top level class.

AppleScriptObjC Part 5 covers reading the documentation. Have you gone
through this tutorial? If so, is there anything I can do to make it
more useful?

Regards,

Craig

Great, now it works.
What I don´t understand, why don´t we need the vertical bars in the first line ???

1 set theText to textField’s stringValue()
2 set curText to textView’s |string|()
3
4 tell class “NSString” of current application
5 set newText to its stringWithFormat_(“%@%@%@”, theText, return, curText)
6 end tell
7
8 textView’s setString_(newText)

This thread is really great Craig. Nowhere else I can put questions and and even get answers.

Where are you from? Next time I come to your place I take you to a restaurant :slight_smile:

Greetings, Ronald