Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Set variable to output of text field- xcode 4.5, applescript

    Member Since
    Oct 03, 2012
    Location
    WV
    Posts
    30
    Specs:
    2012 Mac Mini 2.5ghz OSX 10.10; Black MacBook (late 2006) OSX 10.6.8
    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

  2. #2
    Set variable to output of text field- xcode 4.5, applescript
    Mark FX's Avatar
    Member Since
    Aug 13, 2011
    Location
    West Sussex, UK
    Posts
    180
    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

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

    Member Since
    Oct 03, 2012
    Location
    WV
    Posts
    30
    Specs:
    2012 Mac Mini 2.5ghz OSX 10.10; Black MacBook (late 2006) OSX 10.6.8
    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?

  4. #4
    Set variable to output of text field- xcode 4.5, applescript
    Mark FX's Avatar
    Member Since
    Aug 13, 2011
    Location
    West Sussex, UK
    Posts
    180
    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

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

    Member Since
    Oct 03, 2012
    Location
    WV
    Posts
    30
    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.

  6. #6
    Set variable to output of text field- xcode 4.5, applescript
    Mark FX's Avatar
    Member Since
    Aug 13, 2011
    Location
    West Sussex, UK
    Posts
    180
    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

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

    Member Since
    Oct 03, 2012
    Location
    WV
    Posts
    30
    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:


    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?

  8. #8
    Set variable to output of text field- xcode 4.5, applescript
    Mark FX's Avatar
    Member Since
    Aug 13, 2011
    Location
    West Sussex, UK
    Posts
    180
    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

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

    Member Since
    Oct 03, 2012
    Location
    WV
    Posts
    30
    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"

  10. #10
    Set variable to output of text field- xcode 4.5, applescript
    Mark FX's Avatar
    Member Since
    Aug 13, 2011
    Location
    West Sussex, UK
    Posts
    180
    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.

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

    Member Since
    Oct 03, 2012
    Location
    WV
    Posts
    30
    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.

  12. #12
    Set variable to output of text field- xcode 4.5, applescript
    Mark FX's Avatar
    Member Since
    Aug 13, 2011
    Location
    West Sussex, UK
    Posts
    180
    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_

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

    Member Since
    Oct 03, 2012
    Location
    WV
    Posts
    30
    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)

  14. #14
    Set variable to output of text field- xcode 4.5, applescript
    Mark FX's Avatar
    Member Since
    Aug 13, 2011
    Location
    West Sussex, UK
    Posts
    180
    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

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

    Member Since
    Oct 03, 2012
    Location
    WV
    Posts
    30
    Specs:
    2012 Mac Mini 2.5ghz OSX 10.10; Black MacBook (late 2006) OSX 10.6.8
    This Works!

    Thanks!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Word 2011:Help! I can't paste text into the 'find' field
    By jonathanA in forum OS X - Apps and Games
    Replies: 8
    Last Post: 08-11-2015, 04:29 PM
  2. Applescript Curl and Variable Help
    By FallenAsh in forum OS X - Development and Darwin
    Replies: 3
    Last Post: 01-06-2013, 01:59 PM
  3. Replies: 1
    Last Post: 10-28-2009, 09:36 AM
  4. save container variable to file in applescript?
    By psychodork in forum OS X - Development and Darwin
    Replies: 0
    Last Post: 03-09-2009, 01:41 AM
  5. applescript text field help
    By MrGecko in forum OS X - Development and Darwin
    Replies: 0
    Last Post: 03-24-2007, 11:37 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •