Sunday, November 27, 2022

#1 2022-05-16 08:51:09 am

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

Creating and manipulating dictionaries

I occasionally use dictionaries in my scripts but only in bits and pieces. So, just for learning purposes, I created a rudimentary database which I've included below.

I am not satisfied with the script segment entitled "Modify a dictionary by key value". It works but I would like to eliminate the repeat loop. Also, I was unable to get the last script segment to work with a compound predicate and resorted to the use of two separate predicates. Thanks for any suggestions.

Applescript:

-- Revised 2022.05.19 to correct issues

use framework "Foundation"
use scripting additions

-- Create a mutable array of mutable dictionaries
set personList to {{firstname:"John", lastname:"Adams", age:60, occupation:"President"}, {firstname:"Samuel", lastname:"Adams", age:70, occupation:"Statesman"}, {firstname:"Thomas", lastname:"Jefferson", age:60, occupation:"President"}}
set personArray to current application's NSMutableArray's arrayWithArray:personList

-- Add a dictionary
set newRecord to {firstname:"Samuel", lastname:"Chase", age:80, occupation:"Supreme Court Justice"}
personArray's addObject:newRecord
set personList to personArray as list

-- Modify a dictionary by key value
set searchKey to "lastname"
set searchValue to "Chase"
set revisedData to {age:50, occupation:"Founding Father"}
set thePredicate to current application's NSPredicate's predicateWithFormat:"%K == %@" argumentArray:{searchKey, searchValue}
set theDictionary to personArray's filteredArrayUsingPredicate:thePredicate
if theDictionary's |count|() ≠ 1 then error "Multiple or no matches found"
(theDictionary's objectAtIndex:0)'s addEntriesFromDictionary:revisedData
set personList to personArray as list

-- Get dictionaries by key value (occupation in this case)
set searchKey to "occupation"
set searchValue to "President"
set thePredicate to current application's NSPredicate's predicateWithFormat:"%K == %@" argumentArray:{searchKey, searchValue} -- from Shane
set searchResults to (personArray's filteredArrayUsingPredicate:thePredicate) as list

-- Get dictionaries by two key values (firstname and lastname in this case)
set searchKeyOne to "firstname"
set searchValueOne to "Samuel"
set searchKeyTwo to "lastname"
set searchValueTwo to "Adams"
set thePredicate to current application's NSPredicate's predicateWithFormat:"%K == %@ AND %K == %@" argumentArray:{searchKeyOne, searchValueOne, searchKeyTwo, searchValueTwo}
set searchResults to (personArray's filteredArrayUsingPredicate:thePredicate) as list

Last edited by peavine (2022-05-19 09:22:04 am)


2018 Mac mini - macOS Monterey - Script Debugger 8

Online

 

#2 2022-05-16 06:10:12 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6797

Re: Creating and manipulating dictionaries

peavine wrote:

I am not satisfied with the script segment entitled "Modify a dictionary by key value". It works but I would like to eliminate the repeat loop.



Is there some reason you didn't use a predicate?

Also, I was unable to get the last script segment to work with a compound predicate and resorted to the use of two separate predicates. Thanks for any suggestions.



Your argumentArray should be a flat array, not an array of subarrays:

Applescript:

set thePredicate to current application's NSPredicate's predicateWithFormat:"%K == %@ AND %K == %@" argumentArray:{searchKeyOne, searchValueOne, searchKeyTwo, searchValueTwo}


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#3 2022-05-16 07:00:59 pm

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

Re: Creating and manipulating dictionaries

Thanks Shane. The predicate you suggest for my last script segment works great, and I've incorporated that in my first post above.

As regards my script segment entitled "Modify a dictionary by key value", the following does seem to work without a repeat loop. The fact that this involves an array of mutable dictionaries, and that I am editing an individual dictionary and not the dictionary as part of the array remains a source of some confusion, but I'll give this some thought.

Applescript:

use framework "Foundation"
use scripting additions

-- Create a mutable array of mutable dictonaries
set personList to {{firstname:"John", lastname:"Adams", age:60, occupation:"President"}, {firstname:"Samuel", lastname:"Adams", age:70, occupation:"Statesman"}, {firstname:"Thomas", lastname:"Jefferson", age:60, occupation:"President"}}
set personArray to current application's NSMutableArray's arrayWithArray:personList

-- Modify a dictionary by key value
set searchKey to "firstname"
set searchValue to "John"
set revisedData to {age:70, occupation:"Founding Father"}
set thePredicate to current application's NSPredicate's predicateWithFormat:"%K == %@" argumentArray:{searchKey, searchValue} -- from Shane
set theDictionary to (personArray's filteredArrayUsingPredicate:thePredicate)'s objectAtIndex:0
theDictionary's addEntriesFromDictionary:revisedData
set personList to personArray as list --> {{age:70, lastname:"Adams", occupation:"Founding Father", firstname:"John"}, {age:70, lastname:"Adams", occupation:"Statesman", firstname:"Samuel"}, {age:60, lastname:"Jefferson", occupation:"President", firstname:"Thomas"}}

Last edited by peavine (2022-05-16 07:39:10 pm)


2018 Mac mini - macOS Monterey - Script Debugger 8

Online

 

#4 2022-05-16 09:53:26 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6797

Re: Creating and manipulating dictionaries

peavine wrote:

The fact that this involves an array of mutable dictionaries, and that I am editing an individual dictionary and not the dictionary as part of the array remains a source of some confusion, but I'll give this some thought.



Remember that these variables are just pointers to values -- in this case, the same value.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#5 2022-05-17 06:46:37 am

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

Re: Creating and manipulating dictionaries

Thanks Shane--that makes sense. I've modified my script in post 1 to reflect this method to modify a dictionary.


2018 Mac mini - macOS Monterey - Script Debugger 8

Online

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)