New To Mac-Forums?

Welcome to our community! Join the discussion today by registering your FREE account. If you have any problems with the registration process, please contact us!

Get your questions answered by community gurus • Advice and insight from world-class Apple enthusiasts • Exclusive access to members-only contests, giveaways and deals

Join today!

 
Start a Discussion
 

Mac-Forums Brief

Subscribe to Mac-Forums Brief to receive special offers from Mac-Forums partners and sponsors

Join the conversation RSS
OS X - Development and Darwin Discussion and questions about development for Mac OS X.

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


Post Reply New Thread Subscribe

 
Thread Tools
dhmmjoph

 
Member Since: Oct 03, 2012
Location: WV
Posts: 30
dhmmjoph is on a distinguished road
Mac Specs: 2012 Mac Mini 2.5ghz OSX 10.8.2; Black Macbook (late 2006) OSX 10.6.8; iPhone 4; 3rd Gen iPod Touch

dhmmjoph is offline
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
QUOTE Thanks
Mark FX

 
Mark FX's Avatar
 
Member Since: Aug 13, 2011
Location: West Sussex, UK
Posts: 168
Mark FX has a spectacular aura about

Mark FX is offline
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
QUOTE Thanks
dhmmjoph

 
Member Since: Oct 03, 2012
Location: WV
Posts: 30
dhmmjoph is on a distinguished road
Mac Specs: 2012 Mac Mini 2.5ghz OSX 10.8.2; Black Macbook (late 2006) OSX 10.6.8; iPhone 4; 3rd Gen iPod Touch

dhmmjoph is offline
Quote:
Originally Posted by Mark FX View Post
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?
QUOTE Thanks
Mark FX

 
Mark FX's Avatar
 
Member Since: Aug 13, 2011
Location: West Sussex, UK
Posts: 168
Mark FX has a spectacular aura about

Mark FX is offline
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
QUOTE Thanks
dhmmjoph

 
Member Since: Oct 03, 2012
Location: WV
Posts: 30
dhmmjoph is on a distinguished road
Mac Specs: 2012 Mac Mini 2.5ghz OSX 10.8.2; Black Macbook (late 2006) OSX 10.6.8; iPhone 4; 3rd Gen iPod Touch

dhmmjoph is offline
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.
QUOTE Thanks
Mark FX

 
Mark FX's Avatar
 
Member Since: Aug 13, 2011
Location: West Sussex, UK
Posts: 168
Mark FX has a spectacular aura about

Mark FX is offline
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
QUOTE Thanks
dhmmjoph

 
Member Since: Oct 03, 2012
Location: WV
Posts: 30
dhmmjoph is on a distinguished road
Mac Specs: 2012 Mac Mini 2.5ghz OSX 10.8.2; Black Macbook (late 2006) OSX 10.6.8; iPhone 4; 3rd Gen iPod Touch

dhmmjoph is offline
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:


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?
QUOTE Thanks
Mark FX

 
Mark FX's Avatar
 
Member Since: Aug 13, 2011
Location: West Sussex, UK
Posts: 168
Mark FX has a spectacular aura about

Mark FX is offline
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
QUOTE Thanks
dhmmjoph

 
Member Since: Oct 03, 2012
Location: WV
Posts: 30
dhmmjoph is on a distinguished road
Mac Specs: 2012 Mac Mini 2.5ghz OSX 10.8.2; Black Macbook (late 2006) OSX 10.6.8; iPhone 4; 3rd Gen iPod Touch

dhmmjoph is offline
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"
QUOTE Thanks
Mark FX

 
Mark FX's Avatar
 
Member Since: Aug 13, 2011
Location: West Sussex, UK
Posts: 168
Mark FX has a spectacular aura about

Mark FX is offline
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.
QUOTE Thanks
dhmmjoph

 
Member Since: Oct 03, 2012
Location: WV
Posts: 30
dhmmjoph is on a distinguished road
Mac Specs: 2012 Mac Mini 2.5ghz OSX 10.8.2; Black Macbook (late 2006) OSX 10.6.8; iPhone 4; 3rd Gen iPod Touch

dhmmjoph is offline
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.
QUOTE Thanks
Mark FX

 
Mark FX's Avatar
 
Member Since: Aug 13, 2011
Location: West Sussex, UK
Posts: 168
Mark FX has a spectacular aura about

Mark FX is offline
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_
QUOTE Thanks
dhmmjoph

 
Member Since: Oct 03, 2012
Location: WV
Posts: 30
dhmmjoph is on a distinguished road
Mac Specs: 2012 Mac Mini 2.5ghz OSX 10.8.2; Black Macbook (late 2006) OSX 10.6.8; iPhone 4; 3rd Gen iPod Touch

dhmmjoph is offline
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)
QUOTE Thanks
Mark FX

 
Mark FX's Avatar
 
Member Since: Aug 13, 2011
Location: West Sussex, UK
Posts: 168
Mark FX has a spectacular aura about

Mark FX is offline
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
QUOTE Thanks
dhmmjoph

 
Member Since: Oct 03, 2012
Location: WV
Posts: 30
dhmmjoph is on a distinguished road
Mac Specs: 2012 Mac Mini 2.5ghz OSX 10.8.2; Black Macbook (late 2006) OSX 10.6.8; iPhone 4; 3rd Gen iPod Touch

dhmmjoph is offline
This Works!

Thanks!
QUOTE Thanks

Post Reply New Thread Subscribe


« Importing SDK containing library | Is it possible to send your programming code to remote server? Through terminal? »
Thread Tools

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off
Forum Jump

Similar Threads
Thread
Thread Starter
Forum
Replies
Last Post
Simple question, If greater than x and less than y, how? #Applescript aaon OS X - Development and Darwin 1 06-08-2010 01:34 AM
Applescript help. Please!!! Raiden2205 OS X - Development and Darwin 1 05-06-2010 12:23 PM
Applescript Help req. as script not working under 10.6.2 argeetee OS X - Development and Darwin 0 03-14-2010 05:20 AM
How to set text color in a NSPopupButtonCell in an NSTableView tmeehan OS X - Development and Darwin 0 03-19-2009 12:23 PM
applescript text field help MrGecko OS X - Development and Darwin 0 03-24-2007 11:37 AM

All times are GMT -4. The time now is 10:37 PM.

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

Welcome to Mac-Forums.com

Create your username to jump into the discussion!

New members like you have made this community the ultimate source for your Mac since 2003!


(4 digit year)

Already a member?