2
\$\begingroup\$

I'm writing a sub-routine in AppleScript to get text from a file and insert that text in a list of lists forming a dictionary.

Text File:

a:b
c:d
e:f

Expected list:

{{"a", "b"}, {"c", "d"}, {"e", "f"}}

Here's my code:

on getDictionary(filePath)
    set dictionary to {}
    set dictionaryFile to paragraphs of (read POSIX file filePath)
    repeat with entry in dictionaryFile
        set AppleScript's text item delimiters to {":"}
        set delimitedEntry to every text item of entry
        copy delimitedEntry to the end of dictionary
    end repeat
    return dictionary
end getDictionary

I'm wondering if this is a good approach for my goal. It does produce the desired result.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.