Friday, December 15, 2017

#1 2016-03-30 04:09:58 am

krwllms
Member
Registered: 2016-01-28
Posts: 14

Populate text field with variable and show text.

Here's my code:

Applescript:


set todayDate to do shell script "date '+%Y/%m/%d'"

set salesRef to "COMPANY-AB" & "-" & theCustomerInitials & "-" & todayDate & "-" & theCustomerID

set the clipboard to salesRef as text
display dialog salesRef as text buttons {"OK"} default button 1

Here's the UI:
sfRnpr6.png

I'd like to make the text 'Copied!' appear when the script is complete, but remain hidden until then, and also populate the text field at the bottom with the 'salesRef' variable, so the user get's an output.

Essentially then, I'll be able to remove the display dialog. But I can't work out how to do this sad

Last edited by krwllms (2016-03-30 05:28:42 am)

Offline

 

#2 2016-04-05 02:09:44 am

DJ Bazzie Wazzie
Member
From:: the Netherlands
Registered: 2004-10-20
Posts: 2727
Website

Re: Populate text field with variable and show text.

After you have bind the text field to your script property the only thing you need to do is:

Applescript:

property theTextField : missing value
…
theTextField's setStringValue:salesRef

Offline

 

#3 2016-04-05 02:22:02 am

krwllms
Member
Registered: 2016-01-28
Posts: 14

Re: Populate text field with variable and show text.

DJ Bazzie Wazzie wrote:

After you have bind the text field to your script property the only thing you need to do is:

Applescript:

property theTextField : missing value
…
theTextField's setStringValue:salesRef


Would this be binding the text field's value to the Model Key Path 'theTextField'?

Offline

 

#4 2016-04-05 02:24:49 am

krwllms
Member
Registered: 2016-01-28
Posts: 14

Re: Populate text field with variable and show text.

krwllms wrote:
DJ Bazzie Wazzie wrote:

After you have bind the text field to your script property the only thing you need to do is:

Applescript:

property theTextField : missing value
…
theTextField's setStringValue:salesRef


Would this be binding the text field's value to the Model Key Path 'theTextField'?


I'm getting:
"missing value doesn’t understand the “setStringValue_” message. (error -1708)"

Offline

 

#5 2016-04-05 02:36:51 am

DJ Bazzie Wazzie
Member
From:: the Netherlands
Registered: 2004-10-20
Posts: 2727
Website

Re: Populate text field with variable and show text.

When you start with a new project you have a blue cube in your interface builder view called something like "app delegate" (maybe prefixed with the project name). press and hold ctrl button and then drag from the blue cube (an line appears) to the text field and select the property from the application delegate. As in my example 'theTextField' would appear. In order to make this work the property needs to have the initial missing value as it's value.

This video may be outdated (using Xcode 3) but the basics remains the same:

https://www.youtube.com/watch?v=KrpV00srnv4

Offline

 

#6 2016-04-05 03:00:44 am

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

Re: Populate text field with variable and show text.

DJ Bazzie Wazzie wrote:

After you have bind the text field to your script property the only thing you need to do is:


Strictly spoken it's not a Cocoa binding, it's rather a connection to an outlet.

Binding is for example connecting the value of the text field to a string property via its model key path .


regards

Stefan

Offline

 

#7 2016-04-05 03:29:50 am

krwllms
Member
Registered: 2016-01-28
Posts: 14

Re: Populate text field with variable and show text.

This is a different script in the app, that is having an issue I can't work out.

---- Staff Pricer Properties
property theStaffCostPrice: missing value
property theStaffPriceResult: missing value

Applescript:


set staffPrice to (theStaffCostPrice * 1.02) * 1.2

set formattedPrice to (round_truncate(staffPrice, 2))

set the clipboard to formattedPrice as text

theStaffPriceResult's setStringValue:formattedPrice

'round_truncate' is a sub-routine I have elsewhere.

Every time I enter a value in the field and run, I get the error:

Can’t make «class ocid» id «data optr000000005534383336340000» into type number. (error -1700)

I have another script that deals with numbers the same, that throws up the same error.

Offline

 

#8 2016-04-05 04:12:18 am

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

Re: Populate text field with variable and show text.

I don't know what type both properties contain, but «class ocid» indicates a Cocoa object which must be coerced to an appropriate AppleScript type  for example the basic types NSString to text, NSNumber to integer or real, NSArray to list and NSDictionary to record


regards

Stefan

Offline

 

#9 2016-04-05 04:22:23 am

krwllms
Member
Registered: 2016-01-28
Posts: 14

Re: Populate text field with variable and show text.

StefanK wrote:

I don't know what type both properties contain, but «class ocid» indicates a Cocoa object which must be coerced to an appropriate AppleScript type  for example the basic types NSString to text, NSNumber to integer or real, NSArray to list and NSDictionary to record


I've added:

set costPrice to (theStaffCostPrice as string) as real

So it now reads:

Applescript:


property theStaffCostPrice: missing value

set costPrice to (theStaffCostPrice as string) as real

set staffPrice to (costPrice * 1.02) * 1.2

set formattedPrice to (round_truncate(staffPrice, 2))

set the clipboard to formattedPrice as text

theStaffPriceResult's setStringValue:formattedPrice

Working fine now.

Offline

 

#10 2016-04-05 04:40:22 am

DJ Bazzie Wazzie
Member
From:: the Netherlands
Registered: 2004-10-20
Posts: 2727
Website

Re: Populate text field with variable and show text.

StefanK wrote:
DJ Bazzie Wazzie wrote:

After you have bind the text field to your script property the only thing you need to do is:


Strictly spoken it's not a Cocoa binding, it's rather a connection to an outlet.


I didn't say it was Cocoa binding either smile. Cocoa binding is one of the many different kind of software bindings (UI data binding). But to keep close to Xcode's naming convention "connection" is a better term.

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)