On Aug 31, 2021, at 14:22, Matthew Montgomery <[email protected]> wrote: > I’ve got a couple hundred lines in a tab delimited file that I need to > transform into one new YAML file per line. I’ve got the search and > replacement patterns all sorted out (thanks pattern playground!) but am > getting stumped on what I need to do to process the main file.
Hey Matthew, While this job is not difficult, it's not simple either – particularly if you're not conversant with a scripting language. You need to adjust the following variables in the script to meet your needs: set regexFindPattern to "^(\\w.+)" set regexReplacePattern to "••••• \\1" When writing and testing AppleScript on macOS I advise folks to use Script Debugger <https://latenightsw.com/> – even if they don't write much AppleScript. I've used it for 26 years plus and cannot begin to express how much it changed the AppleScript experience for me. The commercial version is not inexpensive ($99.99), BUT it reverts to its FREE “Lite” version after a 30 day demo period – and the free version still beats the utter pants off of Apple's Script Editor.app. You can run the script directly from Script Debugger (or the insipid Script Editor.app), or you can save it as a compiled script and run it from BBEdit's own Script menu. ~/Library/Application Support/BBEdit/Scripts/ New YAML files will be written to: ~/Downloads/New_YAML_Files/ -- Best Regards, Chris -------------------------------------------------------- # Auth: Christopher Stone <[email protected]> # 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 -------------------------------------------------------- -- This is the BBEdit Talk public discussion group. If you have a feature request or need technical support, please email "[email protected]" rather than posting here. Follow @bbedit on Twitter: <https://twitter.com/bbedit> --- You received this message because you are subscribed to the Google Groups "BBEdit Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/bbedit/78E1065B-58FD-4DAE-8C2E-B5CA7937AAB5%40gmail.com.
