Create Acronym from the Text - 2 effective methods

Plain AppleScript method using JXA functions:


on makeAcronym(s)
	run script quoted form of s & ".match(/\\b(\\w)/g).join(\"\").toUpperCase();" in "JavaScript"
end makeAcronym

my makeAcronym("high ending little little omikron")

.
ASObjC method (maybe there is a better implementation, unknown to me):


use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions

set sourceString to current application's NSString's stringWithString:"high ending little little omikron"
set {ATID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, space}
set theItems to (sourceString's capitalizedString() as text)'s text items
set AppleScript's text item delimiters to ATID

set acronim to {}
repeat with theItem in theItems
	set acronim's end to theItem's character 1
end repeat
set acronim to acronim as text

I’ve included an ASObjC suggestion below. I couldn’t find documentation for firstCharacter, so my suggestion may not be reliable.

-- revised to include hyphens and filter empty strings

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions

set theString to "self-contained underwater breathing apparatus"
set sourceString to current application's NSString's stringWithString:theString
set theDelimiters to (current application's NSCharacterSet's characterSetWithCharactersInString:(" " & "-"))
set sourceArray to (sourceString's componentsSeparatedByCharactersInSet:theDelimiters)
set thePredicate to current application's NSPredicate's predicateWithFormat:"self != ''"
set sourceArray to sourceArray's filteredArrayUsingPredicate:thePredicate
set sourceArray to sourceArray's valueForKeyPath:"uppercaseString.firstCharacter"
set theAcronym to (sourceArray's componentsJoinedByString:"") as text --> "SCUBA"

This may need a more sophisticated regex pattern:

use AppleScript version "2.4" -- OS X 10.10 (Yosemite) or later
use framework "Foundation"
use scripting additions

set sourceString to current application's NSString's stringWithString:"high ending little little omikron"
set theAcronym to sourceString's capitalizedString()'s ¬
	stringByReplacingOccurrencesOfString:("[^A-Z]++") withString:("") ¬
		options:(current application's NSRegularExpressionSearch) range:({0, sourceString's |length|()})

An interesting question is how to deal with hyphenated words. Two of the suggestions in this thread include the word after the hyphen and two don’t. Perhaps the script should check for hyphens and notify the user. An example where the word after the hyphen should be included is:

Hi peavine.

  1. Your valueForKeyPath:“uppercaseString.firstCharacter” works on my Mojave machine, but I can’t find the firstCharacter property in the Xcode 11.3.1 documentation. :confused:

  2. To solve the hyphen problem in your script, you could replace this …

set sourceArray to (sourceString's componentsSeparatedByString:space)

… with this:

set noLetterSet to current application's NSCharacterSet's letterCharacterSet()'s invertedSet()
set sourceArray to (sourceString's componentsSeparatedByCharactersInSet:noLetterSet)

It’s probably a good idea to filter the array for possible empty strings between this and the valueForKeyPath line.

Edit: Parentheses after ‘letterCharacterSet’! :rolleyes:

Thanks Nigel. I tested your suggestion to use letterCharacterSet and that works great.

Nice. :slight_smile:
I liked both AsObjC methods suggested here by users @Nigel Garvey and @peavine.

I still prefer @Nigel Garvey’s elegant solution. With one correction: based on the documentation, stringByReplacingOccurrencesOfString: and withString: should be provided by NSString values instead of String values.

[format]Declaration

  • (NSString *)stringByReplacingOccurrencesOfString:(NSString *)target
    withString:(NSString *)replacement;[/format]

Though I’m not sure if it could be important because both suggested solutions gave correct results in my tests.


use AppleScript version "2.4" -- OS X 10.10 (Yosemite) or later
use framework "Foundation"
use scripting additions

on makeAcronym(theString as text)
	-- convert String values to NSString equivalents
	set sourceString to current application's NSString's stringWithString:theString
	set NSRegexString to current application's NSString's stringWithString:"[^A-Z]++"
	set NSSpaceString to current application's NSString's stringWithString:""
	-- make acronym
	set theAcronym to sourceString's capitalizedString()'s ¬
		stringByReplacingOccurrencesOfString:NSRegexString withString:NSSpaceString ¬
			options:(current application's NSRegularExpressionSearch) range:({0, sourceString's |length|()})
	return theAcronym as text
end makeAcronym

my makeAcronym("high ending little little omikron")

The scripting bridge does the conversion for you – doing it yourself is generally just wasting time and effort.

So, I strained in vain :frowning: , and the handler in the final form will be short, as @Nigel Garvey suggested. Thank you all for your participation.


use AppleScript version "2.4" -- OS X 10.10 (Yosemite) or later
use framework "Foundation"
use scripting additions

on makeAcronym(theString as text)
	set sourceString to current application's NSString's stringWithString:theString
	set theAcronym to sourceString's capitalizedString()'s ¬
		stringByReplacingOccurrencesOfString:("[^A-Z]++") withString:("") ¬
			options:(current application's NSRegularExpressionSearch) range:({0, sourceString's |length|()})
	return theAcronym as text
end makeAcronym

my makeAcronym("high ending little little omikron")