Tuesday, February 21, 2017

#1 2016-11-23 12:52:57 am

davidhmorgan
Member
From: Sydney
Registered: 2014-08-19
Posts: 48

Save result of Dialog Toolkit to an AppleScript property

Hi everybody,

I'm using Shane's Dialog Toolkit and trying to save the result of a text input as an AppleScript property. The idea is that after entering a name in the text field in the dialog box, next time I run the script it will remember the name I typed in last time.

Seems pretty simple.

When I run the following code repeatedly in Script Editor (taking care to run in the foreground), the property gets updated correctly to whatever name I type into the text field and the new name appears as the placeholder text in the dialog box next time I run it.

But when I export it as an app, it does not get updated and always repeats the initial name.

Please send help.
David

Applescript:


property MyName : "Barry"

tell script "Dialog Toolkit"
   
   check for main -- make sure we are in foreground when testing
   
   -- to make it look better, we can get the length of the longest label we will use, and use that to align the controls
   set theLabelStrings to {"This job is for:", "Operator's name:"}
   set maxLabelWidth to max width for labels theLabelStrings
   set controlLeft to maxLabelWidth + 6
   
   -- a LABELED FIELD (Operator's field)
   set {operatorField, operatorLabel, theTop, fieldLeft} to create side labeled field MyName placeholder text "Your name" left inset 0 bottom 0 total width 400 label text "Operator's name:" field left controlLeft
   
   -- make list of controls and pass to display command
   set allControls to {operatorField, operatorLabel}
   
   -- controlResults will in the same order as allControls
   set {buttonName, suppressedState, controlResults} to display enhanced alert "Send for output" message "More text goes in here" as informational buttons {"Cancel", "OK"} giving up after 120 acc view width 400 acc view height theTop acc view controls allControls without suppression
end tell

set MyName to item 1 of item 3 of result

Offline

 

#2 2016-11-23 04:52:30 am

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

Re: Save result of Dialog Toolkit to an AppleScript property

In the normal state of affairs, when a script is run and any of its top-level variables has its value changed, the whole script containing the new values is saved to disk. That's how properties are persisted.

But there are times it can't happen: the file is code-signed and therefore locked, it's on a write-only volume, or one of the variable contains a Cocoa pointer. In that case, the file can't be resaved so properties don't change.

Some of your top-level variables contain Cocoa pointers. So you have two choices: save the value another way, such as using user defaults, or putting the relevant code in a handler so that the variables containing the Cocoa stuff are no longer at the top level of the script.


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

Offline

 

#3 2016-11-23 06:32:54 am

davidhmorgan
Member
From: Sydney
Registered: 2014-08-19
Posts: 48

Re: Save result of Dialog Toolkit to an AppleScript property

Well I'll be... Putting the tell block (and the line that sets "MyName" to the result) into a handler was all that was required. Thanks Shane!

I never considered that ordinary variables would be saved with the app, I thought that only happened with properties. I've noticed that Script Debugger does show the values of ordinary variables from the last time a script was run. But you can't actually do anything with these values can you? Is it just debris?

Offline

 

#4 2016-11-23 06:41:03 am

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

Re: Save result of Dialog Toolkit to an AppleScript property

davidhmorgan wrote:

I've noticed that Script Debugger does show the values of ordinary variables from the last time a script was run. But you can't actually do anything with these values can you? Is it just debris?

You can access them in a convoluted manner in some cases, but generally no.


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

Offline

 

#5 2016-11-23 07:02:17 am

davidhmorgan
Member
From: Sydney
Registered: 2014-08-19
Posts: 48

Re: Save result of Dialog Toolkit to an AppleScript property

Now this also makes sense why Script Editor cannot save an AppleScript ObjC script after it runs without recompiling it first. The variables are still there and they contain ObjC pointers.

Offline

 

#6 2016-11-23 04:19:35 pm

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

Re: Save result of Dialog Toolkit to an AppleScript property

davidhmorgan wrote:

Now this also makes sense why Script Editor cannot save an AppleScript ObjC script after it runs without recompiling it first. The variables are still there and they contain ObjC pointers.

Right. If you sue Script Debugger, it notices you are using ASObjC and deals with the issue, offering to turn off persistence. That makes testing simpler.


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

Offline

 

#7 2016-11-23 11:19:13 pm

davidhmorgan
Member
From: Sydney
Registered: 2014-08-19
Posts: 48

Re: Save result of Dialog Toolkit to an AppleScript property

I don't want to labour this issue but it seems to have morphed into another subject area - Sierra. The above mentioned tweaks fixed the issue on El Capitan and Yosemite but after trying it on Sierra, it doesn't work again. That is, the values of properties do not persist in between running the script as an app.

I tried the following simple test and saved it as an app:

Applescript:


property MyValue : 1
display dialog MyValue
set MyValue to MyValue + 1

It should update the number every time I double click on it but instead it always shows the number 1. Is there something about Sierra and the persistence of properties that I need to know about?

Offline

 

#8 2016-11-23 11:50:44 pm

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

Re: Save result of Dialog Toolkit to an AppleScript property

Your script behaves as expected under Sierra here...


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

Offline

 

Board footer

Powered by FluxBB

[ Generated in 0.046 seconds, 8 queries executed ]

RSS (new topics) RSS (active topics)