Applescript: Automatice Excel para convertir .xls a .csv

Estoy tratando de procesar por lotes la conversión de una carpeta de archivos .xls a .csv usando Applescript para MS Excel V15.15. Estoy usando una muestra que encontré en línea para modelarlo:

set theOutputPath to (path to desktop folder as string) & "My Saved Workbook.csv"
  tell application "Microsoft Excel"
    tell active workbook
      save workbook as filename theOutputPath file format CSV file format
    end tell
  end tell

Este es el script que falla constantemente, aunque parece que se parece más al modelo:

set csv_folder to "Macintosh HD:Users:Me:CSV:" & file_name as string
    tell application "Microsoft Excel"
        open Source_file
        tell active workbook
            save workbook as filename csv_folder file format CSV Mac file format-->
           (*This generates error "Microsoft Excel got an error: Parameter error." number -50 *)
        end tell
    end tell

También he probado:

set csv_folder to "Macintosh HD:Users:Me:CSV:" & file_name & ".csv" as string

tell application "Microsoft Excel"
    open Source_file
        tell active workbook
            save workbook as filename csv_folder -->
           (*This usually generates error "Microsoft Excel got an error:
            Parameter error." number -50 the first time it is run, 
              then works the 2nd time *)
        end tell
end tell

EDITAR: este último script, aunque se completa, no da como resultado un verdadero archivo csv, ya que cuando lo abro con BBEdit muestra el código, no el contenido del archivo.

También intenté configurar cv_folder sin usar "as String". ¿Alguna idea de por qué esto falla? No parece gustarle la sintaxis "guardar libro de trabajo como nombre de archivo formato de archivo formato de archivo CSV theOutputPath".

Respuestas (2)

Para Excel V15 , use una ruta posix, como esta:

set theOutputPath to POSIX path of ((path to desktop folder as string) & "My Saved Workbook.csv")
tell application "Microsoft Excel"
    tell active workbook
        save workbook as filename theOutputPath file format CSV Mac file format with overwrite
    end tell
end tell

Editar 1 -

Lo pruebo en V15.15 y V15.16 , es un error cuando la carpeta de destino no contiene un archivo de Excel abierto recientemente. Así que úsalo

set theOutputPath to POSIX path of ((path to desktop folder as string) & "My Saved Workbook.csv")
set parentFolder to (do shell script "dirname " & quoted form of theOutputPath) as POSIX file -- get the parent folder 

tell application "Microsoft Excel"
    alias parentFolder -- a folder where to save a new file, workaround to a bug when the destination folder doesn't contains a recently opened Excel file
    save workbook as active workbook filename (theOutputPath) file format CSV Mac file format with overwrite
end tell

Editar 2 -

O crea un archivo vacío como este

set theOutputPath to POSIX path of (path to desktop folder as string) & "My Saved Workbook.csv"
do shell script "touch " & quoted form of theOutputPath -- create an empty file
set theOutputPath to theOutputPath as POSIX file
tell application "Microsoft Excel"
    save workbook as active workbook filename (theOutputPath) file format CSV Mac file format with overwrite
end tell

Edición 3 : tuve otra idea, sería el script más simple.

set theOutputPath to (path to desktop folder as string) & "My Saved Workbook.csv"
tell application "Microsoft Excel"
    alias theOutputPath --  workaround to a bug when the destination folder doesn't contains a recently opened Excel file
    save workbook as active workbook filename theOutputPath file format CSV Mac file format with overwrite
end tell
Gracias por la ayuda. La secuencia de comandos que proporcionó aún devuelve el mismo error de parámetro para la línea "guardar libro de trabajo como nombre de archivo theOutputPath formato de archivo CSV Mac formato de archivo con sobrescritura". Estoy empezando a creer que esto es un cambio en Excel para Mac v15 ya que ninguna de las opciones de sintaxis para Guardar como [Tipo de archivo] que he encontrado funciona en Excel 15.
Actualicé mi respuesta.

Parece que el error de parámetro se debe a cambios con Office 2016 para Mac.

http://preview.alturl.com/f8srb

Por lo que aprendí de ese hilo, Excel V15 solo puede escribir automáticamente (activado por AppleScript) dentro de la ruta ~/Library/Containers/com.microsoft.excel/. Parece que tendré que volver a la versión anterior de Excel, o tal vez escribir los archivos en esa carpeta y luego copiarlos en la ubicación deseada. Que PITA.