AppleScriptObjC in Xcode Part 5 - Converting ObjC to ASOC

Translating Objective-C into AppleScriptObjCTo truly utilize AppleScriptObjC we need to understand some Objective-C and how to read the documentation. During this tutorial we will look at similar code in both languages to gain a better understanding of how to implement Objective-C methods in AppleScriptObjC.

UI ElementsSince we are using AppleScriptObjC to build applications we will start with something we all use; text fields.

If you type NSTextField in your Xcode document, hold down the option key and double-click. A small window pops up with some information about this NSTextField. Click the "book symbol" at the top right of this window and the Documentation window will open up to the NSTextField class reference. This is an easy way to get to documentation quickly and search on a specific item. You can also open the documentation from the Help menu.

Popup Information

If you do not have the documentation open and viewing the NSTextField's class reference, do that now. Make sure you can see the "Table of Contents" on the left hand side. If not, click on the words "Table of Contents" near the top and it will open.

Table of Contents

Toggle open the "Instance Methods" under "TOC"Here is a list of all the instance methods for NSTextField. Notice that there are no class methods for NSTextField. The reason for this is all of your applications' NSTextField's are instantiated when your application is launched.

NSTextField's Instance Methods

Background and Text ColorThere is a lot we can do with an NSTextField but for now we are going to change the background and text colors. Notice that we have "backgroundColor" and "setBackgroundColor:", "textColor" and "setTextColor:." These are called setters and getters and they conform to key-value coding naming conventions. The "set" versions are for setting the value and the others are for getting the value.

setTextColor MethodIf the "Instances Methods" under "TOC" is not open click the disclosure triangle. Now click on the "setTextColor:" method.

setTextColor

- (void)setTextColor:(NSColor *)aColorSo, what does that mean? We'll go through it a piece at a time.

"-" means that this is an instance method and requires an instance of the class to use this method. It will make more sense when we deal with classes that have both instance and class methods. Class methods have a "+" instead.

"(void)" means that this method does not return a value to the calling code. Some methods return values but this one does not.

"setTextColor:" is the name of the method and the colon means it takes a parameter.

"(NSColor *)" is the required "type" for this parameter. Types or Kind are what the object or class is. For instance, you will get an error if you try to send an NSString instead of an NSColor. The "*" means that this is a pointer to a place in memory and not the value itself. It is referred to as "pass by reference." The other option is to "pass by value." Don't worry too much about pointers right now.

"aColor" is the variable holding the NSColor.

How do we use it?Create a property for our text field called "textField." We will cover NSColor soon.

Applescript:

What is happening here?We are telling the NSColor class to execute its blueColor() method and return the NSColor object to the blueColor variable. We then tell the textField's method "setTextColor_()" to execute its code with our blueColor as its parameter.

Look at it this way. In AppleScript we could do something very similar using a script object.

Applescript:

Creating an NSColorClick on the NSColor inside the method and the documentation will switch to the NSColor Class Reference.

NSColor Link

Toggle open "Tasks" inside the "Table of Contents" and click on "Creating an NSColor with Preset Components."

Toggle Presets

NSColor Preset ColorsAs you can see, all of these have a "+" next to their name meaning they are class methods. Click on "blueColor."

Color Presets

Blue Color Preset

blueColor

Here the "(NSColor *)" at the beginning of the line says that the return value is a pointer to an NSColor object. Again, don't worry right now about pointers, just realize that what you get back after calling this method is a blue color of type NSColor.

Setting the Background ColorSetting the background color will involve the same procedure but this time we will use a custom color using NSColor's "colorWithCalibratedRed:green:blue:alpha:" method. All of the parameters are expecting a type of CGFloat. If you click on that you will see that it is just a typdef of float for 32 bit and double for 64 bit. What this means to us is that we need to provide a decimal number. Notice also that the values must be between 0.0 and 1.0.

CalibratedColorMethod

I am using the Digital Color Meter, found in /Applications/Utilities/ folder, to discover a particular rgb color. I chose the 8 Bit representation but it is based on a range from 0 to 255. To convert these values to values between 0.0 and 1.0, we divide the number by 255.

