select entire line(s) and extract them

970 views
Skip to first unread message

Gabriel

unread,
Dec 17, 2020, 9:08:27 AM12/17/20
to BBEdit Talk
Hi, 
I just discovered BBEdit.
I'm trying to do something about it:
From a text document that contains n lines, select the first whole line, copy it into a new document, then the next one, until the end of the document (if I have 50 lines, I would have 50 new text files). 
I started to explore the possibilities of "grep", with these commands: 
^.*
But I can't get the selection to stop at the end of the line, despite many tries.
(I'd like to point out that I don't know anything about these code systems!)
Ideally I'd like to be able to save these documents with the content of the line itself as filename, but let's say it's a second step (and I don't even know if it's possible). 
Thanks for the help!

Christian Boyce

unread,
Dec 17, 2020, 10:00:37 AM12/17/20
to bbe...@googlegroups.com
If I understand what you’re saying, you have a document with contents that looks like this:

Line 1
Line 2
Line 3
This is Line 4

Etc.

If that’s so, this AppleScript should do it:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

tell application "BBEdit"
set theDocument to document 1
set theContent to contents of theDocument
set theText to text of theContent
set theParagraphs to every paragraph of theText
repeat with aParagraph in theParagraphs
set myNewDocument to make new document
set contents of myNewDocument to text of aParagraph
end repeat
end tell


It’s possible to make this a shorter script but written this way it’s easier to understand.

c
--
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/77232728-9b03-48d1-9988-74fc67d37005n%40googlegroups.com.

Gabriel

unread,
Dec 19, 2020, 9:18:47 AM12/19/20
to BBEdit Talk
Hi, thanks a lot Christian
I'll try that, it seems great!
g.

Christopher Stone

unread,
Dec 19, 2020, 10:31:55 AM12/19/20
to BBEdit-Talk
On 12/17/2020, at 08:07, Gabriel <gabriel...@gmail.com> wrote:
I just discovered BBEdit.


Hey Gabriel,

Ah, so you've found some treasure – eh?  ;-)

From a text document that contains n lines, select the first whole line, copy it into a new document, then the next one, until the end of the document (if I have 50 lines, I would have 50 new text files).

Fifty, huh?  We'll get to this in a minute.

I started to explore the possibilities of "grep", with these commands: 
^.*
But I can't get the selection to stop at the end of the line, despite many tries.

Regular Expressions are tricky, although BBEdit supports PCRE – and that's pretty much the gold standard.

^.+$

^  == Beginning of line
.  == One character
+  == Or more
$  == End of line

Make sure you turn on the ‘Grep’ checkbox in the BBEdit Find Dialog.

But – this doesn't get us anywhere in the automation phase of things...

Ideally I'd like to be able to save these documents with the content of the line itself as filename, but let's say it's a second step (and I don't even know if it's possible). 

Okay, here's a completely BBEdit-Centric method using AppleScript:

--------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2020/12/19 07:34
# dMod: 2020/12/19 07:34 
# Appl: BBEdit
# Task: Create Files in the Finder from Lines in the Front Document.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @BBEdit, @Create, @Files, @Finder
--------------------------------------------------------

set savePath to path to downloads folder as text

tell application "BBEdit"

    

    tell front text window

        

        if number of characters ≠ 0 then

            

            set lineList to contents of its lines where contents is not ""

            

        else

            

            error "The front window doesn't contain any text!"

            

        end if
    end tell

    

    repeat with i in lineList

        

        # You can remove ‘bounds’ if you don't care about window position and size.
        make new document with properties {contents:i, bounds:{430, 45, 1390, 1196}}
        save front document to (savePath & i & ".txt")

        

        # Close the new document if desired.
        # close front document

        

    end repeat

    

end tell

--------------------------------------------------------

Unlike Christian's script I'm saving the files in the Finder as I make them.

The drawback of this method is that it's a bit slow, and the more files you create the slower it'll be.

So let's do something about that.

