Afternoon all; novice Applescripter/Automator user here, making my first steps. I’m looking to build an Automator/Applescript action that can do the following:
Select all of the files in a folder that have a specific word in the filename (such as “summary”)
Combine all of these files into a single file.
If it makes any difference, the files in question are .csv files, and the end product will be another CSV file for importing into Excel.
I’m sure that is a trivial thing to do, but I can’t find a good, simple example. I’ve looked through the script archives, but can’t find anything along these lines to work with. Anybody got one they would care to share, or a pointer to one?
quick & dirty as AppleScript, the concatenated file is named completeSummary.csv on desktop
The script assumes, that all .csv files are of the same kind, (7- or 8-bit ASCII)
set sourcefolder to choose folder
tell application "Finder" to set csvFiles to files of sourcefolder whose name contains "summary"
set summaryFile to ((path to desktop as Unicode text) & "completeSummary.csv")
try
set ff to open for access file summaryFile with write permission
repeat with oneFile in csvFiles
set theText to read (oneFile as alias)
write theText to ff starting at eof
end repeat
close access ff
on error
try
close access file summaryFile
end try
end try
So if I wanted to refine this, and make the script only copy specific lines of the files into the summary, how would I approach that? Say, I wanted to only copy lines that started with “Test1”?
set sourcefolder to choose folder
tell application "Finder" to set csvFiles to files of sourcefolder whose name contains "summary"
set summaryFile to ((path to desktop as Unicode text) & "completeSummary.csv")
try
set ff to open for access file summaryFile with write permission
repeat with oneFile in csvFiles
set theText to paragraphs of (read (oneFile as alias))
repeat with oneLine in theText
if contents of oneLine starts with "Test1" then
write (contents of oneLine & return) to ff starting at eof
end if
end repeat
end repeat
close access ff
on error
try
close access file summaryFile
end try
end try
Last question (honest ;-); how would I adapt this to grab the next, say, 3 lines from the file after the occurance of the line with the searched text in it. Say I am searching for the text “bob”, and want to save that one and the 3 lines below it to the summary file.
property searchString : "bob"
set sourcefolder to choose folder
tell application "Finder" to set csvFiles to files of sourcefolder whose name contains "summary"
set summaryFile to ((path to desktop as Unicode text) & "completeSummary.csv")
try
set ff to open for access file summaryFile with write permission
repeat with oneFile in csvFiles
set theText to paragraphs of (read (oneFile as alias))
repeat with oneLine from 1 to (count theText)
if item oneLine of theText starts with searchString then
set text item delimiters to return
set textToWrite to (items oneLine thru (oneLine + 3) of theText) as text
set text item delimiters to {""}
write (textToWrite & return) to ff starting at eof
set oneLine to oneLine + 4
if oneLine ≥ (count theText) then exit repeat
end if
end repeat
end repeat
close access ff
on error
try
close access file summaryFile
end try
end try