Re: How to use alloc/init... to create an object
Re: How to use alloc/init... to create an object
- Subject: Re: How to use alloc/init... to create an object
- From: Craig Sutherland <email@hidden>
- Date: Fri, 30 Sep 2011 11:26:40 -0500
On Sep 29, 2011, at 2:20 PM, David Hoerl wrote:
>> Try -
>>
>> property parent : class "NSObject"
>>
>> set theImage to current application's NSImage's alloc()'s initWithContentsOfFile_("/tmp/wine.jpg")
>>
>> Craig Sutherland
>
>
> Thanks! I was sent a link off list by a kind soul with a long tortuous thread on someone else trying to do the same thing as me:
>
> http://macscripter.net/viewtopic.php?id=30462
>
> There were a lot of complications!
>
> In any case, can you tell me what does the first line - the property parent line do? I would have thought I'd only need that if I was defining a class. Just curious...
>
> Also, I hadn't put in the "current application's" as I was unsure if I'd need that in a simple AppleScript.
>
> Thanks so much for the reply!
>
> David
David-
All this is pretty new for me, so be forewarned!
When creating an ASObj-C project in Xcode, the default script created is an AppDelegate script and has the property parent : class "NSObject" as well as the stubs for 2 delegate methods. As explained in Shane Stanley's book that line of code makes all the basic NSObject capabilities & behavior available to the project. ApplescriptObjC lets Applescript objects serve as ObJc objects in the Cocoa Runtime (Mac OS X Dev Library http://developer.apple.com/library/mac/#releasenotes/ScriptingAutomation/RN-AppleScriptObjC/_index.html) You could create the rest of your project in that xxxAppDelegate.applescript file. This would be the case in the example code included in the first e-m. Usually additional script classes are in separate files to help with organization.
There are several ways to call a class method. Set a property to refer to the class as you were, then use the variable name in our code. The downside of this is always remembering to include the property declaration if you use the code elsewhere.
The ApplescriptObjC bridge opens up access to all class names via properties at the application level. Vanilla applescript would not understand the 'parent property " "NSObject" . The bridge syntax uses the tell application block when addressing an object you have created in code and allows the 'parent property : "NSObject" to compile.
You also use the tell application block if your script uses other external references such as scripting additions.
Craig
_______________________________________________
Do not post admin requests to the list. They will be ignored.
applescriptobjc-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden