Saturday, November 18, 2017

#26 2009-09-06 03:09:39 pm

Craig Williams
Administrator
From:: Ft. Smith, AR
Registered: 2006-12-07
Posts: 888

Re: AppleScriptObjC in Xcode Part 1

BS0D wrote:

It's nothing personal and I hope I didn't offend you -- sorry for the way I wrote that.


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.

BSOD wrote:

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.


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

BSOD wrote:

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 ?


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

BSOD wrote:

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 found a great explanation here.
.
.
.

Offline

 

#27 2009-09-12 03:43:21 pm

jcwelch
Member
Registered: 2009-09-11
Posts: 3

Re: AppleScriptObjC in Xcode Part 1

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?

Offline

 

#28 2009-09-12 04:11:20 pm

Craig Williams
Administrator
From:: Ft. Smith, AR
Registered: 2006-12-07
Posts: 888

Re: AppleScriptObjC in Xcode Part 1

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

Offline

 

#29 2009-09-17 06:32:37 am

jcwelch
Member
Registered: 2009-09-11
Posts: 3

Re: AppleScriptObjC in Xcode Part 1

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'

Offline

 

#30 2009-09-18 03:14:47 am

RonaldMHofmann
Member
From:: Switzerland
Registered: 2004-06-01
Posts: 20
Website

Re: AppleScriptObjC in Xcode Part 1

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
---

Offline

 

#31 2009-09-18 10:21:59 am

Craig Williams
Administrator
From:: Ft. Smith, AR
Registered: 2006-12-07
Posts: 888

Re: AppleScriptObjC in Xcode Part 1

RonaldMHofmann wrote:

Applescript:

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


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

RonaldMHofmann wrote:

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


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

Offline

 

#32 2009-09-20 01:29:14 am

RonaldMHofmann
Member
From:: Switzerland
Registered: 2004-06-01
Posts: 20
Website

Re: AppleScriptObjC in Xcode Part 1

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
---

Offline

 

#33 2009-09-20 10:22:18 am

Craig Williams
Administrator
From:: Ft. Smith, AR
Registered: 2006-12-07
Posts: 888

Re: AppleScriptObjC in Xcode Part 1

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.

Applescript:


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


Filed under: NSString

Offline

 

#34 2009-09-22 04:00:11 am

RonaldMHofmann
Member
From:: Switzerland
Registered: 2004-06-01
Posts: 20
Website

Re: AppleScriptObjC in Xcode Part 1

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 :-)

Greetings, Ronald
---


Filed under: NSString

Offline

 

#35 2009-09-22 04:50:05 am

RonaldMHofmann
Member
From:: Switzerland
Registered: 2004-06-01
Posts: 20
Website

Re: AppleScriptObjC in Xcode Part 1

Where did you get this part from?

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

OK. I know what line 3 means.
But I never thought of 'tell class "NSString" of current application' Where can I find this?
I actually don´t know where to look it up. I made many different searches but I always got too many occurences.

Greetings, Ronald
---


Filed under: NSString

Offline

 

#36 2009-09-26 05:37:21 am

robdut
Member
Registered: 2009-09-02
Posts: 301

Re: AppleScriptObjC in Xcode Part 1

Hi All,

I have been reading Arron Hillgass's book on Cocoa Programming which is excellent and also "Programming in Objective C" by Steven Kochan. Both highly recommended

Just getting familiar with the language helps tremendously. It all depends on how far you want to go. I think, with some more good examples along the way, we can recreate anything we did in ASS in a better and simpler way.

Now, if we want to extend much further into complexity, it will be necessary to actually write some class files to augment ASOC apps.  I already had quite a few class files in my ASS apps, thanks to gifts from OBJ_C folks with some minor tweaking on my part. I already need more in ASS than applescript could provide.

So the more you want to learn the more you can do, and  without becoming an OBJ-C programmer (not in this lifetime for me!)

Rob

Offline

 

#37 2009-10-13 09:05:32 pm

