How do i use an NSTextField in AppleScript?

Discussion in 'Mac Programming' started by Intelligent, Oct 5, 2014.

  1. Intelligent macrumors 6502a

    Intelligent

    Joined:
    Aug 7, 2013
    #1
    Im trying to make a code where AppleScript sets a variable from the text returned of a NSTextField

    Code:
    set variable to text returned of (NSTexTField)
    How would i do so that my AppleScript code recognizes that specific NSTextField and can set the text in it to a Variable?
     
  2. Red Menace macrumors 6502

    Joined:
    May 29, 2011
    Location:
    Littleton, Colorado, USA
    #2
    What OS/Xcode versions are you using? I am going to guess that you are using AppleScriptObjC in an Xcode project, in which case you would have the text field connected to a property, where you can use the various methods of the NSTextField class (and its parents) such as stringValue:

    Code:
    property myTextField: missing value -- connected to the text field in IB
    
    --
        set whatever to myTextField's stringValue() as text
     
  3. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #3
    Here's a simple tutorial that shows the basics using an outlet, then a more advanced version using bindings:
    http://www.peachpit.com/articles/article.aspx?p=1942301

    It creates an input text field where you type your name. You then press a button to display an alert with the entered name.

    As with all tutorials, I recommend completing the tutorial exactly as given. Only after completing it and getting a working result should you strike out on your own to apply what you've learned.

    Found by googling: cocoa applescript
     
  4. Intelligent, Oct 5, 2014
    Last edited: Oct 5, 2014

    Intelligent thread starter macrumors 6502a

    Intelligent

    Joined:
    Aug 7, 2013
    #4
    OS X 10.9.5, Xcode 6.1.
    Thank you for that.

    A very similar question (if you don't mind).

    I'm using this code to run a unix script, currently i have it like this

    Code:
    set variable to textField's stringValue() as text
        set mscript to do shell script "command " & variable
        display dialog mscript
    
    And instead of "displaying dialog" i want it to display the output on another text field.
    I did like you said and added

    Code:
        property textField2 : missing value
    and then i connected it to App Delegate in IB.

    However, i can not figure out how display the output on that textfield. Currently i use the display dialog script which works fine.
     
  5. Red Menace macrumors 6502

    Joined:
    May 29, 2011
    Location:
    Littleton, Colorado, USA
    #5
    If the text returned by your shell script is fairly short a text field will be fine, but for anything more than the width of your text field (especially if there are multiple lines) you may want to use a text view instead. For a text field, you would use something like:

    Code:
    textField2's setStringValue:mscript
    Take a look at the NSTextField Class Reference that I linked to earlier for the various methods available.
     
  6. Intelligent thread starter macrumors 6502a

    Intelligent

    Joined:
    Aug 7, 2013
    #6
    Alright thank you, i will try and read it.
     

Share This Page