Set variable to output of text field- xcode 4.5, applescript

Joined
Oct 3, 2012
Messages
30
Reaction score
0
Points
6
Location
WV
Your Mac's Specs
2012 Mac Mini 2.5ghz OSX 10.10; Black MacBook (late 2006) OSX 10.6.8
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
 
Joined
Aug 13, 2011
Messages
200
Reaction score
7
Points
18
Location
West Sussex
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
 
OP
D
Joined
Oct 3, 2012
Messages
30
Reaction score
0
Points
6
Location
WV
Your Mac's Specs
2012 Mac Mini 2.5ghz OSX 10.10; Black MacBook (late 2006) OSX 10.6.8
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:
[COLOR="Magenta"]property[/COLOR] myTextField : [COLOR="Magenta"]missing[/COLOR] value
Is this normal, or am I doing something wrong?
 
Joined
Aug 13, 2011
Messages
200
Reaction score
7
Points
18
Location
West Sussex
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
 
OP
D
Joined
Oct 3, 2012
Messages
30
Reaction score
0
Points
6
Location
WV
Your Mac's Specs
2012 Mac Mini 2.5ghz OSX 10.10; Black MacBook (late 2006) OSX 10.6.8
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.
 
Joined
Aug 13, 2011
Messages
200
Reaction score
7
Points
18
Location
West Sussex
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
 
OP
D
Joined
Oct 3, 2012
Messages
30
Reaction score
0
Points
6
Location
WV
Your Mac's Specs
2012 Mac Mini 2.5ghz OSX 10.10; Black MacBook (late 2006) OSX 10.6.8
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:
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?
 
Joined
Aug 13, 2011
Messages
200
Reaction score
7
Points
18
Location
West Sussex
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
 
OP
D
Joined
Oct 3, 2012
Messages
30
Reaction score
0
Points
6
Location
WV
Your Mac's Specs
2012 Mac Mini 2.5ghz OSX 10.10; Black MacBook (late 2006) OSX 10.6.8
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"
 
Joined
Aug 13, 2011
Messages
200
Reaction score
7
Points
18
Location
West Sussex
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.
 
OP
D
Joined
Oct 3, 2012
Messages
30
Reaction score
0
Points
6
Location
WV
Your Mac's Specs
2012 Mac Mini 2.5ghz OSX 10.10; Black MacBook (late 2006) OSX 10.6.8
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.
 
Joined
Aug 13, 2011
Messages
200
Reaction score
7
Points
18
Location
West Sussex
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_
 
OP
D
Joined
Oct 3, 2012
Messages
30
Reaction score
0
Points
6
Location
WV
Your Mac's Specs
2012 Mac Mini 2.5ghz OSX 10.10; Black MacBook (late 2006) OSX 10.6.8
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:
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)
 
Joined
Aug 13, 2011
Messages
200
Reaction score
7
Points
18
Location
West Sussex
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
 
OP
D
Joined
Oct 3, 2012
Messages
30
Reaction score
0
Points
6
Location
WV
Your Mac's Specs
2012 Mac Mini 2.5ghz OSX 10.10; Black MacBook (late 2006) OSX 10.6.8
This Works!

Thanks!
 
Joined
Aug 13, 2011
Messages
200
Reaction score
7
Points
18
Location
West Sussex
Good luck with it, I wont be around for a couple of days if you have any more questions.

Regards Mark
 

Shop Amazon


Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.
Top