mbs402
Member
From:: Plano, Texas
Registered: 2009-04-07
Posts: 53
Website

Re: AppleScriptObjC in Xcode Part 1

Craig,

Thanks a million for the great tutorial, I thought it was awesome... 

I am not an Objective C person, so I hope you'll get a chance to make some more AppleScriptObjC. I had started several AppleScript Studio projects and they were going fairly well, and now it's gone.

Hopefully we can get by with minimal Objective C ?

Here's a couple of easy changes to the first tutorial script, hope it is useful for beginners.

Thanks Again,

Bill Hernandez
Plano, Texas

--
--  tutorial_part_oneAppDelegate.applescript
--  tutorial_part_one
--
--  Created by Bill Hernandez on 10/13/09.
--  Copyright 2009 mac-specialist.com. All rights reserved.
--
-- +---------+---------+---------+---------+---------+---------+
-- NOTES :
-- +---------+---------+---------+---------+---------+---------+
-- set time_stamp to my get_time_stamp(2)
--
-- ( n ) ---> format choices { 1, 2, 3 }
-- ( 1 ) ---> [2009.10.13](07.15.41.PM)
-- ( 2 ) ---> [ 2009.10.13 ] ( 07.14.43.PM )
-- ( 3 ) ---> 2009.10.13_07.15.15.PM
-- +---------+---------+---------+---------+---------+---------+

script tutorial_part_oneAppDelegate
    -- +---------+---------+---------+---------+---------+---------+
    -- Inheritance
    -- +---------+---------+---------+---------+---------+---------+
    property parent : class "NSObject"
    -- Option double click on ---> NSTextView
   
    -- +---------+---------+---------+---------+---------+---------+
    -- IBOutlets   
    -- Interface Builder considers an outlet as any
    -- property with "missing value" as its value
    -- +---------+---------+---------+---------+---------+---------+
    property aWindow : missing value
    property textView : missing value
    property textField : missing value
   
    property show_message : false
   
    -- +---------+---------+---------+---------+---------+---------+
    -- IBActions (button clicks)
    -- Interface Builder considers an action as any
    -- single parameter method ending with an underscore
    -- +---------+---------+---------+---------+---------+---------+
    on setTextViewFromTextField_(sender)
        -- USER MODIFIABLE
        set which_update_method to 1 -- choices { 1, 2 }
       
        -- date_object and current_date (NOT USED FOR NOW)
        set date_object to current date
        set current_date to (date_object as string)
       
        -- ( n ) ---> time_stamp format choices { 1, 2, 3 }
        -- ( 1 ) ---> [2009.10.13](07.15.41.PM)
        -- ( 2 ) ---> [ 2009.10.13 ] ( 07.14.43.PM )
        -- ( 3 ) ---> 2009.10.13_07.15.15.PM
        set time_stamp to my get_time_stamp(2) -- format choices { 1, 2, 3 }
       
        if (which_update_method is 1) then
            tell textField
                set textFieldValue to get stringValue()
            end tell
           
            tell textView
                set textViewValue to get |string|()
                set newText to (textViewValue as string) & return & time_stamp & "  |  " & (textFieldValue as string)
                setString_(newText)
            end tell
           
            -- THIS ALSO WORKSOUTSIDE OF THE TELL BLOCK
            -- textView's setString_((textFieldValue as string) & return & (textViewValue as string))
           
            (*
                display dialog (textViewValue as string)
                display dialog (textFieldValue as string)       
            *)
        else
            set previousText to textView's |string|()
            set currentText to textField's stringValue()
           
            tell class "NSString" of current application
                set newText to its stringWithFormat_("%@%@%@%@", previousText, return, time_stamp & "  |  ", currentText)
            end tell
           
            textView's setString_(newText)
        end if
        if (show_message) then
            display dialog (newText as string)
        end if
       
    end setTextViewFromTextField_
    -- +---------+---------+---------+---------+---------+---------+
    on get_time_stamp(which)
        if (which is 1) then
            set returnValue to do shell script "date '+[%Y.%m.%d](%I.%M.%S.%p)'"
            ---> [2009.10.13](07.15.41.PM)
        else if (which is 2) then
            set returnValue to do shell script "date '+[ %Y.%m.%d ] ( %I.%M.%S.%p )'"
            ---> [ 2009.10.13 ] ( 07.14.43.PM )
        else if (which is 3) then
            set returnValue to do shell script "date '+%Y.%m.%d_%I.%M.%S.%p'"
            ---> 2009.10.13_07.15.15.PM
        else
            -- which is 1 or anything else
            set returnValue to do shell script "date '+[%Y.%m.%d](%I.%M.%S.%p)'"
            ---> [2009.10.13](07.15.41.PM)
        end if
        return returnValue
    end get_time_stamp
    -- +---------+---------+---------+---------+---------+---------+
    -- Application
    -- +---------+---------+---------+---------+---------+---------+
   
    on awakeFromNib()
        set time_stamp to my get_time_stamp(2) -- format choices { 1, 2, 3 }
       
        tell textView
            setTextContainerInset_({5.0, 5.0})
            setString_(time_stamp & "  |  " & "Bill Hernandez")
        end tell
        tell textField
            setStringValue_("Default text")
        end tell
       
        -- add this as the last item in the awakeFromNib handler
        aWindow's makeFirstResponder_(textField)
       
    end awakeFromNib
    -- +---------+---------+---------+---------+---------+---------+   
    on applicationWillFinishLaunching_(aNotification)
        -- Insert code here to initialize your application before any files are opened
    end applicationWillFinishLaunching_
    -- +---------+---------+---------+---------+---------+---------+
    on applicationShouldTerminate_(sender)
        -- Insert code here to do any housekeeping before your application quits
        return current application's NSTerminateNow
    end applicationShouldTerminate_
    -- +---------+---------+---------+---------+---------+---------+   
