Search code examples
listapplescriptrecordrepeatbbedit

How to turn an AppleScript list into a string


Trying to learn how to use AppleScript records and lists to their upmost potential I've been trying to create a report of a BBEdit project but I'm finding very limited documentation. I asked a question yesterday trying to figure out why my find pattern wasn't working but after finding out that the issue was from me lacking returning results:true I was able to get the result record and I verified it was a record after reading Class and running:

class of findFunction

Since it says it's a record I reviewed here and ran length of findFunction and count of findFunction and they both returned 2. I was curious to know what the two items were in the record so I used return findFunction and was told there was:

found: true
found matches: list of X items

Wanting to know where and what files the matches were located in the list, I did some more searching and read Lists and records and ran:

set theMatches to get found matches of findFunction

it returned the list items and checking the new variable with get count of theMatches I am able get the quantity of items in the targeted list inside the record. When I review what's in the list (learned from: How to get a value from a list with a string in AppleScript? and Searching for items in list) I am able to conclude that when using the find in BBEdit every item in the list contains:

end_offset : 
match_string :
message :
result_file :
result_kind :
result_line :
start_offset :

Experimenting with an item I set a variable with:

set itemOne to get item 1 of theMatches

and checked to see if it worked with:

display dialog (result_file of itemOne) as text

and a dialog with the full file path was displayed. Trying to utilize DRY I created:

set filesResult to get (result_file of (get item 1 of theMatches)) as text

Wanting to add any of the mentioned above to a file with something like:

set filesResult to get (result_file of (get item 1 of theMatches)) as text
set theMessage to get (message of (get item 1 of theMatches)) as text
set combined to filesResult & ":" & theMessage

I recalled being able to use the clipboard and found Set clipboard to Applescript variable? so I added:

set filesResult to the clipboard
make new text document
paste

but my issue I'm running into is how can I take every item in the list found_matches and add it to the clipboard an item on each line? I thought about using a repeat but I get an error when I try:

repeat with x from 1 to (length of matchesItems)
    set filesResult to get (result_file of (get item x of theMatches)) as text
    set theMessage to get (message of (get item x of theMatches)) as text
    set combined to filesResult & ":" & theMessage
end repeat

With a message of:

The variable matchesItems is not defined.

So how can I get every item from the list into the clipboard with every item on it's own line so I can paste all items from the clipboard into a new file?


Solution

  • To clarify wording

    theList = {A,B,C} -- this is a list with 3 variables
    theRecord = {A:something, B:somethingElse, C:somethingElseTwo} -- this is a record.
    

    A list can be addressed by its index.

    theList's item 1 -- A
    

    A record can be addressed by its keys

    A of theRecord -- something
    

    To get all items of a list into a string repeat it by its index (saying every item is of type text)

    set finalString to ""
    repeat with thisItem in TheList
        set finalString to finalString & thisItem & return -- the return creates a new line
    end repeat
    

    Then you have finalString to do with whatever you like.

    To get every item of a record you have to know it's keys (if it's not a ASOC NSDictionary)

    set finalString to ""
    set finalString to finalString & A of theRecord & return;
    -- repeat last line with every key