Digital Color Meter

Objective-C MethodOne of the things I really love about Objective-C is "keyword:value" parameters. There is no confusion about what the method is wanting from you. Lining up the ":" is another Objective-C convention and really makes reading the code easier.

Now, I am giving something away in this code snippet. We will need to use NStextField's "setDrawsBackground" method to see our change.

// The colons do not line up here very well because of webpage rendering//NSColor *color = [NSColor colorWithCalibratedRed:(243.0/255.0) green:(120.0/255.0) blue:(31.0/255.0) alpha:1]; [textField setBackgroundColor:color]; [textField setDrawsBackground:YES];

AppleScriptObjC MethodHere is the AppleScriptObjC way to write the Objective-C code above. Is this starting to make sense?

How did we know to use "setDrawsBackground:"How did we know that we needed to use the additional method? Well, I didn't. This is the first time I have set the background color of a text field. When it didn't work, I went back to the documentation to see if I had missed something. I noticed "setDrawsBackground:" in the See Also section.

setDrawsBackground

Click on "setDrawsBackground:" and let's see what it says.

[/center]

setDrawsBackground Method

This method must go after the "setBackgroundColor:" method for it to draw the new color.

Final CodeAfter all that work this is all the code for the project. After all, it doesn't do much.

To Test it OutAdd this code to a new project but remember to change the script's name from "TextFieldsAppDelegate" to what you named your project. Add a text field and a button to your layout, hook them up and build and run.

Remember, there is a GUI Building Video that demonstrates how to make connections in IB if you need a refresher.

Reading the documentation took me awhile to understand so I hope this helps.

Re: AppleScriptObjC in Xcode Part 5 - Converting ObjC to ASOC

Hi Craig, great tutorial, but this was needed after or in Part 1. Logic is you need to do the tutorials previous to understand the next, 2 was deep end, drowning (still am table view wise), and I would have loved this but tutorial number '5' says "fat chance if you haven't done 1-4, don't bother looking". Perhaps my fault for not 'jumping ahead' when you told me to do the rest.

Re: AppleScriptObjC in Xcode Part 5 - Converting ObjC to ASOC

Re: AppleScriptObjC in Xcode Part 5 - Converting ObjC to ASOC

Thank you!

If your thinking of a next one I would suggest a simple tutorial on help files, not everything, but enough to find out the rest yourself, same with saving preferences, I know there are quite a few methods for plists and another for user defaults but I only have the ability to use delimited text files. I don't need any of this anymore until I've added every user interaction to Dialog Maker but many people would like to see a tutorial on it I think.

Re: AppleScriptObjC in Xcode Part 5 - Converting ObjC to ASOC

I tried this program and almost every thing worked (i.e., when I typed something into the text box and hit the button, the color of the text changed to blue), but the background color did not work. I noticed a slight change in the text field box so I suspect the background color was changed, but the "textField's setDrawsBackground_(true)" statement did not. Is there something else that I need to do? By the way, I'm using OS X 10.6.2, Xcode 3.2 (64 bit), and Interface Builder 3.2 (732).

Re: AppleScriptObjC in Xcode Part 5 - Converting ObjC to ASOC

I can confirm that this is not working on my machine with AppleScriptObjC or Objective-C. It still works on Leopard though. Others are saying it is still working on their machine running Snow Leopard so I am not sure what the specific issue is.

Re: AppleScriptObjC in Xcode Part 5 - Converting ObjC to ASOC

On a hunch, I suspected that if the TextField is set to draw background in IB. That it may interfere with the code to tell it to do so.. why I thought this who knows..

But when I un-check the 'Draws background' in IB.

All worked as expected.

*edit

If you take out the 'textField's setDrawsBackground_(true)' and check the 'Draws background in IB. It does not work. So the bug is in 'Draws background in IB. It will override the code 'textField's setDrawsBackground_(true)' .And not complete on its own, until enter is hit.