Save find/replace match to new file

39 views
Skip to first unread message

Matthew Montgomery

unread,
Aug 31, 2021, 3:55:26 PM8/31/21
to BBEdit Talk
Hi all,

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. I took a look at a text factory, specifically using replace all, but that didn’t seem to have the ability to do what I want with regard to new files.

I basically want to process the main file and save the result of each find/replace match in a new file.

I can think of a number of multi-step ways to do this, but I was hoping there was something I could do that would accomplish this in a single step. It’s very possible I am missing something obvious.

Thanks!

--
Matthew

Christopher Stone

unread,
Aug 31, 2021, 8:03:59 PM8/31/21
to BBEdit-Talk
On Aug 31, 2021, at 14:22, Matthew Montgomery <mat...@signed8bit.com> 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 – 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 <script...@thestoneforge.com>
# 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
--------------------------------------------------------

Matthew Montgomery

unread,
Sep 1, 2021, 10:31:47 AM9/1/21
to BBEdit Talk
Chris,

Thank you so much for this. Your trusty script worked perfectly and has for sure saved me a nice chunk of time. I really appreciate you sharing this.

Matthew

--
This is the BBEdit Talk public discussion group. If you have a feature request or need technical support, please email "sup...@barebones.com" 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 bbedit+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bbedit/78E1065B-58FD-4DAE-8C2E-B5CA7937AAB5%40gmail.com.

Christopher Stone

unread,
Sep 1, 2021, 6:35:29 PM9/1/21
to BBEdit-Talk
On Sep 01, 2021, at 09:07, Matthew Montgomery <mat...@signed8bit.com> wrote:

Your trusty script worked perfectly and has for sure saved me a nice chunk of time. I really appreciate you sharing this.


Hey Matthew,

Super!

And you're welcome.


--
Best Regards,
Chris

Reply all
Reply to author
Forward
0 new messages