While this job is not difficult, it's not simple either – particularly if you're not conversant with a scripting language.
--------------------------------------------------------
# dCre: 2021/08/31 18:49
# dMod: 2021/08/31 18:49
# Appl: BBEdit
# Task: Explode TSV Document into One YAML File Per Line.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @ASObjC, @BBEdit, @Explode, @TSV, @Document, @YAML
--------------------------------------------------------
use AppleScript version "2.4" --» Yosemite or later
use framework "Foundation"
use scripting additions
--------------------------------------------------------
--» USER SETTINGS
--------------------------------------------------------
set regexFindPattern to "^(\\w.+)"
set regexReplacePattern to "••••• \\1"
set new_YAML_Files_Destination to "~/Downloads/New_YAML_Files"
--------------------------------------------------------
set fileNameCounter to 0
set new_YAML_Files_Destination to ((current application's NSString's stringWithString:new_YAML_Files_Destination)'s stringByExpandingTildeInPath) as text
its createDirectoryAtPathWithIntermediates:new_YAML_Files_Destination
set itemFound to true
tell application "BBEdit"
set frontDoc to a reference to the front text document
select insertion point before character 1 of frontDoc
repeat while itemFound
set findRecord to find regexFindPattern searching in frontDoc options {search mode:grep, case sensitive:false, wrap around:false}
if found of findRecord ≠ true then
return
else
select found object of findRecord
set newText to replace regexFindPattern using regexReplacePattern searchingString (get found text of findRecord) options {search mode:grep, case sensitive:false, starting at top:true}
set fileNameCounter to fileNameCounter + 1
set newFileName to "File_Name " & fileNameCounter & ".yaml"
set targetFilePath to new_YAML_Files_Destination & "/" & newFileName
write_UTF8(newText, targetFilePath) of me
end if
end repeat
end tell
--------------------------------------------------------
--» HANDLERS
--------------------------------------------------------
on createDirectoryAtPathWithIntermediates:thePath
set {theResult, theError} to current application's NSFileManager's defaultManager()'s createDirectoryAtPath:thePath ¬
withIntermediateDirectories:true attributes:(missing value) |error|:(reference)
if not (theResult as boolean) then
set errorMsg to theError's localizedDescription() as text
error errorMsg
end if
end createDirectoryAtPathWithIntermediates:
--------------------------------------------------------
on write_UTF8(_text, targetFilePath)
try
if targetFilePath starts with "~/" then
set targetFilePath to POSIX path of (path to home folder as text) & text 3 thru -1 of targetFilePath
end if
set fRef to open for access targetFilePath with write permission
set eof of fRef to 0
write _text to fRef as «class utf8»
close access fRef
if targetFilePath contains ":" then
return alias targetFilePath
else if targetFilePath starts with "/" then
return alias POSIX file targetFilePath
end if
on error e number n
try
close access fRef
on error e number n
error "Error in write_UTF8() handler" & linefeed & linefeed & e
end try
end try
end write_UTF8
--------------------------------------------------------