The following script is more intimidating looking than the first one, because it has a couple of complicated looking handlers.  One is for reporting errors, and the other is for writing the files in the Finder.

The main part of the script is all too simple.

Instead of having BBEdit open each new document, populate it, and write the file – I'm scraping the lines of the front document and then writing the files directly with AppleScript, and this is fast as lightning.

Fifty files get written in less than 1/2 a second even on my old Mid-2010 17" MacBook Pro.

And yes they use the contents of the line for the file name.

--------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2020/12/19 07:34
# dMod: 2020/12/19 08:58
# Appl: BBEdit
# Task: Create Files in the Finder from Lines in the Front Document.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @BBEdit, @Create, @Write, @Files, @Finder
--------------------------------------------------------

try

    

    set savePath to path to downloads folder as text

    

    tell application "BBEdit"
        tell front text window to set lineList to contents of its lines where contents is not ""
    end tell

    

    repeat with i in lineList
        writeUTF8(i, savePath & i & ".txt")
    end repeat

    

    set shCMD to "
        osascript -e 'display notification \"Finished!\" with title \"BBEdit\" subtitle \"Write Files Script\" sound name \"Tink\"'
    "
    do shell script shCMD

    

on error errMsg number errNum
    stdErr(errMsg, errNum, true, true) of me
end try

--------------------------------------------------------
--» HANDLERS
--------------------------------------------------------
on stdErr(errMsg, errNum, beepFlag, ddFlag)
    set errMsg to errMsg & return & return & "Num: " & errNum
    if beepFlag = true then
        beep
    end if
    if ddFlag = true then
        if errNum ≠ -128 then
            try
                tell application (path to frontmost application as text) to set ddButton to button returned of ¬
                    (display dialog errMsg with title "ERROR!" buttons {"Copy Error Message", "Cancel", "OK"} ¬
                        default button "OK" giving up after 30)
                if ddButton = "Copy Error Message" then set the clipboard to errMsg
            end try
        end if
    else
        return errMsg
    end if
end stdErr
--------------------------------------------------------
on writeUTF8(dataStr, 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 dataStr 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 errMsg number errNum
        try
            close access fRef
        on error errMsg number errNum
            error "Error in writeUTF8() handler of library: gen.lib" & return & return & errMsg
        end try
    end try
end writeUTF8
--------------------------------------------------------

Let me know if you have any problems.

--
Best Regards,
Chris

Gabriel

unread,
Dec 27, 2020, 9:28:53 AM12/27/20
to BBEdit Talk
Hi Christopher
first, sorry for the delay.
This is AmAzIng ! I'll use it. I'm impressed.
Thank you very much. If trouble i'll ask for sure ! ;) 
gabriel 

Gabriel

unread,
Dec 27, 2020, 9:28:57 AM12/27/20
to BBEdit Talk
... but for some reason, the expression ^.+$ doesn't work:
it selects the whole text, not line by line, as I tried from the beginning.

Le samedi 19 décembre 2020 à 16:31:55 UTC+1, listmei...@gmail.com a écrit :

Christopher Stone

unread,
Dec 30, 2020, 1:15:46 AM12/30/20
to BBEdit-Talk
On 12/27/2020, at 08:21, Gabriel <gabriel...@gmail.com> wrote:
... but for some reason, the expression ^.+$ doesn't work:
it selects the whole text, not line by line, as I tried from the beginning.


Hey Gabriel,

I think you probably have the 'Show matches' checkbox in the Find dialog turned on.

If I'm correct then you're seeing every possible match, rather than the entire match.

Type Cmd-G a few times to find-next.

And/or add a blank line or two to the middle of your document – then open the Find dialog again.

Let me know if I've guessed right or not.

--
Best Regards,
Chris

Gabriel

unread,
Dec 31, 2020, 4:45:33 PM12/31/20
to BBEdit Talk
Hi Chris
thank you for your answer! 
Indeed, very well guessed. 
It was very misleading! 
Thanks again, BBEdit is impressive and i need to get used to it!
Best Regards
Reply all
Reply to author
Forward
0 new messages