No, there will not be any single character words. Though I’m not sure it should matter since the regex will in theory find a capital, search until it finds another capital etc.
Thanks for this! Works great. I didn’t even think about doing it this way - I was using the “Search Variable” action and the RegEx was tripping me up there.
@JMichaelTX provides an interesting way to do it, and has the advantage of working with any number of words. Given your constraint for 2-6 words, you can use this regex:
------------------------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2017/03/31 01:02
# dMod: 2017/03/31 01:02
# Appl: AppleScriptObjC & Keyboard Maestro Engine
# Task: Split a String into Words with a RegEx and Enter them into KM Variables.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @ASObjC, @Keyboard_Maestro_Engine, @Split, @String, @RegEx, @Enter, @Words, @Into, @Variables
------------------------------------------------------------------------------
use AppleScript version "2.4"
use framework "Foundation"
use scripting additions
------------------------------------------------------------------------------
# Set Keyboard Maestro Variable Example
------------------------------------------------------------------------------
tell application "Keyboard Maestro Engine"
setvariable "testStr" to "NoCoffeeTodayThanks"
end tell
------------------------------------------------------------------------------
# Get Keyboard Maestro Variable Example
------------------------------------------------------------------------------
tell application "Keyboard Maestro Engine"
set myAppleScriptVar to getvariable "testStr"
end tell
------------------------------------------------------------------------------
# Break string into paragraphs at lower-case/upper-case letter boundary:
set wordList to its cngStr:"(?-i)([a-z])([A-Z])" intoString:("$1" & linefeed & "$2") inString:myAppleScriptVar
# Ensure there is no vertical whitespace at top or bottom of string:
set wordList to its cngStr:"\\A\\s+|\\s+\\Z" intoString:"" inString:wordList
# Split the paragraphs into an AppleScript list-object:
set wordList to its splitstring:wordList withString:linefeed
------------------------------------------------------------------------------
# Create the New Variables in Keyboard Maestro
------------------------------------------------------------------------------
set baseKmVarName to "kmVar"
tell application "Keyboard Maestro Engine"
repeat with wordNum from 1 to length of wordList
set varName to baseKmVarName & wordNum
setvariable varName to (item wordNum of wordList)
end repeat
end tell
------------------------------------------------------------------------------
--» HANDLERS
------------------------------------------------------------------------------
on cngStr:findString intoString:replaceString inString:dataString -- courtesy of Shane Stanley
set anNSString to current application's NSString's stringWithString:dataString
set dataString to (anNSString's ¬
stringByReplacingOccurrencesOfString:findString withString:replaceString ¬
options:(current application's NSRegularExpressionSearch) range:{0, length of dataString}) as text
end cngStr:intoString:inString:
------------------------------------------------------------------------------
on splitstring:someText withString:mySeparator -- courtesy of Shane Stanley
set theString to current application's NSString's stringWithString:someText
set theList to theString's componentsSeparatedByString:mySeparator
return theList as list
end splitstring:withString:
------------------------------------------------------------------------------
------------------------------------------------------------------------------
# REQUIRES the Satimage.osax ⇢ http://tinyurl.com/satimage-osaxen
------------------------------------------------------------------------------
set myString to "NoCoffeeTodayThanks"
set wordList to splittext myString using "(?<=[a-z])(?=[A-Z])" with regexp
--> {"No", "Coffee", "Today", "Thanks"}
------------------------------------------------------------------------------
One line using positive-lookbehind and positive-lookahead assertions.
Poof!
Of course in this case I'm not allowing for the possibility of vertical whitespace, but that is also easily done using the Satimage.osax.
Also, you can select the Regular Expression Unicode Properties item in Keyboard Maestro’s Help menu to take you to this ICU Unicode Properties for Regular Expressions page that I created that will show you what characters are included in each character class (it’s not live updated, so it is always possible some have changed, but it was right at the time I made it).