Saturday, December 16, 2017

#1 2017-04-19 03:00:53 pm

TheREDNAVE
Member
Registered: 2015-11-21
Posts: 38

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

Applescript:

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.

Offline

 

#2 2017-04-19 03:52:30 pm

TheREDNAVE
Member
Registered: 2015-11-21
Posts: 38

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

after some tweaking I've now got

Applescript:

--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?

Last edited by TheREDNAVE (2017-04-19 03:59:55 pm)

Offline

 

#3 2017-04-19 05:04:54 pm

Marc Anthony
Member
From:: Dallas, TX
Registered: 2006-04-27
Posts: 772

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

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


Applescript:

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

Offline

 

#4 2017-04-19 06:39:22 pm

TheREDNAVE
Member
Registered: 2015-11-21
Posts: 38

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

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

Applescript:

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

Offline

 

#5 2017-04-19 06:55:39 pm

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 4456

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

TheREDNAVE wrote:

Applescript:

--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.

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 …

Applescript:

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:

Applescript:

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:

Applescript:

--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

Last edited by Nigel Garvey (2017-04-19 06:57:45 pm)


NG

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)