end script

Model: iMac 2.6 GHz Intel
AppleScript: Xcode 3.2.1
Browser: Safari 531.9
Operating System: Mac OS X (10.6)


Filed under: NSString, time_stamp

Offline

 

#38 2009-10-13 09:28:49 pm

mbs402
Member
From:: Plano, Texas
Registered: 2009-04-07
Posts: 53
Website

Re: AppleScriptObjC in Xcode Part 1

Craig,

How and where would you over-ride NSTextView in order to have a function stringValue(), or textValue() for  the
NSTextView class it so that instead of calling :

set previousText to textView's |string|()

You could call :

set previousText to textView's stringValue()
or :
set previousText to textView's textValue()

Such that :

set previousText to textView's |string|()
set currentText to textField's stringValue()

Would become more consistent :

set previousText to textView's stringValue()
set currentText to textField's stringValue()

OR :

set previousText to textView's textValue()
set currentText to textField's stringValue()

I could probably do something like :

on get_textValue(textViewRef)
    return textViewRef's |string|()
end get_textValue

Then call :

set previousText to get_textValue(textView)

But that would be somewhat of a kludge. Would it be easy to create a SubClass and substitute it for NSTextView. If so, how would you let the Xcode project know to use NSTextViewPlus instead of NSTextView ?

Thanks again,

Bill Hernandez
Plano, Texas

Model: iMac 2.6 GHz Intel
AppleScript: Xcode 3.2.1
Browser: Safari 531.9
Operating System: Mac OS X (10.6)


Filed under: NSTextView

Offline

 

#39 2009-10-13 09:37:09 pm

mbs402
Member
From:: Plano, Texas
Registered: 2009-04-07
Posts: 53
Website

Re: AppleScriptObjC in Xcode Part 1

Craig,

Is it possible we might look forward to seeing you doing AppleScripgObjC at Lynda.com ?

I just got a membership...

Talk about superb...

Bill Hernandez
Plano, Texas

Offline

 

