Cocoa For Applescripters part 1: call method/rtm

Incidentally, who says you can’t get the first responder using AppleScript alone?


[This script was automatically tagged for color coded syntax by Convert Script to Markup Code]

These may not all be the exact thing you’re looking for, and gasp I researched what I suspect to work but haven’t tried the code :oops: gasp but here goes…

note… I am assuming you’ve been following the thread to this point and can find the documentation I refer to if I mention a class… all of these ones are in the ApplicationKit… and understand what I mean by type, instance, label, and colon.

(A)Inserting text at the insertion point: (of an NSTextView?) (I skipped putting it on the pasteboard… if that’s an essential piece, or you mean any text entry object like text field etc. tell me I misunderstood the question)

NSTextView -(void)insertText:(id)aString

“-” tells us we need an instance of NSTextView: If we have a text view on our window, and let’s say we get it and set “theTV” to it. A type in paranthesis before the first label tells us what the method returns… void means nothing. It takes a single parameter… in this case (id) which means anything, but from the name, and the description we can glean that we should send it the string we wish to insert. The method name (labels and colons squished together) is “insertText:”

call method "insertText:" of theTV with parameter "my text to insert"

(B) Current text field of a window ( I believe this is the same as first responder… note that responders are in a chain… and each responder has a “nextResponder” method pointing to the next one… the very first responder is the application, if it doesn’t handle the event it passes it to the nextResponder and so on… the “firstResponder” of a window is the item that responds first within all of the subviews of the window. Actions that get passed along the responder chain include key presses… which is why I suspect this is the one you want )

NSWindow -(NSResponder*)firstResponder

“-” means we need an instance… lets say the key window (the key window is the one that currently responds to key presses, which is different from main window in the example of a find dialog where the key window is the find dialog and the main window is the one being searched)… first (type) is what it returns : a responder (or one of its subclasses…) and the name is labels and colons squished together “firstResponder” as there’s no colons we send no parameters.

call method "firstReponder" of key window

(C) Getting screen resolution

This is a three parter… some lucky people have multiple screens so we actually need to ask the NSScreen class for an instance that we then ask for its resolution.

(i) getting the main screen

NSScreen +(NSScreen*)mainScreen

“+” means we call on the class “NSScreen” … the method name is labels and colons squished together “mainScreen”, the first parentheses mention the type of thing it returns: an NSScreen instance

set mainScreen to call method "mainScreen" of class "NSScreen"
-- mainScreen turn on... you have no chance to survive make your time...
-- couldn't help it...;P

(ii) getting this screen’s resolution

NSScreen -(NSRect)visibleFrame

note the visible frame leaves out the dock and menu bar… for the complete resolution use -frame

“-”: needs an instance… see above… check!
name (labels and colons squished): “visibleFrame”
return type: NSRect

set theArea to call method "visibleFrame" of mainScreen

(iii) :oops: this really isn’t possible with ASS after all…

you don’t want an NSRect… you want the width and height as numbers! the thing is that this would be possible if NSRect was an objc class that you know we could like call methods on to retrieve the values and all… but it’s a plain old C-struct… which doesn’t respond to methods at all… this next bit is (i) and (ii) plus this in obj-c you’ll understand it all by part four (I hope…) when I finally get around to posting part 3 and 4… :oops:

#import <Cocoa/Cocoa.h>

@interface ASMainScreenFrameQuery : NSObject
@implementation ASMainScreenFrameQuery
    return [[NSScreen mainScreen] visibleFrame].size.width;

    return [[NSScreen mainScreen] visibleFrame].size.height;

If you put the above in a file name “ASMainScreenFrameQuery.m” and add it to the Sources build phase of your app, you should be able to do

set screenHeight to call method "mainScreenHeight" of class "ASMainScreenFrameQuery"
set screenWidth to call method "mainScreenWidth" of class "ASMainScreenFrameQuery"

(oh yeah… I returned floats, as in floating decimal point numbers… not integers… that’s what they are stored as in the NSSize c-struct… I hope it works out for you)

OK, Mooresan. I am stumped once again. My intention is to display a picture in an image view. I have have thePicture, which is RAW JPG data (which I have obtained from within an .mp3 file by scripting iTunes to parse it out), and an imageView. I want to use a call method to display thePicture in the imageView.

It seems my choices are NSImageView, NSImage, NSImageCell, and NSImageRep. The NSImageView class offers

  • (void)setImage:(NSImage *)image
    The NSImage class offers
  • (id)initWithData:(NSData *)data
    The NSImageCell class seems promising, but I can’t find a method that really applies.
    And the NSImageRep class is . . . well puzzling.

So, in looking at - (void)setImage:(NSImage *)image of NSImageView, I assume my INSTANCE is the imageView object I have created with IB. So I try:

call method “setImage:” of imageView with parameter thePicture

This gives me an error. I wonder if this is because it is RAW data, so I try setting thePicture to an actual JPEG file. This errors as well. So I move on to

  • (id)initWithData:(NSData *)data
    Somehow I don’t think the INSTANCE is the imageView, so I try to figure out what object to call on. Once again, I am confuzed as to how to create an object, since I don’t have an NSImage object in AS. Previously, you have directed me to the “Class Description” header of the documentation for the answer. So, in this case it reads:

“An NSImage object contains an image that can be composited anywhere without first being drawn in any particular view.”

Nothing there to use. What do I do now?



The parameter is an NSImage, not a path to the image so try this with code:


[This script was automatically tagged for color coded syntax by Convert Script to Markup Code]

That clears up why the JPEG image on disk wouldn’t load. However, that is not my main concern. My main goal here is to display a JPEG image that I have as RAW DATA stored in a variable called thePicture. The actual file on disk is an .mp3 file that contains the JPEG data. Trying to load that file will not work, so I have a handler which pulls the data out an stores in a variable “thePicture”.

So, I am trying to figure out which call method(s) will accomplish this, since AS doesn’t seem to have the ability to load the raw data into an image view.

Thanks for the response,


on thursday I had the unfortunate experience of watching my iBook suffer a logic board failure… and my model is not in the listed ones for free replacement. As such I appologize for any inconveniance in the timely-ness (or lack of) in my responses and postings… I am posting from a windows box that I have infrequent access to… and I dont think I can properly continue the series. A good place to start on learning objc is at the CocoaDev Wiki The commmunity there is great and its a great place to look up all kinds of information on many topics, because of the way it all cross references… I wrote which contains briefly most of the planned content for parts 3, and 4… (other than objc files structure… but there are plenty of tutorials there to get you going) Anyways… this is mooresan, signing off (for the next while) :cry:

I’m trying to use the methods outlined in this thread to load a webpage into a webview via applescript. This is what I have…

on awake from nib myWebView
	set myWebView to call method "mainFrame" of myWebView
	call method "loadHTMLString:baseURL:" of myWebView with parameters {""}
end awake from nib

All this does is place the URL as text into the webview. I want it to actually load and render that page in the webview.

Seems like I’m one step away, but can’t figure out how to do this.


Try the following code which works for me. Can’t remember exactly from where I copied this … I think I found it here in this forum.

on awake from nib theObject
	set method to "stringByEvaluatingJavaScriptFromString:"
	set myScript to "location.href=''"
	call method method of theObject with parameter myScript
end awake from nib