Automator: xlsx zu pdf

rocknroller

Registriert
Thread Starter
Dabei seit
19.09.2016
Beiträge
1
Reaktionspunkte
0
Hallo zusammen

Ich bin ein blutiger Automator/Skript-Anfänger und zudem auch noch neu hier - daher bitte Nachsicht, sollte dieses Thema bereits irgendwo behandelt worden sein (die Suchfunktion lieferte mir keine Ergebnisse) oder sollte ich mich unverständlich oder gar falsch ausdrücken.

Problemstellung
Ich habe einen Ordner, in welchen ich Rechnungen als Excel-Dateien im Format *.xlsx speichere. Diese muss ich jeweils zweimal physisch ausdrucken und einmal in einen Unterordner als PDF abspeichern. Das physische Ausdrucken erledige ich gerne händisch, wobei es mir aber zusehends müßig wird, jedesmal nochmals des Druckdialog aufzurufen und "Als PDF sichern..." zu klicken.


Überlegung
Ich möchte eine Ordneraktion erstellen, welche:
  1. Jede neue Excel im Ordner Rechnungen automatisch in eine PDF konvertiert und dabei die Seitenumbrüche der Datei sowie das darin definierte Seitenformat berücksichtigt.
  2. Diese neu erstellten PDF ebenfalls automatisch in den Unterordner Rechnungen PDF verschiebt.
  3. Beim Verschieben der PDF einen Datumsstempel an den Dateinamen anhängt - nach dem Muster dateinamederoriginaldatei_jjjj-mm-tt.pdf

Herangehensweise

Ich habe versucht ein bereits existierendes Skript gemäss meinen Anforderungen zu modifizieren. Hierbei habe ich das convert - PostScript to PDF.scpt als Grundlage genommen und property type_list und property extension_list geändert. Das Ganze funktioniert anscheinend soweit bis zum Shellscript pstopdf. Und ab hier bin ich vollkommen aufgeschmissen. Ich hab mich durch Google und diverse Foren gewälzt, aber bin immer nicht schlauer. Ich hoffe daher wirklich auf euer geballtes Schwarmwissen und eure Hilfe... Ich erwarte kein "vorgekautes" Ergebnis, freue mich aber über jeden Tipp, Hinweis, Codeschnipsel etc.!


Skript bisher
Code:
property done_foldername : "PDF Files"
property originals_foldername : "Original Files"
property newimage_extension : "pdf"
-- the list of file types which will be processed
-- eg: {"PICT", "JPEG", "TIFF", "GIFf"}
property type_list : {"XLSX"}
-- since file types are optional in Mac OS X,
-- check the name extension if there is no file type
-- NOTE: do not use periods (.) with the items in the name extensions list
-- eg: {"txt", "text", "jpg", "jpeg"}, NOT: {".txt", ".text", ".jpg", ".jpeg"}
property extension_list : {"xlsx"}


on adding folder items to this_folder after receiving these_items
    tell application "Finder"
        if not (exists folder done_foldername of this_folder) then
            make new folder at this_folder with properties {name:done_foldername}
        end if
        set the results_folder to (folder done_foldername of this_folder) as alias
        if not (exists folder originals_foldername of this_folder) then
            make new folder at this_folder with properties {name:originals_foldername}
            set current view of container window of this_folder to list view
        end if
        set the originals_folder to folder originals_foldername of this_folder
    end tell
    try
        repeat with i from 1 to number of items in these_items
            set this_item to item i of these_items
            set the item_info to the info for this_item
            if (alias of the item_info is false and the file type of the item_info is in the type_list) or (the name extension of the item_info is in the extension_list) then
                tell application "Finder"
                    my resolve_conflicts(this_item, originals_folder, "")
                    set the new_name to my resolve_conflicts(this_item, results_folder, newimage_extension)
                    set the source_file to (move this_item to the originals_folder with replacing) as alias
                end tell
                process_item(source_file, new_name, results_folder)
            end if
        end repeat
    on error error_message number error_number
        if the error_number is not -128 then
            tell application "Finder"
                activate
                display dialog error_message buttons {"Cancel"} default button 1 giving up after 120
            end tell
        end if
    end try
end adding folder items to

on resolve_conflicts(this_item, target_folder, new_extension)
    tell application "Finder"
        set the file_name to the name of this_item
        set file_extension to the name extension of this_item
        if the file_extension is "" then
            set the trimmed_name to the file_name
        else
            set the trimmed_name to text 1 thru -((length of file_extension) + 2) of the file_name
        end if
        if the new_extension is "" then
            set target_name to file_name
            set target_extension to file_extension
        else
            set target_extension to new_extension
            set target_name to (the trimmed_name & "." & target_extension) as string
        end if
        if (exists document file target_name of target_folder) then
            set the name_increment to 1
            repeat
                set the new_name to (the trimmed_name & "." & (name_increment as string) & "." & target_extension) as string
                if not (exists document file new_name of the target_folder) then
                    -- rename to conflicting file
                    set the name of document file target_name of the target_folder to the new_name
                    exit repeat
                else
                    set the name_increment to the name_increment + 1
                end if
            end repeat
        end if
    end tell
    return the target_name
end resolve_conflicts

-- this sub-routine processes files
on process_item(source_file, new_name, results_folder)
    -- NOTE that the variable this_item is a file reference in alias format
    -- FILE PROCESSING STATEMENTS GO HERE
    try
        set the source_item to the quoted form of the POSIX path of the source_file
        -- the target path is the destination folder and the new file name
        set the target_path to the quoted form of the POSIX path of (((results_folder as string) & new_name) as string)
        with timeout of 900 seconds
            do shell script ("pstopdf " & source_item & " -o " & target_path)
        end timeout
    on error error_message
        tell application "Finder"
            activate
            display dialog error_message buttons {"Cancel"} default button 1 giving up after 120

        end tell

    end try

end process_item

Und natürlich
Vielen vielen tausend Dank schonmal im Voraus für eure Hilfe!!

 
Zuletzt bearbeitet von einem Moderator:
Bitte die "code"-Tags verwenden.

code-tags.png
 
mit pstopdf kommst du nicht hin, weil du damit ja nur postscript files wandeln kannst und die müsstest du ja auch erst aus dem xlsx erzeugen.

1. excel mit applescript ansteuern und dort deine datei als pdf speichern. damit gibt es aber ab und zu probleme ein single-pdf zu erstellen.

oder

2. libreoffice installieren
Code:
/Applications/LibreOffice.app/Contents/MacOS/soffice --headless --convert-to pdf:"calc_pdf_Export" --outdir ~/outdir file.xlsx
 
Zurück
Oben Unten