Hey @skillet,
Here’s another way of doing the job with AppleScript and the Keyboard Maestro Engine’s search/replace command.
----------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2018/04/21 16:00
# dMod: 2018/04/21 16:07
# Appl: Keyboard Maestro Engine
# Task: Extract item 2 of pipe-delimited text.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Keyboard_Maestro_Engine, @Extract, @Pipe-delimited, @Text
----------------------------------------------------------------
set the clipboard to "
3| 2| 413
" -- simulate data already being on the clipboard - add some junky whitespace for testing.
set dataStr to the clipboard
# Split text on the pipe character and any surrounding horizontal whitespace.
set foundText to kmReplace("\\h*\\|\\h*", "\\n", dataStr, true, true, true)
# Remove any other existing horizontal whitespace for good measure.
set foundText to kmReplace("\\h+", "", foundText, true, true, true)
# Remove any leading or trailing vertical whitespace for good measure.
set foundText to kmReplace("\\A\\s+|\\s+\\Z", "", foundText, true, true, true)
set foundText to paragraph 2 of foundText
----------------------------------------------------------------
--» HANDLERS
----------------------------------------------------------------
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
----------------------------------------------------------------
Note that I’m teaching techniques here and not just specific targeted regex.
If I was doing this job for myself I’d use the Satimage AppleScript Extension and make the job really simple:
----------------------------------------------------------------
# The Satimage.osax MUST be installed for this to work!
----------------------------------------------------------------
set the clipboard to "
3| 2| 413
" -- simulate data already being on the clipboard - add some junky whitespace for testing.
set foundText to item 2 of (find text "\\d+" in (get the clipboard) with regexp, all occurrences and string result)
This is really simple in that I’m finding all contiguous digits, and the result is an AppleScript list.
So all I have to do then is pick the second item in the list.
-Chris