Friday, January 28, 2022

#1 2004-01-15 06:55:20 pm

RayC
Member
Registered: 2004-01-16
Posts: 3

Can you create a record dynamically in applescript?

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?

Thanks, Ray

Offline

 

#2 2004-01-16 04:32:34 am

Fredo d;o)
Member
From:: StÉ
Registered: 2003-02-11
Posts: 226

Re: Can you create a record dynamically in applescript?

Hi smile
Here one suggestion (using "run script" method):

Applescript:

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"}

wink


Fredo d;o)
"One step at time is enough for me..." (Gandhi)

Offline

 

#3 2004-01-16 02:58:08 pm

RayC
Member
Registered: 2004-01-16
Posts: 3

Re: Can you create a record dynamically in applescript?

Thanks.... that works a treat!

Ray

Offline

 

#4 2021-11-27 07:03:04 pm

mausfang
Member
Registered: 2020-03-18
Posts: 1

Re: Can you create a record dynamically in applescript?

Fredo d;o) wrote:

Hi smile
Here one suggestion (using "run script" method):

Applescript:

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"}

wink



Is something like this possible with recent applescript?

Can't get this code snippet to work, but would love to have dynamically named key/value-pairs.

Last edited by mausfang (2021-11-27 07:03:22 pm)

Offline

 

#5 2021-11-27 10:29:13 pm

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 2208

Re: Can you create a record dynamically in applescript?

It is possible:
--
--
Original plain ApleScript solution (fixed for escapes):

Applescript:


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:

Applescript:


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):

Applescript:


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

Last edited by KniazidisR (2021-11-27 11:19:11 pm)


Model: MacBook Pro
OS X: Catalina 10.15.7
Web Browser: Safari 14.1
Ram: 4 GB

Offline

 

#6 2021-11-27 11:31:05 pm

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 2208

Re: Can you create a record dynamically in applescript?

The "run script" method above can be written in the repeat loop form (to work with 2 given lists):

Applescript:


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"}

Last edited by KniazidisR (2021-11-27 11:31:43 pm)


Model: MacBook Pro
OS X: Catalina 10.15.7
Web Browser: Safari 14.1
Ram: 4 GB

Offline

 

#7 2021-11-28 08:07:55 am

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 1218

Re: Can you create a record dynamically in applescript?

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.

Applescript:

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

Last edited by peavine (2021-11-29 08:24:15 am)


2018 Mac mini - macOS Monterey - Script Debugger 8

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)