You are not logged in.
Translating Objective-C into AppleScriptObjC
To 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 Elements
Since 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.
NSColor *blueColor = [NSColor blueColor];
[textField setTextColor:blueColor];
In AppleScriptObjC
Applescript:
tell class "NSColor" of current application
set blueColor to its blueColor()
end tell
textField's setTextColor_(blueColor)
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:
script textField
on setTextColor(theColor)
color me theColor
end setTextColor
end script
set blueColor to {0, 0, 65535}
textField's setTextColor(blueColor)
Creating an NSColor
Click on the NSColor inside the method and the documentation will switch to the NSColor Class Reference.
// 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 Method
Here is the AppleScriptObjC way to write the Objective-C code above. Is this starting to make sense?
Applescript:
tell class "NSColor" of current application
set blueColor to its blueColor()
set backgroundColor to its colorWithCalibratedRed_green_blue_alpha_((243.0 / 255.0), (120.0 / 255.0), (31.0 / 255.0), 1)
end tell
textField's setTextColor_(blueColor)
textField's setBackgroundColor_(backgroundColor)
textField's setDrawsBackground_(true)
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.
Applescript:
script TextFieldsAppDelegate
-- Inheritance
property parent : class "NSObject"
-- IBOutlets
property textField : missing value
-- IBActions ( button clicks )
on changeTextColor_(sender)
tell class "NSColor" of current application
set blueColor to its blueColor()
set backgroundColor to its colorWithCalibratedRed_green_blue_alpha_((243.0 / 255.0), (120.0 / 255.0), (31.0 / 255.0), 1)
end tell
textField's setTextColor_(blueColor)
textField's setBackgroundColor_(backgroundColor)
textField's setDrawsBackground_(true)
end changeTextColor_
end script
To Test it Out
Add 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.
Until next time, happy coding!
Offline
Yet another great tutorial from you Craig !
Keep up the good work, it's really awesome that you contribute for everyone who is lost in ASOC
Offline
You are very welcome.
Are you ready for more advanced techniques or should I spend some more time on basics?
Offline
This one was very helpful for me. How about one that does return a value? I'm trying to figure out createSnapshotImage from class QCView.
Offline
I'd like to see a tutorial on how to control other applications from your project--example:
Tell application "iTunes"
play next track
end tell
How would you do something like that in the new AppleScriptObjectiveC language? This *used* to be that easy to do.
Offline
It still is. You only need to hook that code to a button.
Offline
But, what (or how) do you surround it with the new handler code? I'm still not good with all that language. Do you have a quick example of what it would look like in the code somewhere?
Offline
Have you gone through the other tutorials? If not, they will help a lot.
Connect this to a button.
on playNextTrack_(sender)
tell application "iTunes"
play (next track)
end tell
end playNextTrack_
Offline
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.
Offline
There is now a comment at the beginning of tutorial #1 suggesting they read this one first.
Offline
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.
Offline
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).
Carl
Model: iMac
AppleScript: 2.1.1
Browser: Firefox 3.5.5
Operating System: Mac OS X (10.6)
Offline
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.
Offline
I just checked this out. If you hit enter after you hit your button, the colour changes in the background.
Could it be something about finished editing?
Tap,tap,tap , hello , testing 1..2, 1...2,
http://www.markosx.com/thecocoaquest/
Offline
Ah, Found the problem.
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.
Last edited by mark hunte (2009-12-28 06:21:37 am)
Tap,tap,tap , hello , testing 1..2, 1...2,
http://www.markosx.com/thecocoaquest/
Offline
Thanks Mark!
I could have swore I had tried that but I guess not.
Hope you had a wonderful Christmas!
Craig
Offline
Thanks Mark!
I could have swore I had tried that but I guess not.
Hope you had a wonderful Christmas!
Craig
I did, thanks, hope you had a good one, And have a happy New year..
Tap,tap,tap , hello , testing 1..2, 1...2,
http://www.markosx.com/thecocoaquest/
Offline
I think the problem is that it won't draw the background while there is uncommitted editing.
Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com
Offline