Mac Forums

Mac Forums (http://www.mac-forums.com/forums/)
-   OS X - Development and Darwin (http://www.mac-forums.com/forums/os-x-development-darwin/)
-   -   Set variable to output of text field- xcode 4.5, applescript (http://www.mac-forums.com/forums/os-x-development-darwin/294510-set-variable-output-text-field-xcode-4-5-applescript.html)

dhmmjoph 02-03-2013 02:51 PM

Set variable to output of text field- xcode 4.5, applescript
 
Hello,
I am fairly new to applescript and very new to xcode, so excuse me if my question has a really obvious answer that I haven't been able to find for some reason.

I am writing an app in applescript to perform various tasks based on commands entered by the user. The idea is that there's a text field into which the user types a command. When they hit return, the app does something (e.g. 'email' opens mail, 'sqrt' finds the square root of a number, etc.)

I am using xcode to write this app and have successfully linked a text field to an action (type anything in the field, hit return, and a dialog comes up. I want to set the thing that is typed in the text field as a variable so that it becomes the command. I have googled extensively and found how to do it in xcode 3.x, but that code doesn't work in xcode 4.5. I also tried asking in the IRC Channel #xcode, but no one seems to be responding there. I thought this would be a fairly simple thing to do, but apparently it isn't.

How do I do this?

Thanks in advance for your help.
-dhmmjoph

Mark FX 02-03-2013 03:18 PM

Are you using vanilla Applescript, or ApplescriptObjC?

If your using Xcode I will assume your using the ApplescriptObjC language.

First you need to set up a property to represent the TextField at the top of your script
like this.

Code:

property myTextField : missing value
then save the file, and then in Interface Builder this property will show up as an IBOutlet in the file object, probably the AppDelegate object if thats where your code is, then drag from this outlet to the TextField in your window, to link this textField to the property.

Then in your IBAction method you can retrieve the text from the TextField like this.

Code:

set theText to myTextField's stringValue()
Hope this helps, I wont be able to reply to any other questions until Wednesday.

regards Mark

dhmmjoph 02-03-2013 03:33 PM

Quote:

Originally Posted by Mark FX (Post 1492865)
in Interface Builder this property will show up as an IBOutlet in the file object, probably the AppDelegate object if thats where your code is, then drag from this outlet to the TextField in your window, to link this textField to the property.

Sorry, I'm confused. Where should it show up? I added the code to my script but the "missing" was pink and the "value" wasn't.
Code:

property myTextField : missing value
Is this normal, or am I doing something wrong?

Mark FX 02-03-2013 03:41 PM

You hav'nt told us what Applescript language your using, or in which file your code is in, for example is it in the AppDelegate?

Mark

dhmmjoph 02-03-2013 03:43 PM

I assume I am using ApplescriptObjC, but I am not sure. I selected "cocoa-AppleScript Application" in xcode, does that determine the language? If not how do I find out?

My code is in the AppDelegate.

Mark FX 02-03-2013 03:59 PM

OK!

that means your working with the ApplescriptObjC language.

When you created the new project, at the top of the AppDelegate script file, you should have had this.

Code:

property parent : class "NSObject"
what you want is this.

Code:

property parent : class "NSObject"
property myTextField : missing value

then save the AppDelegate file, Then click the mainMenu.xib file, which will show you a main window, to the left of the window editor screen and the project navigation list there should be a blue cube called AppDelegate, right click this cube and you should see a connection menu, find the myTextField property item, add drag from this menu item to your textField, that will make a connection between the textfield and the property, this will enable you to read and write to the properties of the textField, and also call its class methods.

Then you can use the above stringValue() class method to retreive the text in the textField.

Hope this is clearer.

Regards Mark

dhmmjoph 02-03-2013 04:16 PM

I attempted to follow these instructions, but it still didn't work.
Here is a picture of the window that appears when I right-click the App Delegate in the Interface Builder:
http://i.imgur.com/lpLNJvT.png

My code in the AppDelegate looks like this:
Code:

       
    property parent : class "NSObject"
    property myTextField : missing value
   
    on test_(sender)
        set theText to myTextField's stringValue()
        if theText is equal to "123" then
            display dialog "Yay! This works!"
        end if
    end test_

When I run the app, type '123' in the box and hit return, nothing happens. Am I doing something else wrong?

Mark FX 02-03-2013 04:30 PM

Try this in your IBAction method, also check the connection from the textField to the method.

Code:

if sender is equal to myTextField then
    set thetext to sender's stringValue()
end if

Mark

dhmmjoph 02-03-2013 04:37 PM

My code now looks like this:
Code:

property parent : class "NSObject"
    property myTextField : missing value
   
    on test_(sender)
        if sender is equal to myTextField then
            set theText to sender's stringValue()
        end if
        if theText is equal to "123" then
            display dialog "Yay! This works!"
        end if
    end test_

The additional code did not fix the problem.

I am unsure what you mean by "check the connection from the textField to the method"

Mark FX 02-03-2013 04:40 PM

Is the textField actually firing the method, check this with a log statement.

Code:

log "Got here"
Log stements will print out to the terminal at the bottom of the editor screen.

dhmmjoph 02-03-2013 04:44 PM

with the log here:
Code:

if theText is equal to "123" then
            log "Got here"
            display dialog "Yay! This works!"
        end if

nothing showed up in the log.

With the log here:
Code:

        if sender is equal to myTextField then
            log "Got here"
            set theText to sender's stringValue()
        end if

the log showed up.

Mark FX 02-03-2013 04:46 PM

I've just started a new project with a textField connected to a method with this method and it works fine.

Code:

    on test_(sender)
        log sender's stringValue()
    end test_


dhmmjoph 02-03-2013 04:57 PM

I started a new project and used the same code. I then added to it:
Code:

on test_(sender)
        log sender's stringValue()
        set theText to sender's stringValue()
        log theText
    end test

the text I entered showed up twice in the log, indicating that the variable is being written.

However, if I try to display the variable in a dialog:
Code:

on test_(sender)
        log sender's stringValue()
        set theText to sender's stringValue()
        display dialog (theText)
    end test

then an error shows up in the log:
Quote:

2013-02-03 15:56:10.341 Test[24347:303] *** -[AppDelegate test:]: «class ocid» id «data optr00000000D0C47471FF7F0000» doesn’t understand the «event sysodlog» message. (error -1708)

Mark FX 02-03-2013 05:05 PM

The problem you have is that the text returned by the NSTextField is of a class called NSCFString, this needs to be converted into an Applescript text class like this.

Code:

    on test_(sender)
        set theText to sender's stringValue() as text --see here
        if theText is equal to "Hello World"
            display dialog theText
        end if
    end test_

See how i've converted the senders stringvalue() into type Applescript text

I mostly work in the Objective C language, so dont do much Applescript these days, so you have to make sure that you convert into a class that Applescript understands, to check the class of an object do this.

Code:

    on test_(sender)
        log class of sender's stringValue()
    end test_

That should do you.

Regards mark

dhmmjoph 02-03-2013 05:08 PM

This Works!

Thanks!


All times are GMT -4. The time now is 08:54 AM.

Powered by vBulletin
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.