Reputation: 45
I have a script that has an array of terms. I'd like to create variables based on each term. I'm attempting to count how many times each term is chosen by the user and log a number associated with the term (also from user input).
My history with jQuery leads me to want to do something like this:
set term + "_Count" to term + "_Count" + 1
set term + "_Log" to term + "_Log" + inputNum
However, (obviously) that sort of syntax is not possible with AppleScript. Is there a way to concatenate a string onto a variable name?
-- for more reference, I'm trying to avoid listing every term out as I try to set 2 variables related to each term. I've currently got a long If/Then statement to set each one when the user picks a term.
I've been searching everywhere but haven't found anything conclusive for my question. Perhaps I just don't know the proper terms to search or maybe my whole approach is incorrect. Any help would be appreciated.
Upvotes: 0
Views: 1216
Reputation: 3142
It's difficult to give an exact answer without seeing all of your code but you can use Concatenation to define new variables.
If you were to save this following code as an application, the item chosen and the amount of times it has been chosen, get stored in the script and the values get updated.
Again, it's difficult to pinpoint exactly what you need but this is an example of Concatenation and defining how many times an item has been chosen
property theUserChose : {"Project_1", "Project_2", "Project_3"}
property term_1_count : 0
property term_2_count : 0
property term_3_count : 0
property minutes : 120
property term : missing value
set resultValue to choose from list theUserChose ¬
with title "Make Your Choice" OK button name ¬
"OK" cancel button name "Cancel" without empty selection allowed
set resultValue to resultValue as string
if resultValue is item 1 of theUserChose then
set term_1_count to term_1_count + 1
set term to resultValue & "_" & term_1_count & "_" & minutes
else
if resultValue is item 2 of theUserChose then
set term_2_count to term_2_count + 1
set term to resultValue & "_" & term_2_count & "_" & minutes
else
if resultValue is item 3 of theUserChose then
set term_3_count to term_3_count + 1
set term to resultValue & "_" & term_3_count & "_" & minutes
end if
end if
end if
display dialog term
Upvotes: 0
Reputation: 167
You really don’t. What you want is a dictionary
datatype (aka "hash", "map", "associative array", etc), which store and retrieve values using arbitrary keys (most commonly strings). AS doesn’t have a native dictionary type, but for storing simple value types (boolean, integer, real, string) you can use Cocoa’s NSMutableDictionary
class via the AppleScript-ObjC bridge:
use framework "Foundation"
set myDict to current application's NSMutableDictionary's dictionary()
myDict's setValue:32 forKey:"Bob"
myDict's setValue:48 forKey:"Sue"
(myDict's valueForKey:"Bob") as anything
--> 32
Upvotes: 4
Reputation: 285082
Short story:
Variable names are evaluated at compile time. Dynamic variables (evaluated at runtime) are not possible.
Upvotes: 0