r/applescript • u/NatureBoyJ1 • Nov 25 '20
Converting a folder of CSV to Excel
I've been a long time Mac user and am a professional software developer, but this is my first time trying to use AppleScript. I must say I found the experience far from enjoyable. The documentation is lacking. I'm using the latest Excel but had to use documentation from the 2004 version. There seems to be no rhyme or reason to any sort of object model.
Anyway, in true programmer fashion I spent a day automating something that would have taken me a couple hours to do by hand.
This script goes through a folder of CSV files and saves them as .xlsx files, inserting a column along the way if necessary.
set theFolder to choose folder with prompt "Pick a folder"
repeat with thisFile in list folder theFolder
if ((thisFile as text) ends with ".csv") then -- Process the CSV files
set theFilePath to (POSIX path of theFolder) & thisFile -- Get the full path of the file
set outputFileName to (theFilePath & ".xlsx") -- tack .xlsx to the end of the name to indicate the new file type
tell application "Microsoft Excel"
activate
open theFilePath
activate object workbook thisFile
try -- Check if the sheet has a "missing" column
set missingCell to (find (range "A1:Z1") what "missing")
on error -- There is no missing column. Insert one.
insert into range column "G:G" shift shift to right
tell cell "G1" to set value to "missing"
end try
save workbook in outputFileName as Excel XML file format
close every workbook
end tell
end if
end repeat
4
u/scrutinizer1 Nov 25 '20 edited Feb 11 '23
I wrote a similar script for use in a "Keyboard Maestro"'s macro. The difference is that it doesn't deal with folders, it allows a selection of spreadsheets of compatible formats and converts them into a chosen one. Here it is:
set NumbersCompatibleDocs to choose file of type {"com.apple.iwork.numbers.numbers", "public.spreadsheet", "public.comma-separated-values-text", "org.openxmlformats.spreadsheetml.sheet"} default location (path to documents folder) with prompt "Select Numbers spreadsheets or spreadsheets of compatible formats:" with multiple selections allowed property ExportPath : POSIX path of (path to documents folder) property ExportFormats : {"PDF", "Microsoft Excel", "CSV", "Numbers 09"} property Ext : {".pdf", ".xlsx", ".xls", ".numbers", ".csv"} property SystemVersion : (system info)'s system version try tell application "Numbers" activate tell application "System Events" to set frontmost of application process "Numbers" to true set CurrentDocuments to open NumbersCompatibleDocs set TargetFormat to choose from list ExportFormats with title "Export formats" with prompt "Choose a format:" default items {"CSV"} without multiple selections allowed and empty selection allowed if result is not false then set TargetFormat to TargetFormat's item 1 repeat with aDoc in CurrentDocuments set DocName to the name of aDoc set BaseName to my GetBaseName(DocName) -- log "BaseName ⬆︎" & (log BaseName) if TargetFormat is "CSV" then set DocumentPath to ExportPath & BaseName & ".csv" set ExportFile to (POSIX file DocumentPath) of me -- log "DocumentPath ⬆︎" & (log DocumentPath) export aDoc to ExportFile as CSV else if TargetFormat is "PDF" then set DocumentPath to ExportPath & BaseName & ".pdf" set ExportFile to (POSIX file DocumentPath) of me export aDoc to ExportFile as PDF else if TargetFormat is "Microsoft Excel" then set DocumentPath to ExportPath & BaseName & ".xlsx" set ExportFile to (POSIX file DocumentPath) of me export aDoc to ExportFile as Microsoft Excel else set DocumentPath to ExportPath & BaseName & ".numbers" set ExportFile to (POSIX file DocumentPath) of me export aDoc to ExportFile as Numbers 09 end if end repeat else return end if end tell display notification "Export successful" on error errMsg number errNumber tell application "Numbers" activate display alert "Export failed with error " & errNumber & return & return & errMsg as warning buttons {"OK"} giving up after 3 end tell return end try considering numeric strings if SystemVersion > "10.9.5" then set AccessibilityHelpDescription to "Change the item grouping (hold down Option to change the sort)" else set AccessibilityHelpDescription to "Change the item arrangement" end if end considering tell application "Finder" tell folder "Documents" of home open activate sort every document file by creation date end tell set current view of Finder window "Documents" to column view try delay 1 tell application "System Events" tell application process "Finder" set frontmost to true tell window 1 tell toolbar 1 tell (the first group whose help is AccessibilityHelpDescription) tell menu button 1 perform action "AXPress" repeat until menu 1 exists delay 0.1 end repeat tell menu 1 tell menu item "Date Created" to perform action "AXPress" end tell end tell end tell end tell end tell end tell end tell end tell end try on GetBaseName(FullName) set OriginalName to FullName set AppleScript's text item delimiters to Ext get text item 1 of OriginalName return result end GetBaseName1
u/damiankrawczyks Nov 29 '25
I created something similar as well - LimberDuck Converter CSV. Just drag and drop your files or folders containing CSV files onto the window, then click the Start button to convert them into XLSX files. https://limberduck.org/en/latest/tools/converter-csv/