The Address data which I can extract from a picture in Apple Photos is in the format of:
Market Square
Scottish Borders
Scotland
TD6
United Kingdom
I’m looking for a script which can be run within an Apple Shortcut to reformat it as follows:
Market Square, Scottish Borders, Scotland, TD6, United Kingdom
That is add a ‘comma’ & space to the end of the lines (excepting the last one) and then join them together to give a nicely formatted single line.
I’ve looked within the shortcut options in the Shortcuts app and can’t see anything as standard which can accomplish this.
Any help (as always) would be very much appreciated.
I’ve found this link, which suggests using the replace option to replace \n but this doesn’t do anything at all.
Try this. Can’t take any credit as I found the original script here, in a thread from 2015; Convert a List to a String - #34 by TecNik
set theList to {"Market Square", "Scottish Borders", "Scotland", "TD6", "United Kingdom"}
return join(theList, ", ")
on join(lst, sep)
tell AppleScript
set oldTIDs to text item delimiters
set text item delimiters to sep
set str to lst as string
set text item delimiters to oldTIDs
end tell
return str
end join
dbrewood. There are three shortcut approaches that will do what you want. The first is to use the Replace Text action with the Regular Expression option enabled. If the line endings are not a linefeed, replace backslash-n with backslash-R (the forum software won’t let me type the actual characters).
Create Address One.shortcut (21.7 KB)
The second is to split the text into a list and then to combine the list using a comma and a space as the new separator.
Create Address Two.shortcut (21.7 KB)
The third approach is a bit clumsy and probably should not be used unless you want to add additional text or to reformat the address in some way.
Assuming the Address data is one string containing multiple lines you can reformat the string with text item delimiters
as suggested in post #3 processing the paragraphs
of the string
set theAddress to "Market Square
Scottish Borders
Scotland
TD6
United Kingdom"
set {TID, text item delimiters} to {text item delimiters, ", "}
set formattedAddress to (get paragraphs of theAddress) as text
set text item delimiters to TID
display dialog formattedAddress buttons {"OK"} default button 1
Thanks guys (as always), the ‘Create Address One.shortcut’ shortcut worked perfectly, in my initial effort I’d not checked the ‘Regular Expression:’ option so of course it failed (argh idiot me).
I now need to parse that string to remove the post code and region data but I think I’m okay with that, if not I’ll look at the other options to see how they affect the data.
Very many thanks guys, I’'d been trying to achieve this all day 
dbrewood. I’m sure you have a solution in hand, but I thought I’d provide a few suggestions just in case anyone has a similar need down the road.
One approach is to make the address into a list and then to create a new string from items in the list:
Create Address Three.shortcut (22.0 KB)
A second approach is to use a regex with capture groups. If you look at the regex pattern, the capture groups are the portion of the pattern in parentheses. In the replacement, the capture groups are $1 through $5. You only need 3 capture groups to accomplish what you want, but it’s easier to follow the regex pattern with 5 capture groups. In actual use, the address substring may not be the very-first item in the string, and the regex pattern would need to be adjusted to account for this.
Create Address Four.shortcut (21.7 KB)
Thanks, the ‘Create Address Three.shortcut’ may come in handy. I’ve created a very long and complex set of logical steps to try and achieve what I want. The ‘problem’ I have is that the address seems to vary greatly as to what data is in the ‘address text’.
Address data can vary as:
Dryburgh Mains Farmhouse
Melrose
Scotland
TD6 0RQ
United Kingdom
Scalby Lodge, Burniston Road
Scarborough
England
YO13 0DA
United Kingdom
TD6
Melrose
Scotland
TD6
United Kingdom
Salt Pans Road
Salt Pans Road, Cloughton
Scarborough
England
YO13
United Kingdom
Usually I want to ignore the last two lines (Post Code and Country), and as you can see the first line, the street, can occasionally contain a duplicate of the post code, and in that case it should not be included.
Similarly the Town can also contain the street data,
It’s a flipping nightmare. You can get individual data out of the photo but those fields when added together do not contain all the data that is found in the ‘address text data’.
Okay in the ‘Create Address Three.shortcut’ how do you get the different index items into the text box, when I look to do similar in my shortcut each of the three list items contain the data from the first item.
[Update] Worked it put you have to use the variable chooser…
Now playing to see what I can achieve…
Very happy the ‘Create Address Three’ technique works is most cases excepting where the address data is weird. It’s allowed me to get rid of a heck of a lot of shortcut code.
If anyone is interested I can post a copy of the entire shortcut. It does need PixelMator Pro to run…
The aim of it is to select a random ‘favourite’ photo from my Apple Photos, adjust the size and crop where needed to fit the desktop, tint the picture by a defined amount (30% currently), overlay a ‘widget’ at the screen bottom, which shows; Address, ‘camera’ used, and the datestamp of when it was taken, finally a bit of debug data saying when it was set.
I know I’ve got a lot of variable cleaning up to do, there are probably better ways of doing some of things I’ve done. Planned:
- Settings moved into a dictionary
- Work around to get rid of duplicate Postcode data
- Work around to get rid of duplicate Street data
- Save out various 'debug data - ful address, Map URL, ’ etc into a defined Apple Note
- Look to see if the overlays can be done without using Pixelmator
- Option to add a ‘mini’ Map overlay image bottom right (may be too small to be useful)
A typical result is as per the attached… I’ve been working on this for 3 years now, this is the 7th rewrite of the shortcut so far 
dbrewood. I’m glad you’re making good progress. I don’t have PixelMator Pro, but I’d be interested to look at your shortcut to see how you’re handling different matters. Most of my shortcuts are relatively simple, but yours sounds very advanced.
I wonder if sorting out the address data could be done with a regular expression–perhaps using the logical steps you mention. The criteria that would be used to distinguish one address component from another would, of course, determine if this is possible.
Not a problem, I’ve just posted it here.
I look forward to any thoughts…
dbrewood. I downloaded your shortcut and had a quick look. I set the TintScreen variable to false, because I don’t have PixelMator Pro, and removed actions that reference that app. The shortcut ran without issue and set my wallpaper as shown below. I’ve never attempted a shortcut anywhere near this involved. Very nice. 
I’ll spend some time with the shortcut tomorrow to see how everything works.
Very glad to hear it worked okay for you. I’m amazed the widget overlay worked as I thought that needed Pixelmator functions. Looks an awesome picture 
FYI I actually run it automatically every hour using the Shortery App, that way I get a new wallpaper image on a regular basis.
I’d be very interested on your thoughts.
I’ve updated the shortcut, see the shortcut thread.