Saturday, December 16, 2017

#1 2014-08-03 01:12:03 pm

aizimmer
Member
Registered: 2014-06-15
Posts: 10

NSTextField action sent on changed value

So I have a text field that I would like to run the following code when the text changes (you can tell it's a workaround for not being able to figure out drop areas for files but maybe I'm on the right path):

Applescript:

set single_string to (stringValue() of files_drop) as string
set AppleScript's text item delimiters to {return, linefeed, return & linefeed, linefeed & return}
set pre_list to text items of single_string as list
set AppleScript's text item delimiters to {}

set list_text to ""

repeat with i from 1 to number of items in pre_list
set this_item to item i of pre_list
set Applescript's text item delimiters to "/"
set text_item to last text item of this_item as text
set Applescript's text item delimiters to ""
set list_text to list_text & text_item & return & return
end repeat

files_list's setStringValue_(list_text)
files_drop's setHidden:1
files_list's setHidden:0
files_drop's displayIfNeeded()
files_list's displayIfNeeded()

The problem is in the interface builder the only options are sent on end editing and sent on enter only. Is there a way to make it so that this routine runs once the text changes (like altering the properties of the NSTextField on application launch for instance?) I tried with an NSTimer forcing a tab/return, but I can't think of a good condition to be present to make sure it doesn't keep running.

Thanks for anyone's help!

Offline

 

#2 2014-08-03 06:46:40 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5200

Re: NSTextField action sent on changed value

Do a search here and you will find code for handling drag-and-drop of files -- that's what you really need.

In the meantime:

Applescript:

set AppleScript's text item delimiters to {return, linefeed, return & linefeed, linefeed & return}
set pre_list to text items of single_string as list
set AppleScript's text item delimiters to {}


Just use:

Applescript:

set pre_list to paragraphs of single_string

Applescript:

repeat with i from 1 to number of items in pre_list
set this_item to item i of pre_list
set Applescript's text item delimiters to "/"
set text_item to last text item of this_item as text
set Applescript's text item delimiters to ""
set list_text to list_text & text_item & return & return
end repeat


Use:

Applescript:

