Swap Name (Doe, John → John Doe)

This extension idea I received by email is to swap around a person’s name, e.g. if it currently says “John Smith” it would flip it to “Smith, John” or vice versa.

How it might work for more complex names

  • John Herbert Walker Smith <=> Smith, John Herbert Walker
  • John H. W. Smith, <=> Smith, John H. W.
  • J. Herbert Smith <=> Smith, J. Herbert

Basically it should assume the surname is one word an everything else is the forenames.

A quick snippet for this, it goes from Doe, Jon to John Doe but not the other way round.

#popclip
name: Swap Name
icon: symbol:arrow.triangle.swap
after: paste-result
regex: '\w+, .+'
javascript: |
  const match = popclip.input.text.match(/([\w\-]+), (.+)/)
  if (match) return match[2] + ' ' + match[1]; else return null

(The above block is an extension snippet - select it to install the extension with PopClip)

Test inputs:
Bush, George
Bush, George Herbert Walker
Bush, George H. W.
Smith-Smythe-Smith, Tarquin

Thanks for this! I would more often need the reverse of this (John Do to Doe, John), but will keep this one in mind. Is there a simple “transpose 2 words” (or 2 characters) operation?

That would go partway but would also be useful to place punctuation inside double or single quotation marks for example when they appear on the outside.

And what about all the French, German and Dutch names with the prefixes: de, von, von der, van, van de, vanden…?

transpose 2 words:

// #popclip
// name: Transpose words
// icon: iconify:ri:swap-box-line
// language: javascript
// regex: ^\w+\s\w+$
// after: paste-result
const words = popclip.input.text.split(" ");
return words[1] + " " + words[0];

transpose 2 characters:

// #popclip
// name: Transpose chars
// icon: iconify:ri:swap-box-fill
// language: javascript
// regex: ^\w\w$
// after: paste-result
return popclip.input.text[1] + popclip.input.text[0];

(The above block is an extension snippet — select it then click “Install Extension” in PopClip.)

The extension was installed and is active (i.e. is checked) but does not appear in the application bar (popup).

These only appear when the selected text is in the required format.

For example, transpose chars only appears if you select exactly two characters.