hi, can you create a record dynamically in applescript using variables as the name (key) and value pairs. if so, how?
I have been trying like this (have simplified code):
set R to {name:“Ray”}
set key2 to “key2”
set value2 to “Value2”
set R to R & {key2:value2}
set key2 to “key3”
set value2 to “Value3”
set R to R & {key2:value2}
log R
It seems that the first time I assign the key as key2 it applies the name of the variable, not it’s value. As a cosequence, the second attempt to creat a new pair fails. Does anyone know how to force the value?
Hi
Here one suggestion (using “run script” method):
set R to {name:"Ray"}
set key2 to "key2"
set value2 to "Value2"
set R to R & (run script "return {" & key2 & ":"" & value2 & ""}")
return R --> {name:"Ray", key2:"Value2"}
– Original plain ApleScript solution (fixed for escapes):
set R to {name:"Ray"}
set key2 to "key2"
set value2 to "Value2"
set R to R & (run script "return {" & key2 & ":\"" & value2 & "\"}")
return R --> {name:"Ray", key2:"Value2"}
–
Other plain AppleScript solution:
set R to {name:"Ray"}
script o
property theKeys : {"Key2", "Key3"}
property theValues : {"Value2", "Value3"}
end script
-- Create a list containing keys subsequently followed by it's associated value
set rawList to a reference to {}
repeat with i from 1 to count o's theKeys
tell rawList to set {end, end} to {item i of o's theKeys, item i of o's theValues}
end repeat
-- Create a record who is similar to records containing only user defined keys in AppleEvents
set rawRecord to «class seld» of (record {«class usrf»:rawList} as record)
set R to R & rawRecord
–
AsObjC solution (for Yosemite (10.10) or later systems):
use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions
set theseLabels to {"name", "Key2", "Key3"}
set theseValues to {"Ray", "Value2", "Value3"}
recordFromLabelsAndValues(theseLabels, theseValues)
on recordFromLabelsAndValues(theseLabels, theseValues)
-- create a Cocoa dictionary using lists of keys and values
set theResult to ¬
current application's NSDictionary's dictionaryWithObjects:theseValues forKeys:theseLabels
-- return the resulting dictionary as an AppleScript record
return theResult as record
end recordFromLabelsAndValues
The “run script” method above can be written in the repeat loop form (to work with 2 given lists):
script o
property theKeys : {"name", "key2", "key3"}
property theValues : {"Ray", "Value1", "Value2"}
end script
set R to {}
repeat with i from 1 to count o's theKeys
set R to R & (run script "return {" & o's theKeys's item i & ":\"" & o's theValues's item i & "\"}")
end repeat
--> { key2:"Value2", key3:"Value3", name:"Ray"}
KniazidisR has answered the OP’s question but, FWIW, I thought I would add a second ASObjC solution. In testing with a record that contained 25 properties, the first script in post 5 above and my ASObjC solution both took less than one millisecond.
use framework "Foundation"
use scripting additions
set theRecord to {firstname:"John"} -- use |name| if label is name
set theLabel to "lastname"
set theValue to "Smith"
set theRecord to addToRecord(theRecord, theLabel, theValue)
on addToRecord(theRecord, theLabel, theValue)
set theDictionary to current application's NSMutableDictionary's dictionaryWithDictionary:theRecord
theDictionary's setObject:theValue forKey:theLabel
return theDictionary as record
end addToRecord