set pre_list to current application's NSArray's arrayWithArray:pre_list
set list_text to (pre_list's valueForKey:"lastPathComponent") as list


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#3 2014-08-03 11:04:46 pm

x74353
Member
From:: Boston, MA, USA
Registered: 2012-02-21
Posts: 118

Re: NSTextField action sent on changed value

You can use controlTextDidChange and NSNotificationCenter with your NSTextField to detect string changes in the text field.


In all our decadence people die.

Offline

 

#4 2014-08-03 11:22:35 pm

aizimmer
Member
Registered: 2014-06-15
Posts: 10

Re: NSTextField action sent on changed value

Thanks for the help!

Offline

 

#5 2014-08-18 05:35:56 pm

bentoms
Member
Registered: 2008-10-21
Posts: 66
Website

Re: NSTextField action sent on changed value

x74353 wrote:

You can use controlTextDidChange and NSNotificationCenter with your NSTextField to detect string changes in the text field.


Hi! Me again.. can you please elaborate on the above?

My app isn't updating textfield values unless a user clicks another element or presses enter.

The above sounds like the solution i'm looking for.

Offline

 

#6 2014-08-18 05:40:29 pm

bentoms
Member
Registered: 2008-10-21
Posts: 66
Website

Re: NSTextField action sent on changed value

FWIW, this seemingly echo's my issue: http://stackoverflow.com/questions/1964 … pplescript

Offline

 

#7 2014-08-18 07:21:06 pm

x74353
Member
From:: Boston, MA, USA
Registered: 2012-02-21
Posts: 118

Re: NSTextField action sent on changed value

Hi,

It's pretty easy to set up. In Interface Builder, just set the delegate of your NSTextField(s) to your App Delegate object, and then add something like this to your AppDelegate.applescript file:

Applescript:


property aTextField : missing value
property aTextField2 : missing value

property aTextLabel : missing value


on controlTextDidChange_(aNotification)
   
   if aNotification's object() is aTextField then
       -- run whatever you want to,
       -- but here is an example of setting a label's string to the field's new string
       set newText to aTextField's stringValue()
       aTextLabel's setStringValue_(newText)
   end if


   if aNotification's object() is aTextField2 then
       -- run whatever you want to,
       -- but here is an example of setting a label's string to the field's new string
       set newText to aTextField2's stringValue()
       aTextLabel's setStringValue_(newText)
   end if

end controlTextDidChange_

Last edited by x74353 (2014-08-18 07:36:09 pm)


In all our decadence people die.

Offline

 

#8 2014-08-19 03:22:04 am

bentoms
Member
Registered: 2008-10-21
Posts: 66
Website

Re: NSTextField action sent on changed value

Thanks.. but I think i'm missing some steps, the textfields value is bound to netBootNameTextfield's value.

On send (which is on end editing) the below action is run:

Changing from (sender) to (aNotification) didn't work.. So what are the steps to register aNotification?

Applescript:


-- Make sure a name is specified for the NetBoot Image, error if not.
on netBootName_(sender)

-- If textfield is empty
if netBootNameTextField is missing value then

-- Set netBoot Name
set my netBootNameTextField to selectedOSdmgVersion & " AutoCasperNBI"

-- Display error to user
display dialog "Please select a Name for the NetBoot Image" with icon 0 buttons {"OK"}

--Log Action
set logMe to "Error: Name required for NetBoot Image"

-- Log To file
logToFile_(me)

else

--Log Action
set logMe to "Name: " & netBootNameTextField

-- Log To file
logToFile_(me)

end if

end netBootName_

Offline

 

#9 2014-08-19 09:07:09 am

bentoms
Member
Registered: 2008-10-21
Posts: 66
Website

Re: NSTextField action sent on changed value

I've found a workaround that works for me.

The app i'm writing has various textfield options in the XIB's, to close there are buttons.

I've created an action for the button that re-runs the sender actions for each textfield, this then has logic to alert the user or proceed.. seems to work for me.

As an example, here's the Time Server action:

Applescript:

-- Bound to Time Server Text field
on timeServerCheck_(sender)

-- If textfield is empty
if timeServerSelected is missing value then

-- Set netBoot Name
set my timeServerSelected to "time.apple.com"

-- Display error to user
display dialog "Please enter a Time Server" with icon 0 buttons {"OK"}

--Log Action
set logMe to "Error: Please enter a Time Server"

-- Log To file
logToFile_(me)

-- Update plist with selection
tell defaults to setObject_forKey_(timeServerSelected, "timeServerSelected")

-- Set to false so we don't proceed
set closeButtonPreCheckPassed to false

else

--Log Action
set logMe to "Time Server: " & timeServerSelected

-- Log To file
logToFile_(me)

-- Update plist with selection
tell defaults to setObject_forKey_(timeServerSelected, "timeServerSelected")

end if

end timeServerCheck_

That would trigger when enter was pressed or another text filed, but not on clicking the XIB's "Close" push button.

That now has an action that does the below, therefore rechecking where values are needed they are set.

Applescript:

-- Make sure all variables are set if enabled, if passed close options window
on closeOptionsWindowCheck_(sender)

-- reset value
set closeButtonPreCheckPassed to true

-- Verify that the description field has a value & reset & prompt if not
netBootDescriptionCheck_(me)

-- Bound to NetBoot Image Resize value
netBootImageExpandEnteredValue_(me)

-- Check the value entered in the ARD Username textfield
checkardUsername_(me)

-- Check the value entered in the ARD Password textfield
checkardPassword_(me)

-- Check the value entered in the vnc Password textfield
checkvncPassword_(me)

-- Check that a custom desktop image has been selected
checkcustomDesktopImagePath_(me)

-- Bound to Time Server Text field
timeServerCheck_(me)

-- Set to boolean of value
set closeButtonPreCheckPassed to closeButtonPreCheckPassed as boolean

-- Proceed if we've passed precheck
if closeButtonPreCheckPassed is true then

-- reload options from plist
retrieveDefaults_(me)

-- close options window
optionsWindow's orderOut_(null)

-- enable options
set my optionWindowEnabled to true

end if

end closeOptionsWindowCheck_

Offline

 

#10 2014-08-19 12:36:40 pm

x74353
Member
From:: Boston, MA, USA
Registered: 2012-02-21
Posts: 118

Re: NSTextField action sent on changed value

There's nothing special you need to do to get controlTextDidChange working. I'll list the steps in order, starting from scratch...

1. Create a new Cocoa-AppleScript application. Name it whatever you want
2. Under the IBOutlets comment, Add this to your AppDelegate.applescript file (without quotes): "property testTextField : missing value"
3. Open your MainMenu.xib file, add a new NSTextField from the object library to your NSWindow
4. Right-click on the NSTextField you just added to the NSWindow.
5. Click and drag a connection from the little circle next to "New Referencing Outlet" to the App Delegate (blue cube) object
6. Choose "testTextField" for the Referencing Outlet for the NSTextField
7. Right-click the NSTextField again and click and drag the little circle next to "delegate" (under Outlets) to the App Delegate (blue cube) object
8. Add the following handler to your AppDelegate.applescript file (after applicationWillFinishLaunching and before applicationShouldTerminate):

Applescript:


on controlTextDidChange_(aNotification)
   
   if aNotification's object() is testTextField then
       log "Handler is being called every time the string changes"
   end if
   
end controlTextDidChange_

9. Run your project, edit the contents of the text field (either by typing something in there or by dragging and dropping a file from Finder into it).
10. Check your Debug panel in Xcode to confirm that the handler is called every time a change occurs

Last edited by x74353 (2014-08-19 05:36:10 pm)


In all our decadence people die.

Offline

 

#11 2014-09-20 06:21:30 pm

bentoms
Member
Registered: 2008-10-21
Posts: 66
Website

Re: NSTextField action sent on changed value

Thanks x74353.

I'll bear that in mind with my next app!

Offline

 

#12 2015-10-30 07:47:18 am

ilexxey
Member
From:: Moscow
Registered: 2012-08-02
Posts: 94
Website

Re: NSTextField action sent on changed value

Hi, there!

I try to do a sample project with this example, but don't get an property object:

Applescript:


script AppDelegate
   property parent : class "NSObject"
   
   -- IBOutlets
   property theWindow : missing value
   property myText : missing value
   property textEntered : ""
   
   on applicationWillFinishLaunching:aNotification
       -- Insert code here to initialize your application before any files are opened
   end applicationWillFinishLaunching:
   
   on controlTextDidChange:aNotification
       if aNotification's object() is myText then
           log "Handler is being called every time the string changes"
           set newText to myText's stringValue()
           textEntered's setStringValue:newText
       else
           log "Do nothing"
       end if
   end controlTextDidChange:
   
   on applicationShouldTerminate:sender
       -- Insert code here to do any housekeeping before your application quits
       return current application's NSTerminateNow
   end applicationShouldTerminate:
   
end script

always got a message "Do nothing".

I have connected my text field in IB section "Outlets/delegate" to blue cube "Delegate", and section "Referencing Outlets/New referencing outlet" to blue cube "Delegate/Referencing Outlets/myText" (whose declared as "property myText : missing value").

What wrong?


Regards, Alex

Offline

 

#13 2015-10-30 07:58:33 am

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11482
Website

Re: NSTextField action sent on changed value

You have also to connect the delegate of the text field to the AppDelegate object


regards

Stefan

Offline

 

#14 2015-10-30 08:27:13 am

ilexxey
Member
From:: Moscow
Registered: 2012-08-02
Posts: 94
Website

Re: NSTextField action sent on changed value

Hi, Stefan

Yes, i have 2 connections – one with property Outlet, and one delegate with blue cube Delegate.

if i try to log my object

Applescript:

set theObject to aNotification's object()
       log theObject

i got a message "<NSTextField: 0x6000001e0600>"


Regards, Alex

Offline

 

#15 2015-10-30 08:34:09 am

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11482
Website

Re: NSTextField action sent on changed value

If you have only one text field you don't have to perform a type check, otherwise try the Cocoa equation method

Applescript:


if aNotification's object()'s isEqual:myText then


regards

Stefan

Offline

 

#16 2015-10-30 09:08:48 am

ilexxey
Member
From:: Moscow
Registered: 2012-08-02
Posts: 94
Website

Re: NSTextField action sent on changed value

Thank's, Stefan it works well smile

Last edited by ilexxey (2015-11-02 06:48:50 am)


Regards, Alex

Offline

 

#17 2015-10-30 05:47:26 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5200

Re: NSTextField action sent on changed value

FWIW, you can also use:

Applescript:

if aNotification's object() is myText() then

No idea why...:|


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#18 2015-11-02 03:34:04 am

ilexxey
Member
From:: Moscow
Registered: 2012-08-02
Posts: 94
Website

Re: NSTextField action sent on changed value

Hi, All.

Why

Applescript:

if aNotification's object() is myText() then

don't worked?


Regards, Alex

Offline

 

#19 2015-11-02 06:30:19 am

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5200

Re: NSTextField action sent on changed value

It works here...


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)