#40 2009-10-13 10:48:54 pm

Craig Williams
Administrator
From:: Ft. Smith, AR
Registered: 2006-12-07
Posts: 888

Re: AppleScriptObjC in Xcode Part 1

Hi Bill,

mbs402 wrote:

How and where would you over-ride NSTextView in order to have a function stringValue(), or textValue() for  the
NSTextView class it so that instead of calling :

set previousText to textView's |string|()

You could call :

set previousText to textView's stringValue()
or :
set previousText to textView's textValue()

Such that :

set previousText to textView's |string|()
set currentText to textField's stringValue()

Would become more consistent :

set previousText to textView's stringValue()
set currentText to textField's stringValue()


To add methods to any class you create a category.

Add a new Objective-C file to your project.
Name it "NSTextView+methods."

The .h file:

@interface NSTextView (methods)
-(NSString *)stringValue;
@end


The .m file:

#import "NSTextView+methods.h"

@implementation NSTextView (methods)
-(NSString *)stringValue
{
    return [self string];
}
@end


In the AppleScriptObjC file you use:

on doAction_(sender)
    log textView's stringValue()
end doAction_


You can add as many methods onto Objective-C class as you like. I would document your changes for future reference.

Also, avoid using the words "get" and "set" in your method names. This will bite you later on when you start using bindings. I would only use underscores in method names to conform to AppleScriptObjC standards. Meaning, don't use them unless you have multiple parameters and the underscores are identifying them.

I have not looked into doing videos for Lynda but I have been a member for over three years now.

hth,

Craig

Offline

 

#41 2009-10-14 01:20:25 am

mbs402
Member
From:: Plano, Texas
Registered: 2009-04-07
Posts: 53
Website

Re: AppleScriptObjC in Xcode Part 1

Craig,

Here are the .h, .m.and AppleScriptObjC files.

This works great.

I am glad you knew the answer to my questions.

Thanks Again...

Bill Hernandez
Plano, Texas

// +---------+---------+---------+---------+---------+---------+
// [4325] ( BEGIN ) OBJECTIVE-C METHOD INTERFACE FILE
// The .h file:
// +---------+---------+---------+---------+---------+---------+
//  NSTextView+methods.h
//  tutorial_part_one
//
//  Created by Bill Hernandez on 10/14/09.
//  Copyright 2009 mac-specialist.com. All rights reserved.
// +---------+---------+---------+---------+---------+---------+

#import <Cocoa/Cocoa.h>

// +---------+---------+---------+---------+---------+---------+
// This was the default text when I created the new 
// 'Objective-C Class' File, so I commented it out
// +---------+---------+---------+---------+---------+---------+
//  @interface NSTextView_methods : NSObject {
// 
//  }
// 
//  @end
// +---------+---------+---------+---------+---------+---------+

// +---------+---------+---------+---------+---------+---------+
// METHOD INTERFACE TO EXTEND NSTextView by Craig Williams
//
// To add methods to any class you create a category.
//
// Add a new 'Objective-C Class' file to your project.
// Name it "NSTextView+methods."
//
// Add a new method, Xcode creates two files (an interface file, and an implementation)
// ( 1 ) NSTextView+methods.h
// ( 2 ) NSTextView+methods.m
// +---------+---------+---------+---------+---------+---------+
@interface NSTextView (methods)
-(NSString *)stringValue;
@end
// +---------+---------+---------+---------+---------+---------+
// [4325] ( _END_ ) OBJECTIVE-C METHOD INTERFACE FILE
// +---------+---------+---------+---------+---------+---------+


// +---------+---------+---------+---------+---------+---------+
// [4326] ( BEGIN ) OBJECTIVE-C METHOD IMPLEMENTATION FILE
// The .m file:
// +---------+---------+---------+---------+---------+---------+
//  NSTextView+methods.m
//  tutorial_part_one
//
//  Created by Bill Hernandez on 10/14/09.
//  Copyright 2009 mac-specialist.com. All rights reserved.
// +---------+---------+---------+---------+---------+---------+

