Using Text Item Delims to Rearrange "First Last" to "Last, First"

Hey everyone,

I’m trying to wrap my brain around the text item delimiters after reading the long post about it, but I’m clearly missing something. All I’m trying to do is copy a name (from a website) that is Firstname Lastname.
Then I intend to put the script into a textExpander snippet so that I can spit it out as Lastname, Firstname.

I thought it was easy to just say X is the clipboard, and then set Y to the last word of X & ", " & first word of X
But then I noticed that won’t work if they have a middle name or Jr. in their name

so now I have

set firstLast to the clipboard
changeName from firstLast

to changeName from t
	set d to AppleScript's text item delimiters
	set AppleScript's text item delimiters to " "
	if (count t's text items) > 1 then set t to (t's text items -1 thru -2) & "," & (first word of t)
	set t to t's text items
	tell t to set t to beginning & ({""} & rest)
	set AppleScript's text item delimiters to d
	return t
end changeName

This sort of is better, but now it won’t work if theres just two words in the name. And it also puts a space after the third word. Can someone with some wisdom help me on this? I’m sure it’s more simple than I’m making it.

after some tweaking I’ve now got

--set firstLast to the clipboard
set firstLast to "First Last Jr."
changeName from firstLast
to changeName from t
	set d to AppleScript's text item delimiters
	set AppleScript's text item delimiters to " "
	set t to (t's text items 2 thru -1) & "," & (t's text item 1)
	set t to t's text items
	tell t to set t to beginning & ({""} & rest)
	set AppleScript's text item delimiters to d
	return t
end changeName

This gets me what I want, but I cant figure out how to get rid of the extra space before the comma. Any ideas?

Hi. This should work for persons with First/Last or First/Middle/Last and the optional suffix. Test it against your names.

set theName to "Martin Luther King Jr."
set text item delimiters to space

tell theName's text items
	if item -1 ends with "." then
		if (count items) > 3 then
			item -2 & ", " & item 1 & space & middle item & space & item -1
		else
			item -2 & ", " & item 1 & space & item -1
		end if
	else
		if (count items) > 2 then
			item -1 & ", " & item 1 & space & middle item
		else
			item -1 & ", " & item 1
		end if
	end if
end tell

Thanks for your help. A friend helped me find a simple solution!

set firstLast to "First Last Jr."
changeName from firstLast
to changeName from t
	set d to AppleScript's text item delimiters
	set AppleScript's text item delimiters to " "
	set t to ((t's text items 2 thru -1) as text) & ", " & (t's text item 1)
	set AppleScript's text item delimiters to d
	return t
end changeName

Hi.

You’re getting the extra space because ‘rest’ in this case consists of the last three items of {“Last”, “Jr.”, “,”, “First”} ” that is {“Jr.”, “,”, “First”}. When you coerce this list to text (which happens because the list is being concatenated to the text “Last”), the space text item delimiter is inserted between each item.

However, the construction .

tell t to set t to beginning & ({""} & rest)

. is just a trick from the days when AppleScript used to have separate ‘string’ and ‘Unicode text’ classes. It was a way of coercing the whole of list ‘t’ to text while preserving the contents’ original class. Nowadays there is only the ‘text’ class and ‘string’ and ‘Unicode text’ are synonyms for it. So the line you’d use is:

set t to t as text

But this would still give you the extra space. It would be better in this case to get the text from the second word to the end as a single text object and concatenate ", " and the first word to it:

--set firstLast to the clipboard
set firstLast to "First Last Jr."
changeName from firstLast
to changeName from t
	set d to AppleScript's text item delimiters
	set AppleScript's text item delimiters to " "
	set t to (t's text from text item 2 to -1) & ", " & (t's text item 1)
	set AppleScript's text item delimiters to d
	return t
end changeName