0

I'd like to create an array of arrays, like so,

set TagList to {}
repeat with i from 1 to count of AllTasks
set taskName to item i of AllTasks
[SNIP]
set TaskX to {|name|:taskName, Duedate:taskDue, |Note|:taskNote, |id|:taskId}
set TagList to TagList & TaskX
end repeat

so that the result would be a list of multiple tasks with each task showing the various items above (name, due date, etc.). When I try this repeat, the TagList ends up only with the last TaskX in the iteration. If it worked correctly, I'd like to extract the taskName of the task whose |id| = "xzy".

Can someone please guide me on these two issues (iteration and extraction)? Thanks.

1
  • RE: "I'd like to create an array of arrays," -- JSYK AppleScript uses list not "array" and e.g. {|name|:taskName, Duedate:taskDue, |Note|:taskNote, |id|:taskId} is a record. Commented Sep 9, 2021 at 18:47

1 Answer 1

1

The problem is the line set TagList to TagList & TaskX. What you’re attempting to do (as I understand it) is add TaskX as a new item to the end of the list TagList. What you’re doing is concatenating a list and a record. The results are not what you’re expecting. I’m not sure that’s even defined in AppleScript.

The most common correct ways to append an item to a list in AppleScript are:

copy TaskX to the end of TagList

or

set TagList to TagList & {TaskX}

The first appends an item to the end of the list (you could also use set the end of TagList to TaskX). The second concatenates two lists by placing the item taskX into a list, and then concatenating it with TagList.

That the item you’re appending to the list is a record is irrelevant to the process of appending.

The second part of your question is more difficult, because it’s technically not possible. AppleScript’s lists don’t really care what’s inside them, and because of this you can’t request only those records with a particular value in a particular field.

What you’ll have to do is iterate through the list looking for that field; most likely creating a handler. It would likely look something like this (see Apple’s documentation About Handlers):

to findTask of taskList given id:desiredId
    repeat with i from 1 to the number of items of taskList
        if the id of item i of taskList is desiredId then
            return item i of taskList
        end if
    end repeat
end findTask

This example generates a list of twenty-one records, whose names are “Item 1”, “Item 2”, “Item 3”, and so on, whose due dates are the dates beyond the current date, sequentially, and whose id is the same as the item number in the name.

set TagList to {}
repeat with i from 1 to 21
    set taskName to "Item " & i
    set taskDue to the (current date) + i * days
    set TaskX to {name:taskName, duedate:taskDue, id:i}
    copy TaskX to the end of TagList
    --set TagList to TagList & {TaskX}
    --set the end of TagList to TaskX
end repeat

findTask of TagList given id:3

to findTask of taskList given id:desiredId
    repeat with i from 1 to the number of items of taskList
        if id of item i of taskList is desiredId then
            return item i of taskList
        end if
    end repeat
end findTask

The hardcoded call asking for the item with an id of 3 produces:

{name:"Item 3", duedate:date "Sunday, September 12, 2021 at 2:01:19 PM", id:3}

Adding get the name of the result after the findTask call produces "Item 3", which is the name of that arbitrarily-generated record.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.