#import "NSTextView+methods.h"

// +---------+---------+---------+---------+---------+---------+
// This was the default text when I created the new 
// 'Objective-C Class' File, so I commented it out
// +---------+---------+---------+---------+---------+---------+
//  @implementation NSTextView_methods
// 
//  @end
// +---------+---------+---------+---------+---------+---------+



// +---------+---------+---------+---------+---------+---------+
// METHOD IMPLEMENTATION TO EXTEND NSTextView by Craig Williams
//
// To add methods to any class you create a category.
//
// Add a new 'Objective-C Class' file to your project.
// Name it "NSTextView+methods."
//
// Add a new method, Xcode creates two files (an interface file, and an implementation)
// ( 1 ) NSTextView+methods.h
// ( 2 ) NSTextView+methods.m
// +---------+---------+---------+---------+---------+---------+
@implementation NSTextView (methods)
-(NSString *)stringValue
{
    return [self string];
}
@end
// +---------+---------+---------+---------+---------+---------+
// [4326] ( _END_ ) OBJECTIVE-C METHOD IMPLEMENTATION FILE
// +---------+---------+---------+---------+---------+---------+


-- +---------+---------+---------+---------+---------+---------+
-- [4327] ( _END_ ) AppleScriptObjC FILE
-- +---------+---------+---------+---------+---------+---------+
--  tutorial_part_oneAppDelegate.applescript
--  tutorial_part_one
--
--  Created by Bill Hernandez on 10/13/09.
--  Copyright 2009 mac-specialist.com. All rights reserved.
--
-- +---------+---------+---------+---------+---------+---------+
-- NOTES :
-- +---------+---------+---------+---------+---------+---------+
-- set time_stamp to my get_time_stamp(2)
--
-- ( n ) ---> format choices { 1, 2, 3 }
-- ( 1 ) ---> [2009.10.13](07.15.41.PM)
-- ( 2 ) ---> [ 2009.10.13 ] ( 07.14.43.PM )
-- ( 3 ) ---> 2009.10.13_07.15.15.PM
-- +---------+---------+---------+---------+---------+---------+

script tutorial_part_oneAppDelegate
    -- +---------+---------+---------+---------+---------+---------+
    -- Inheritance
    -- +---------+---------+---------+---------+---------+---------+
    property parent : class "NSObject"
    --Option DoubleClick on ---> NSTextView then click on the book icon
    --Option DoubleClick on ---> NSText then click on the book icon
   
    -- +---------+---------+---------+---------+---------+---------+
    -- IBOutlets   
    -- Interface Builder considers an outlet as any
    -- property with "missing value" as its value
    -- +---------+---------+---------+---------+---------+---------+
    property aWindow : missing value
    property textView : missing value
    property textField : missing value
   
    property show_message : false
   
    -- +---------+---------+---------+---------+---------+---------+
    -- IBActions (button clicks)
    -- Interface Builder considers an action as any
    -- single parameter method ending with an underscore
    -- +---------+---------+---------+---------+---------+---------+
    on setTextViewFromTextField_(sender)
        -- USER MODIFIABLE
        set which_update_method to 1 -- choices { 1, 2, 3 }
       
        -- date_object and current_date (NOT USED FOR NOW)
        set date_object to current date
        set current_date to (date_object as string)
       
        -- ( n ) ---> time_stamp format choices { 1, 2, 3 }
        -- ( 1 ) ---> [2009.10.13](07.15.41.PM)
        -- ( 2 ) ---> [ 2009.10.13 ] ( 07.14.43.PM )
        -- ( 3 ) ---> 2009.10.13_07.15.15.PM
        set time_stamp to my get_time_stamp(2) -- format choices { 1, 2, 3 }
       
        if (which_update_method is 1) then
            tell textField
                set textFieldValue to get stringValue()
            end tell
           
            tell textView
                set textViewValue to get stringValue() -- USE THE NEWLY DEFINED METHOD
                set newText to (textViewValue as string) & return & time_stamp & "  |  " & (textFieldValue as string)
                setString_(newText)
            end tell
           
           
        else if (which_update_method is 2) then
            tell textField
                set textFieldValue to get stringValue()
            end tell
           
            tell textView
                set textViewValue to get |string|()
                set newText to (textViewValue as string) & return & time_stamp & "  |  " & (textFieldValue as string)
                setString_(newText)
            end tell
           
            -- THIS ALSO WORKS OUTSIDE OF THE TELL BLOCK
            -- textView's setString_((textFieldValue as string) & return & (textViewValue as string))
           
        else        ---> (which_update_method is 3)
            set previousText to textView's |string|()
            set currentText to textField's stringValue()
           
            tell class "NSString" of current application
                set newText to its stringWithFormat_("%@%@%@%@", previousText, return, time_stamp & "  |  ", currentText)
            end tell
           
            textView's setString_(newText)
        end if
       
        if (show_message) then
            set x to textView's stringValue()        -- USE THE NEWLY DEFINED METHOD
            display dialog (x as string)
           
            display dialog (newText as string)
        end if
       
    end setTextViewFromTextField_
    -- +---------+---------+---------+---------+---------+---------+
    on get_time_stamp(which)
        if (which is 1) then
            set returnValue to do shell script "date '+[%Y.%m.%d](%I.%M.%S.%p)'"
            ---> [2009.10.13](07.15.41.PM)
        else if (which is 2) then
            set returnValue to do shell script "date '+[ %Y.%m.%d ] ( %I.%M.%S.%p )'"
            ---> [ 2009.10.13 ] ( 07.14.43.PM )
        else if (which is 3) then
            set returnValue to do shell script "date '+%Y.%m.%d_%I.%M.%S.%p'"
            ---> 2009.10.13_07.15.15.PM
        else
            -- which is 1 or anything else
            set returnValue to do shell script "date '+[%Y.%m.%d](%I.%M.%S.%p)'"
            ---> [2009.10.13](07.15.41.PM)
        end if
        return returnValue
    end get_time_stamp
    -- +---------+---------+---------+---------+---------+---------+
    -- Application
    -- +---------+---------+---------+---------+---------+---------+
   
    on awakeFromNib()
        set time_stamp to my get_time_stamp(2) -- format choices { 1, 2, 3 }
       
        tell textView
            setTextContainerInset_({5.0, 5.0})
            setString_(time_stamp & "  |  " & "Bill Hernandez")
        end tell
        tell textField
            setStringValue_("Default text")
        end tell
       
        -- add this as the last item in the awakeFromNib handler
        aWindow's makeFirstResponder_(textField)
       
    end awakeFromNib
    -- +---------+---------+---------+---------+---------+---------+   
    on applicationWillFinishLaunching_(aNotification)
        -- Insert code here to initialize your application before any files are opened
    end applicationWillFinishLaunching_
    -- +---------+---------+---------+---------+---------+---------+
    on applicationShouldTerminate_(sender)
        -- Insert code here to do any housekeeping before your application quits
        return current application's NSTerminateNow
    end applicationShouldTerminate_
    -- +---------+---------+---------+---------+---------+---------+   
end script
-- +---------+---------+---------+---------+---------+---------+
-- [4327] ( _END_ ) AppleScriptObjC FILE
-- +---------+---------+---------+---------+---------+---------+

Offline

 

#42 2010-02-06 12:29:37 pm

Dylan Weber
Member
From:: IL, USA
Registered: 2009-08-07
Posts: 539

Re: AppleScriptObjC in Xcode Part 1

Applescript:

on setTextViewFromTextField_(sender)
       set textFieldValue to textfield's stringValue() as string
       set textfieldhistory to textFieldValue & return & textfieldhistory
       textView's setString_(textfieldhistory)
   end setTextViewFromTextField_

