On 12/17/2020, at 08:07, Gabriel <[email protected]
<mailto:[email protected]>> 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
--
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/2117D832-1AEE-42DE-A4BB-5A43A782118F%40gmail.com.