-------------------------------------------------------------------------------------------
# dCre: 2014/12/18 00:05
# dMod: 2014/12/18 00:28
# Appl: TextWrangler
# Task: Parse a single file collection of .csv data into separate files.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @TextWrangler, @csv, @parse
-------------------------------------------------------------------------------------------
set csvFileContent to {}
tell application "TextWrangler"
tell front text window
set _file to its file as alias
select insertion point before its text
repeat
set foundReco to find "(?ms)^START_.+?^END_.+?$" selecting match true ¬
options {case sensitive:false, extend selection:false, returning results:true, search mode:grep, showing results:true}
if found of foundReco = true then
set end of csvFileContent to found text of foundReco
else
exit repeat
end if
end repeat
end tell
end tell
tell application "Finder" to set _parent to parent of _file as text
repeat with i in csvFileContent
set _start to (paragraph 1 of i)
set _end to (paragraph -1 of i)
if _start starts with "START_" and _end starts with "END_" then
set AppleScript's text item delimiters to "_"
set filePath to _parent & ((text items 2 thru -1 of _start) as text) & ".csv"
if exTant(filePath) then error "File exists!"
set AppleScript's text item delimiters to linefeed
set _content to ((paragraphs 2 thru -2 of (contents of i)) as text) & linefeed
writeUTF8(_content, filePath)
else
error "Something is wrong with the START_ or END_ delimiters of the data-set."
end if
end repeat
-------------------------------------------------------------------------------------------
--» HANDLERS
-------------------------------------------------------------------------------------------
on exTant(_path) # Takes an HFS, Posix, or ~/Posix path as input.
local _path
try
if _path starts with "~/" then
set _path to (POSIX path of (path to home folder as text)) & text 3 thru -1 of _path
end if
if _path starts with "/" then
alias POSIX file _path
else if _path contains ":" then
alias _path
end if
return true
on error
return false
end try
end exTant
-------------------------------------------------------------------------------------------
on writeUTF8(_text, _file)
try
if _file starts with "~/" then
set _file to POSIX path of (path to home folder as text) & text 3 thru -1 of _file
end if
set fRef to open for access _file with write permission
set eof of fRef to 0
write _text to fRef as «class utf8»
close access fRef
on error e number n
try
close access fRef
on error e number n
error "Error in writeUTF8() handler!" & return & return & e
end try
end try
end writeUTF8
-------------------------------------------------------------------------------------------
I don't know what your definition of very large file is, but if it wasn't monstrous I'd much rather do the job with AppleScript + the Satimage.osax and exclude TextWrangler from the equation. Processing will be quite a lot faster.
This script operates on the selected file in the Finder.
-------------------------------------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2014/12/18 12:55
# dMod: 2014/12/18 01:04
# Appl: TextWrangler
# Task: Parse a single file collection of .csv data into separate files.
# Libs: None
# Tags: @Applescript, @Script, @TextWrangler, @csv, @parse
-------------------------------------------------------------------------------------------
tell application "Finder" to set _sel to selection as alias list
if length of _sel = 1 then set _file to item 1 of _sel
set AppleScript's text item delimiters to ":"
set _parent to ((text items 1 thru -2 of (_file as text)) as text) & ":"
set foundList to find text "(?m)^START_.+?$[\\r\\n](.+?)[\\r\\n]^END_.+?$" in _file using ¬
"\\1" with regexp
, all occurrences
and string result
if foundList ≠ {} then
set AppleScript's text item delimiters to ","
repeat with i in foundList
set _name to text item 1 of i
set newFilePath to _parent & _name & ".csv"
writetext i to file newFilePath
end repeat
end if
-------------------------------------------------------------------------------------------
Neither one of these scripts has very much in the way of error-checking, and I would certain add more for a production script.
Both scripts put the new .csv files in the same folder as the original file.
--
Best Regards,
Chris