big_smile


I am addicted to this site.
Look, over 500 posts! I barley help! :O
My specialty is replacing common AppleScript commands with hard-to-work-with methods.

Offline

 

#43 2010-02-07 10:57:12 am

cherf
Member
Registered: 2010-02-06
Posts: 2

Re: AppleScriptObjC in Xcode Part 1

Hello Craig -

First, thanks very much for the effort here. After a year of reading Cocoa tutorials, IB tutorials and Xcode 3 Unleashed I was very happy to find a simple guide that was current. Most of the tutorials I've found have been based on older versions of Xcode/IB and have been nearly useless.

I just finished going through part one with good results but one thing doesn't work right. When I run the app, a window named "Window" pops up in front of the "PartOne" window. It's empty, you cant type anything in it, but you can dismiss it or put it in the background.

Where is this coming from and how can I get rid of it?

I'm running 10.6.2 with Xcode 3.2.1 and IB 3.2.1(740).

Thanks,

Scott.

Model: MacBook Pro Intel Core Duo, Mac Mini Intel Core Duo
AppleScript: 2.3
Browser: Safari 531.21.10
Operating System: Mac OS X (10.6)

Offline

 

#44 2010-02-07 02:26:42 pm

mark hunte
Member
From:: Hello, London Calling
Registered: 2006-03-20
Posts: 1030
Website

Re: AppleScriptObjC in Xcode Part 1

cherf wrote:

a window named "Window" pops up in front of the "PartOne" window. It's empty, you cant type anything in it, but you can dismiss it or put it in the background.

Where is this coming from and how can I get rid of it?

)


Sounds like for some reason you have an extra window in IB.
Just delete it
image

Last edited by mark hunte (2010-02-07 02:34:48 pm)


Tap,tap,tap , hello , testing 1..2,  1...2,
http://www.markosx.com/thecocoaquest/

Offline

 

#45 2010-02-07 02:32:47 pm

cherf
Member
Registered: 2010-02-06
Posts: 2

Re: AppleScriptObjC in Xcode Part 1

Thanks, you're right I did. Can't figure how it got there but there it was...

Thanks,
Scott.

Offline

 

#46 2010-08-27 10:46:55 am

fdenger
Member
Registered: 2010-08-26
Posts: 4

Re: AppleScriptObjC in Xcode Part 1

The problem I had with the tutorial:

I believe that the tutorial assumes you know what cocoa is, what obj-c is, and what all these classes and parents mean. For a little old mac scripter (no pun intended) who just wants to run simple automations and create a GUI to connect his scripts into a nice interface.

All I wanted to do was learn how to take one script I have already made, and put it into this program and connect it to a button in the GUI. I finally figured it out after much puzzeling over what was necessary for this to happen.

AppleScript Studio assumes you are just a simple scripter and want to build a UI for your scripts. You can use it without any help. This new stuff AppleScript Objc assumes you are an XCode developer and you want to help your friends hook up their scripts into an GUI.

Anyway. thanks for the tutorial. But I think it might be appropriate to have smaller more specific tutorials on how to use the simplest most basic program (1 button connected to 1 script and nothing else) and then maybe build your way up as you add more and more controls to the program. Since the first tutorial includes how to use the strings and text areas and spends more time on them then the button (which was an afterthought) it made it difficult to determine exactly what is required to just simply have 1 button trigger to running a script.

Offline

 

#47 2010-08-27 06:50:48 pm

ief2
Member
Registered: 2010-07-23
Posts: 66
Website

Re: AppleScriptObjC in Xcode Part 1

A tip to all of you not knowing anything about Objective-C, C, inheritance and Xcode in particular, this is the guide which made me the programmer I'm now:

BecomeAnXcoder

Hope it helps,
ief2

Last edited by ief2 (2010-08-27 06:55:23 pm)


Find your magical software at www.wizardsoftware.tk.

Offline

 

#48 2010-08-29 12:02:48 pm

