Manan Shukla
Manan Shukla

Reputation: 69

How to update AppleScript List "Forever"

Look at the following code:

set TheStringsQ1Happy to {"Fabulous", "Great", "Alright", "Excited", "Not Bad", "", "Decent", "Fine", "Awesome", "Bored", "Cool", "Sad", "Fantastic", "Alright", "Good", "Ok"}

set theResponse to the text returned of (display dialog "" default answer "" giving up after 5)

if TheStringsQ1Happy contains theResponse then 
    display dialog "That's Great!" 
else
    say "That term is not in my vocabulary. Would you like me to add it?" using "Tom" speaking rate 220
    set theResponseNotInVocabulary to text returned of (display dialog "" default answer "" giving up after 5)

    if theResponseNotInVocabulary is "Yes" then 
       set end of TheStringsQ1Happy to theResponse
       return TheStringsQ1Happy
   end if

Although I can update TheStringsQ1Happy, this update only lasts the span of the script. How can I change the code so that every time I run the script, it also contains the updated vocabulary?

For example, if I said "All Good", the computer would recognize that the vocabulary is not on the list, and would later update this list only for that instance. How can I make it so that "All Good" stays for every instance from now on?

Upvotes: 0

Views: 100

Answers (3)

wch1zpink
wch1zpink

Reputation: 3142

This works for me using the latest version of Sierra

If this script is saved as an application,and you launch this new app... every time a new Item is added to the list, that new item will remain in the list every time you reopen the application. However if you open the application again in script editor,to edit the code and re-save... You will lose all of the values of the added list items And the whole cycle starts over again with the default list.

property TheStringsQ1Happy : {"Fabulous", "Great", "Alright", "Excited", "Not Bad", "", "Decent", "Fine", "Awesome", "Bored", "Cool", "Sad", "Fantastic", "Alright", "Good", "Ok"}

set theResponse to (display dialog "How Do You Feel Today?" default answer "" giving up after 25)

if text returned of theResponse is in TheStringsQ1Happy then
    display dialog "That's Great!"
else
    say "That term is not in my vocabulary. Would you like me to add it?" using "Tom" speaking rate 220
    set theResponseNotInVocabulary to display dialog "Add This Term" & " " & quote & text returned of theResponse & quote & " " & "To My Vocabulary?" buttons {"No", "Yes"} default button 2
    if the button returned of the result is "Yes" then
        if TheStringsQ1Happy does not contain text returned of theResponse then
            set end of TheStringsQ1Happy to text returned of theResponse
        end if
        return TheStringsQ1Happy
    end if
end if

Upvotes: 0

CJK
CJK

Reputation: 6102

Variables in AppleScript don't span outside the duration of execution of the script in which they are defined, as you've quite rightly noticed.

However, a property can, and will continue into subsequent executions of a script with the information left over from the previous execution.

Bear in mind, though, that a property will get reset (restored to its original value) each time the script is re-compiled (which happens whenever you make edits to it, or trigger it to compile manually).

With this in mind, change this line:

set TheStringsQ1Happy to {"Fabulous", "Great", "Alright", "Excited", "Not Bad", "", "Decent", "Fine", "Awesome", "Bored", "Cool", "Sad", "Fantastic", "Alright", "Good", "Ok"}

to this:

property TheStringsQ1Happy : {"Fabulous", "Great", "Alright", "Excited", "Not Bad", "", "Decent", "Fine", "Awesome", "Bored", "Cool", "Sad", "Fantastic", "Alright", "Good", "Ok"}

and you'll be good to go.


If you want a more permanent way to ensure you don't accidentally lose the new additions to this property, such as when you need to make any changes to the script in the future that will reset its value, then you'll need to store the information in an external file, which will serve as a sort of "dictionary" of vocabulary terms.

The simplest way to do this is to create a text file and put each item of the list on its own line, like this:

Fabulous
Great
Alright
Excited
...etc.

Save it as something like HappyTerms.txt, somewhere like your Documents folder, then change the variable declaration for TheStringsQ1Happy to this:

set TheStringsQ1Happy to the paragraphs of (read "/Users/%you%/Documents/HappyTerms.txt")

replacing %you% with the name of your Home folder in which your Documents folder lives. In fact, it's a useful idea to put the path to this text file in its own variable definition just beforehand:

set VocabDictionary to "/Users/%you%/Documents/HappyTerms.txt"
set TheStringsQ1Happy to the paragraphs of (read VocabDictionary)

Finally, to make changes to this file and add new terms to it, immediately after this line:

if theResponseNotInVocabulary is "Yes" then set end of TheStringsQ1Happy to theResponse

simply add either these lines:

set AppleScript's text item delimiters to return
write (TheStringsQ1Happy as text) to VocabDictionary starting at 1

OR this line

write "\n" & theResponse to VocabDictionary starting at (get eof VocabDictionary) + 1

The first version overwrites the entire file with all the terms in the new list. The second version simply appends the new addition to the end of the file. You might want to experiment with both and get the file turning out the way you want it to, as you'll sneakily discover that one might give you a stray blank line in the file that results harmlessly in an empty string "" appearing in your list; whilst the other does not; but I'll leave you to figure out if and why this happens, and—should you really want it not to happen—how to prevent it. 🙃 Either way, it shouldn't cause you any problems.

If you have any other queries, post a comment and I'll back to you. If this is helpful, don't forget to +1 my answer, and mark it as "correct" if solves your problem for you.

Upvotes: 0

user3439894
user3439894

Reputation: 7555

The following is strictly an example to help you with what you asked, not fix the broken code you posted.

If you run the following in Script Editor:

property theList : {1, 2, 3}
copy (count theList) + 1 to end of theList
log theList

You'll see theList as a property grow by 1 each time you run it, that is until the script is recompiled.

If you need absolute long term storage where nothing will be lost of anything added to theList, then you need to save to and retrieve from a disk file.

Upvotes: 1

Related Questions