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
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
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.
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?
You can access them in a convoluted manner in some cases, but generally no.
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.
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:
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?
Your script behaves as expected under Sierra here…