script to alphabetize Name or Title

Looking for a script that will transform selected (highlighted) text to “alpha” format.

“Neil Armstrong” becomes “Armstrong, Neil”
and
“The Applescript Programmers Guide” becomes “Applescript Programmers Guide, The”

Logic:
On the selected text, capture first word, delete next space char, then append the word to the end of selection, preceded by a comma and space.

Thanks.

You may try to use :

tell application "System Events" -- get frontmost process and application
	set frontmostProcess to first process where it is frontmost -- this will be the script process
	if name of frontmostProcess is in {"Script Editor", "AppleScript Editor", "Time Machine"} then
		set visible of frontmostProcess to false -- hide the script process
		repeat while (frontmostProcess is frontmost) -- wait until the script is hiden
			delay 0.2
		end repeat
		set theProcess to name of first process where it is frontmost -- get name of frontmost process (ignoring the script process)
		set frontmost of frontmostProcess to true -- unhide the script process
	else
		set theProcess to name of frontmostProcess
	end if
	set fileApp to path of file of process theProcess
end tell # System Events

try
	tell application fileApp to activate
	tell application "System Events" to tell process theProcess
		set frontmost to true
		# Copy the selected string
		keystroke "c" using {command down}
		# Modify the string
		tell me
			set theString to the clipboard
			set beg to word 1 of theString
			set theString to (text (2 + (count beg)) thru -1 of theString) & ", " & beg
			set the clipboard to theString
		end tell
		# Paste it in the source
		keystroke "v" using {command down}
	end tell
end try

Yvan KOENIG running El Capitan 10.11.3 in French (VALLAURIS, France) samedi 13 février 2016 17:25:11

As I don’t know for sure which is the application owning the source, I’m not sure that my script fits your needs.

It apply to TextEdit but it fails if the selection is a filename selected in a Finder window.

Maybe this enhanced one would fit your needs.

tell application "System Events" -- get frontmost process and application
	set frontmostProcess to first process where it is frontmost -- this will be the script process
	if name of frontmostProcess is in {"Script Editor", "AppleScript Editor", "Time Machine"} then
		set visible of frontmostProcess to false -- hide the script process
		repeat while (frontmostProcess is frontmost) -- wait until the script is hiden
			delay 0.2
		end repeat
		set theProcess to name of first process where it is frontmost -- get name of frontmost process (ignoring the script process)
		set frontmost of frontmostProcess to true -- unhide the script process
	else
		set theProcess to name of frontmostProcess
	end if
	set fileApp to path of file of process theProcess
end tell # System Events
try
	if theProcess is "Finder" then
		tell application fileApp
			activate
			set maybe to selection as alias
			set oldName to name of maybe
			set theExt to name extension of maybe
			if theExt > "" then
				set shortName to text 1 thru -(2 + (count theExt)) of oldName
				set newShortName to my moveFirstword(shortName)
				set name of maybe to (newShortName & "." & theExt)
			else
				set newName to my moveFirstword(oldame)
				set name of maybe to newName
			end if
		end tell
	else
		tell application fileApp to activate
		tell application "System Events" to tell process theProcess
			set frontmost to true
			# Copy the selected string
			keystroke "c" using {command down}
			# Modify the string
			tell me
				delay 0.1
				set theString to the clipboard
				set beg to word 1 of theString
				set theString to (text (2 + (count beg)) thru -1 of theString) & ", " & beg
				set the clipboard to theString
			end tell
			# Paste it in the source
			keystroke "v" using {command down}
		end tell
	end if
end try

on moveFirstword(theString)
	set beg to word 1 of theString
	set theString to (text (2 + (count beg)) thru -1 of theString) & ", " & beg
	return theString
end moveFirstword

Yvan KOENIG running El Capitan 10.11.3 in French (VALLAURIS, France) samedi 13 février 2016 17:51:06

Wow! Excellent! Sorry about not giving my app. I’m in Pages 09 (4.3) and a few runs shows this works great in Pages. Could have easily taken my text list into TextEditor, BBedit, TextWrangler, etc.

Thank you so much. That will save me so much time.

As info… I’m using this for book Indexes (not TOC! Index) - I’m reseting some edited books for the printer. I take index terms from an existing Pages file where index was done by hand. Proper names are “Last, First” and I wont get a name hit when using PDFindexGenerator. So I manually flip the name, run Generator against the book to get my index info, then have to flip the name back to Alpha format. This is where the script comes in, the back end “flip.”

Now I need to see if I can turn the script into a workign front end script also (regular name to “Smith, John” format).

Just what I needed… Thanks, thanks, and thanks.