Craig Williams
Administrator
From:: Ft. Smith, AR
Registered: 2006-12-07
Posts: 888

Re: AppleScriptObjC in Xcode Part 1

@fdenger - I have felt the same way on so many occasions when going through a programming book or a tutorial. When things are over your head there can be some anger toward the author because they are not explaining it in a way that you understand.

Things that you might consider about these particular tutorials.
* They were not meant to be a comprehensive covering of AppleScriptObjC.
* I put them together just weeks after Snow Leopard was released to give others an overview of the new framework because there was relatively no other documentation at that time.
* The audience I was targeting was experienced AppleScript Studio users who just needed an overview to get started.

That being said, I want to reiterate that I understand your position. Programming is hard and it doesn't make it any easier to learn when tutorials expect you to already have a good grasp on things before reading them. You may have better success reading chapter 30 of "Learn AppleScript, The Comprehensive Guide to scripting and Automation on Mac OS X, Third Edition". I wrote that with a focus on more of a "ground up" approach. There are several example applications built during the chapter and the first one is a dialog attached to a button.

If you have specific questions then please post them to the forum. There are many wonderful people here who really go out of their way to help. And if you are serious about learning AppleScript, the best way is to start answering peoples questions on this forum. Myself and many others learned a lot about programming by doing this very thing.

Best,

Craig

Offline

 

#49 2010-09-23 04:03:43 pm

fdenger
Member
Registered: 2010-08-26
Posts: 4

Re: AppleScriptObjC in Xcode Part 1

Craig,

Thanks for the reply. I was just wanting to post my feedback. I think your tutorial is good I just think if you can't/don't have time to wrap your head around objC its difficult to follow along.

I'm working on a little modification to my application now. I want the window to stay on top because its a utility app for users of our database application... Here's my code (hopefully someone can tell me why it doesn't work):


Applescript:

on activated_(sender)
tell class "NSWindow" of current application
sender's setLevel(3)
end tell
end activated_

I'm sure its an obvious problem but I read all about how to setLevel in objC and I think i have it in the right place in my main applescript file in my project. I am not getting any errors in the debugger so I am scratching my head...

Thanks!

Offline

 

#50 2010-09-28 09:09:55 am

fdenger
Member
Registered: 2010-08-26
Posts: 4

Re: AppleScriptObjC in Xcode Part 1

Hi All,

Ok I did some research and figured out the answer to my question, after much playing around and searching. Here's a good tutorial on the subject to access the window methods from Applescript ObjC. The basic idea is you need to make a property with a missing value, then connect the window to that property. Once you do that you have an object you can call all the window's methods from. So for example:

Applescript:

property myWindow : missing value

on setTopLevel_(sender)
myWindow's setLevel(3)
end setTopLevel_

on setNormalLevel_(sender)
myWindow's setLevel(0)
end setNormalLevel_

So now go into IB and control-drag the window's titlebar to App Delegate and select myWindow to connect to. Then take a button in your window and control-drag it to to the App Delegate and select setTopLevel: to connect to. Then connect another button to setNormalLevel: Then compile and run and you will see that by clicking the buttons you can set the window to "floating" (top) level and the other button sets it back to normal. Note: make sure you put this code inside your AppDelegate script in XCode after the parent property. Otherwise cocoa won't know what your talking about.

Note that now that you have the window connected to the property "myWindow" you can access any of cocoa's NSWindow's methods by calling them like so:

Applescript:


myWindow's (any-NSWindow-method)

I got most of my information from:

http://www.applescriptwiki.com/applescr … lOfAWindow

Also as far as the most basic tutorial to just create 1 button in IB and connect it to just 1 function in applescript for the non-cocoa non-C programmer who doesn't want nor care about C or ObjC or Cocoa, I wrote it down here:

http://fatcpu.blogspot.com/2010/09/appl … basic.html

Thanks again Craig for your initial tutorial and getting me started. I'm glad this info was here.

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)