Hey Mike,
You can put the regex search code in your AppleScript and probably skip some steps...
This is AppleScriptObjC:
--------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2016/05/08 00:59
# dMod: 2021/08/27 19:51
# Appl: AppleScriptObjC
# Task: Find-Replace or Change-Text Handler
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @ASObjC, @Find, @Replace, @Text, @RegEx, @cngStr, @Change
--------------------------------------------------------
use AppleScript version "2.4"
use framework "Foundation"
use scripting additions
--------------------------------------------------------
set dataString to "
Line 01
Line 02
Your authorization code is: YEXCYC34
Line 04
Line 05
"
set authCode to its cngStr:"(?mis).*?Your authorization code is:\\h*(\\w+).*" intoString:"$1" inString:dataString
--------------------------------------------------------
--» HANDLERS
--------------------------------------------------------
on cngStr:findString intoString:replaceString inString:dataString
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:
--------------------------------------------------------
You can also do this using Keyboard Maestro instead of AppleScriptObjC:
--------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2022/12/07 21:30
# dMod: 2022/12/07 21:30
# Appl: Keyboard Maestro Engine
# Task: Find & Replace with RegEx in AppleScript with Keyboard Maestro.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Keyboard_Maestro_Engine, @RegEx
--------------------------------------------------------
set dataStr to "
Line 01
Line 02
Your authorization code is: YEXCYC34
Line 04
Line 05
"
# These handlers are the same in function but the calls are formatted differently.
# kmCng:regExPat inStr:dataStr withStr:replacePat usingRegEx:regExBool
# kmReplace(findPattern, replacePattern, dataStr, regExBool, caseBool, tokensBool)
set authCode1 to its kmCng:"(?mis).*Your authorization code is:\\h*(\\w+).+" inStr:dataStr withStr:"$1" usingRegEx:true usingCase:false usingTokens:false
set authCode2 to its kmReplace("(?mis).*Your authorization code is:\\h*(\\w+).+", "$1", dataStr, true, false, false) -- regEx, case, tokens
--------------------------------------------------------
--» kmCng()
--------------------------------------------------------
-- Task: Find and Replace with RegEx using Keyboard Maestro's AppleScript search command.
-- dMod: 2019/01/19 04:33
--------------------------------------------------------
on kmCng:regExPat inStr:dataStr withStr:replacePat usingRegEx:regExBool usingCase:caseBool usingTokens:tokensBool
tell application "Keyboard Maestro Engine"
set foundDataList to search dataStr for regExPat replace replacePat ¬
regex regExBool case sensitive caseBool process tokens tokensBool
end tell
end kmCng:inStr:withStr:usingRegEx:usingCase:usingTokens:
--------------------------------------------------------
--» kmReplace()
--------------------------------------------------------
-- Task: Find and Replace with RegEx using Keyboard Maestro's AppleScript search command.
-- dMod: 2018/04/06 04:55
--------------------------------------------------------
on kmReplace(findPattern, replacePattern, dataStr, regExBool, caseBool, tokensBool)
tell application "Keyboard Maestro Engine"
set foundDataList to search dataStr for findPattern replace replacePattern ¬
regex regExBool case sensitive caseBool process tokens tokensBool
end tell
end kmReplace
--------------------------------------------------------