5

Is there way to get a Record by the value of one of its keys without looping?

I've been doing this:

set theKey to 22

set theRecords to {{key:11, value:"foo"}, {key:22, value:"bar"}}
repeat with theRecord in theRecords
    if key of theRecord = theKey then
      display dialog (key of theRecord as text) & ":" & value of theRecord
    end if
end repeat
3

I suppose that you are worried about the speed of iterating. Iterating would be a time of order-n O(n).

In AppleScript, you can possibly use a record of records instead of a list of records, but I believe that then the class of the key would have to be an identifier -- not integer like in your example.

Here is a link to somebody with a similar question: Emulating associative arrays in AppleScript

Lauri Ranta's answer is to use the command-line tool called "awk".

The solution that I toyed with was to make a faceless background app that would be what is called an "Agent" which AppleScript's could call upon for using its datastructures such as an associative array (also known as a dictionary or a Mapping.)

I didn't get my solution done. It's just an idea, but you could use Lauri's answer, she usually knows what she's talking about.

Since I know a little Pyton, and since Pyton comes with OS X, pre-installed in the System Folder, I would consider using Python myself.

I tried it again in "pure AppleScript" with the following code which I'm not really happy with:

set theRecords to {fooKey:{key:"fooKey", value:"fooValue"}, barKey:{key:"barKey", value:"barValue"}}
try
    set theRecord to get barKey of theRecords
    display dialog (key of theRecord & ":" & value of theRecord)
on error errorMessage number errorNumber
    if errorNumber is not equal to -128 then
        display dialog "No match"
    end if
end try

You can mix shell script and AppleScripts:

-- in AppleScript, you can call a shell script
do shell script "<enter your shell script here>"

Or, you can call an AppleScript from a shell script with the command-line tool called "osascript".

1
  • 2
    I had exactly the same question. What I don't understand is why AppleScript does not allow the following one-liner: (every item in theRecords whose key is theKey) or (first item in theRecords whose key is theKey). This would be very powerful and similar to Python's list comprehensions. – Bill Nov 26 '17 at 22:44
0

using Foundation framework

use AppleScript version "2.7"
use scripting additions
use framework "Foundation"

to get_item from pool for item_name
    set poolData to my (NSDictionary's dictionaryWithDictionary:pool)
    set poolDataKeys to poolData's allKeys()
    if (poolDataKeys as list) contains item_name then
        set item_value to poolData's valueForKey:item_name
        return item_value as text
    else
        return false
    end if
end get_item


set keyName to "circle"
set pool to {box:"BOX A", circle:"CIRCLE B"}
set value to get_item from pool for keyName

tell application "System Events"
    set keyName to "circle"
    set pool to {box:"BOX A", circle:"CIRCLE B"}
    set value to get_item of me from pool for keyName
end tell

from 1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .