Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Rob64

macrumors newbie
Original poster
Sep 21, 2018
6
0
I'm working in Script Editor rather than Xcode at the moment. Borrowing from Shane Stanley's Dialog Toolkit Plus, I've got the following test field set up, but I can't seem to hook up the action handler to the text field and have it trigger continuously (or at all for that matter). It's been a while since I did some ASOC work in Xcode at my last job, but my memory of everything feels solid. What am I doing wrong here?

Code:
property Cocoa : current application
property testValue : missing value

on testAction(sender)
    set my testValue to (sender's stringValue) as text
end testAction

set theField to (Cocoa's NSTextField's alloc()'s initWithFrame:{{0, 0}, {100, 22}})
tell theField
    (its setEditable:true)
    (its setBezeled:true)
    its setAction:testAction
    its setContinuous:true
    its setTarget:me
    its setDelegate:me
    its setStringValue:""
end tell

My end goal is to use this in the accessory view in Myriad Tables. I'm able to get the field into an acc view and attach that to the table/window, and it displays correctly and allows me to enter text, but the action doesn't seem to trigger... I'm stumped.
 

Red Menace

macrumors 6502a
May 29, 2011
578
226
Colorado, USA
The declaration for your action handler should be:
on testAction:sender

Usually one of the delegate methods is used, but the selector for the setAction method is the handler name as text:
its setAction:"testAction:"
 

Rob64

macrumors newbie
Original poster
Sep 21, 2018
6
0
The declaration for your action handler should be:
on testAction:sender

Usually one of the delegate methods is used, but the selector for the setAction method is the handler name as text:
its setAction:"testAction:"

Oh brilliant! Thanks so much! I was hoping it would be you who saw this. Your answers in other threads have helped me many times.
[doublepost=1537573339][/doublepost]Also, is 'continuous' what I want to set to get the control to trigger its action any time something is typed in the field?
 

Red Menace

macrumors 6502a
May 29, 2011
578
226
Colorado, USA
Also, is 'continuous' what I want to set to get the control to trigger its action any time something is typed in the field?
Yes, setting the continuous property to true sends its action message continuously - for example, if you are using the controlTextDidChange delegate method, it would get called each time the field's text changes.
 

Rob64

macrumors newbie
Original poster
Sep 21, 2018
6
0
Yes, setting the continuous property to true sends its action message continuously - for example, if you are using the controlTextDidChange delegate method, it would get called each time the field's text changes.
That's what I was expecting. Unfortunately, whether I set continuous to true or false seems to have no effect on the field's behavior. I can only get the action to send when I hit enter while focused on the field. Any idea what's going on there?
 

Red Menace

macrumors 6502a
May 29, 2011
578
226
Colorado, USA
Well, that is the only action you are looking for (via your testAction handler). If you are wanting something else, look at adding a delegate method ( such as controlTextDidChange: ) to get notified when something else happens with the text field.
 

Rob64

macrumors newbie
Original poster
Sep 21, 2018
6
0
Well, that is the only action you are looking for (via your testAction handler). If you are wanting something else, look at adding a delegate method ( such as controlTextDidChange: ) to get notified when something else happens with the text field.
Yeah I had tried that, and controlTextDidChange: doesn't seem to do anything. Unless I'm missing something else you have to do other than override that function with a controlTextDidChange: handler. Does it need to be inside an OBJ-C class like you would have as AppDelegate in Xcode?
 

Red Menace

macrumors 6502a
May 29, 2011
578
226
Colorado, USA
Not unless you want that - setting the textField's delegate as you have to the script (me) tells it where to look for its delegate methods. What are you doing in the controlTextDidChange: handler?
 

Rob64

macrumors newbie
Original poster
Sep 21, 2018
6
0
Not unless you want that - setting the textField's delegate as you have to the script (me) tells it where to look for its delegate methods. What are you doing in the controlTextDidChange: handler?
I was trying to use it to do the same thing as the action handler, hoping that it would actually respond continuously, since the action only seems to want to send when return is pressed, regardless of the continuous state.

On the other hand, I realized that a popup button would better accomplish what I'm trying to do and since that will send its action whenever its clicked, the continuous thing is no longer an issue. I'd still love to figure it out, just for my own piece of mind. :p I've been able to successfully send actions from both a popup and a checkbox now. I'll paste below the command I've modified from Dialog Toolkit Plus.

Thanks for all the help, though!

Code:
on create popup entryList left inset theLeft : 0 bottom theBottom popup width theWidth initial choice defaultValue : 1 action actionHandler : missing value target actionTarget : missing value
    if actionTarget is missing value and actionHandler is not missing value then error "A 'me' object must be passed via the target parameter in order to set an action."
    
    if class of defaultValue is integer then set defaultValue to item defaultValue of entryList
    set thePopup to NSPopUpButton's alloc()'s initWithFrame:{{theLeft, theBottom}, {theWidth, 26}} pullsDown:false
    tell thePopup
        its addItemsWithTitles:entryList
        its selectItemWithTitle:defaultValue
    end tell
    if actionHandler is not missing value then
        tell thePopup
            its setTarget:actionTarget
            its setAction:actionHandler
        end tell
        actionTarget's performSelectorOnMainThread:actionHandler withObject:thePopup waitUntilDone:true
    end if
    return {thePopup, theBottom + 26}
end create popup
 

Red Menace

macrumors 6502a
May 29, 2011
578
226
Colorado, USA
Well, a text field isn't a button, so its default is to send an action when editing is completed. As for the delegate method, it is called for each change in the text field (i.e. while it is being edited), which also doesn't work the same as a button, so I'm thinking that a text field wasn't the control you were looking for.

With the delegate methods, a notification object is sent, not just the sender, so the equivalent of your action handler (although it would be called for each change) would be:

Code:
on controlTextDidChange:theNotification
    set my testValue to theNotification's object's stringValue as text
end controlTextDidChange:
 

Rob64

macrumors newbie
Original poster
Sep 21, 2018
6
0
Ah hah! I forgot the object sent to delegate methods was a notification. That explains everything. Thanks so much!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.