Combine multi-line address data into one single line?

I’m looking for some help here with a script that needs to work with Apple Shortcuts on the Mac.

If I extract address data from a photo in shortcuts I get the address information in the format of:

Scalby Lodge, Burniston Road
Scarborough
England
YO13 0DA
United Kingdom

I’m looking for a script which can take that and reformat it into one line of text, i.e.:

Scalby Lodge, Burniston Road, Scarborough, England, YO13 0DA, United Kingdom

Adding the commas in between each line would be a bonus :slight_smile:

Is this possible? The input to the script would be a multi-line address and the desired output the single line…

set addr to "Scalby Lodge, Burniston Road
Scarborough
England
YO13 0DA
United Kingdom"

set listAddr to paragraphs of addr

set {TIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ", "}
set addr to listAddr as text
set AppleScript's text item delimiters to TIDs
addr
--> "Scalby Lodge, Burniston Road, Scarborough, England, YO13 0DA, United Kingdom"

Under the assumption that the address is formatted as you showed us.

That is wonderful many many thanks. It always amazes me how you guys can wrap your head round this sort of thing. :grinning:

And for the fun of it, the same in JavaScript:

`Scalby Lodge, Burniston Road
Scarborough
England
YO13 0DA
United Kingdom`.split('\n').join(', ');

split creates an array from the single lines. Then join concatenates the elements of this array, inserting ", " (comma followed by space) between them. So split acts like paragraphs of in AppleScript and join like the combination of set text delimeter to and listAddr as text.

Way cool, thanks guys!