在原始文件夹内将AppleWorks文件批量转换为PDF,删除原始文件?

在原始文件夹内将AppleWorks文件批量转换为PDF,删除原始文件?

可能 AppleScript 是解决此问题的方法 - 我在网上找到了可以执行此操作的脚本,但打印区域过大且文件放在同一个文件夹中会遇到问题 - 我需要文件保留在源文件夹中。如果脚本还删除了原始 AppleWorks 文件,那就更好了,但不是必需的。

我已经尝试了这篇文章中的最后一个脚本:

https://discussions.apple.com/message/10127260#10127260#10127260

任何建议将不胜感激。

以下是 Yvan 的最终代码:

--{code}
--[SCRIPT batch_AW2PDF]
(*
This script batch prints AW docs in PDF files.

 To use the script, save it as a script or as an "Application Bundle".
As it's not a script which will be used daily, it's not useful to install it in the Scripts Menu.

Run it (or double click its icon)
navigate to the folder containing the docs
click "OK"

or

drag and drop AW documents or folders 
containing AW documents on the script's icon.

In both cases they will be saved as PDF documents.

Yvan KOENIG (Vallauris, FRANCE)
2009/08/27
2010/08/25 - revised
2012/01/10 - supposed to store the PDF in the original folder
2012/01/11 - filter slash and colon in filenames.
*)
--=====

property msg3 : "" -- globale
property msg90 : "" -- globale
property msg91 : "" -- globale
property msg94 : "" -- globale
property msg97 : "" -- globale
property rapport : "" -- globale
property dossierParDefaut : "" -- globale

--=====

on run
    set dossier to choose folder
    my main({dossier})
end run

--=====

on open sel
    my main(sel)
end open

--=====

on main(sel)
    local elem, ptree, nomDuRapport, p2d, p2r, MsgErr, NroErr

    if (system attribute "sysv") < 4176 then
        if (do shell script "defaults read 'Apple Global Domain' AppleLocale") starts with "fr_" then
            error "Ce script requiert MacOS X 10.5 ou supérieur !" number 8001
        else
            error "This script requires MacOS X 10.5 or higher !" number 8001
        end if
    end if

    my activateGUIscripting()
    my nettoie()
    my |prépareMessages|()

    set dossierParDefaut to my getDossierParDefaut()
    try
        repeat with elem in sel
            try
                tell application "System Events" to tell disk item ("" & elem)
                    if class is folder then
                        set ptree to ""
                    else
                        set ptree to path of container
                    end if
                end tell -- System Events
                my exploreTraite(elem, ptree)
            end try
        end repeat

        if rapport = "" then set rapport to msg90 (*
crée un fichier texte sur le Bureau *)
        set nomDuRapport to "report_AW2PDF.txt"
        set p2d to path to desktop
        set p2r to "" & p2d & nomDuRapport
        tell application "System Events"
            if exists (file p2r) then delete (file p2r)
            make new file at end of p2d with properties {name:nomDuRapport}
        end tell -- System Events

        write (rapport as text) to (p2r as alias)

        my afficheLeMessage(msg3) (*
Traitement terminé
• Export done.
*)
    on error MsgErr number NroErr
        if NroErr is not -128 then
            beep 2
            tell application (path to frontmost application as string) to ¬
                display alert "" & NroErr & " : " & MsgErr giving up after 20
        end if -- NroErr is…
    end try

    my nettoie()

end main

--=====

on exploreTraite(elem, ptree) (*
elem est un alias
• elem is an alias *)
    local elemT, laClasse, UTI
    set elemT to "" & elem
    tell application "System Events" to tell disk item elemT
        set laClasse to "" & class
        try
            set UTI to type identifier
        on error
            set UTI to ""
        end try
    end tell -- System Events
    (*
CAUTION, if AppleWorks was never launched under the installed operating system,
UTI will resemble to dyn.age81c65e. Happily, "info for" will return the correct one *)
    ignoring case
        if UTI starts with "dyn" then
            set UTI to type identifier of (get info for file elemT)
        end if
    end ignoring

    if UTI is not in {"", "com.apple.appleworks.document", "com.apple.appleworks.cwk"} then
        set rapport to rapport & elemT & msg94 & return (*
Attention, ce n'est pas un document AW.
• Caution, it's not an AW document *)
    else if UTI is in {"com.apple.appleworks.document", "com.apple.appleworks.cwk"} then
        my traiteUnFichier(elemT, UTI)
    else if laClasse is in {"file package", "«class cpkg»"} then
        set rapport to rapport & elemT & msg91 & return (*
Attention, un package EST un dossier "spécial".
• Caution, a package IS a "special" folder. *)
    else if laClasse is in {"folder", "«class cfol»"} then
        my ExploreUnDossier(elemT, ptree)
    else
        set rapport to rapport & elemT & msg94 & return (*
Pas un document AW.
• not an AW file. *)
    end if
end exploreTraite

--=====

on ExploreUnDossier(dossier, ptree)
    local nomElement, cheminElement
    repeat with nomElement in list folder dossier without invisibles
        try
            set cheminElement to dossier & nomElement
            tell application "System Events" to set c to name of folder dossier
            my exploreTraite(cheminElement as alias, ptree & c & ":")
        on error errMsg number errnbr
            set rapport to rapport & dossier & msg97 & return & errnbr & return & errMsg
        end try
    end repeat
end ExploreUnDossier

--=====

on afficheLeMessage(m)
    beep 1
    tell application (path to frontmost application as string)
        activate
        display alert m giving up after 10
    end tell
end afficheLeMessage

--=====

on traiteUnFichier(cheminDocAW, UTI)
    (*
cheminDocAW is a string *)
    local contenant, nomSource, nomFenetre, nomComplet, cheminPDFtemporaire, cheminPDFfinal

    tell application "System Events" to tell disk item cheminDocAW
        set contenant to path of container
        set nomSource to name
    end tell
    copy nomSource to nomPropre
    if nomPropre contains "/" then
        set nomPropre to my remplace(nomPropre, "/", "_")
    else if nomPropre contains ":" then
        set nomPropre to my remplace(nomPropre, ":", "_")
    end if
    if nomPropre is not nomSource then
        tell application "System Events" to set name of disk item cheminDocAW to nomPropre
        set cheminDocAW to contenant & nomPropre
    end if

    set theDoc to cheminDocAW as alias
    tell application "AppleWorks 6"
        activate
        open theDoc
        delay 0.5
        set docName to name of document 1
        set nomFenetre to get name of window 1
    end tell -- AppleWorks

    set nomComplet to nomFenetre & ".pdf"
    set cheminPDFtemporaire to dossierParDefaut & nomComplet
    tell application "System Events"
        if exists (disk item cheminPDFtemporaire) then set name of disk item cheminPDFtemporaire to nomFenetre & my horoDateur(modification date of file cheminPDFtemporaire) & ".PDF" (* name stamped *)
        tell process "AppleWorks 6"
            keystroke "p" using {command down} (* trigger the Print menu *)
            (*
May be a dialog about margins *)
            if (count of buttons of window 1) = 1 then keystroke return
            (*
Now it's the true dialog window *)
            click menu button 1 of window 1 (* click button (PDF) *)
            click menu item 1 of menu 1 of menu button 1 of window 1 (* click item Print in a PDF file *)
            keystroke return (* idem click OK *)
        end tell -- process
    end tell -- System Events…

    my wait4File(cheminPDFtemporaire)

    -- puis on déplace vers le dossier de stockage
    set cheminPDFfinal to "" & contenant & nomComplet
    tell application "System Events" to if exists (file cheminPDFfinal) then set name of file cheminPDFfinal to nomFenetre & my horoDateur(modification date of file cheminPDFfinal) & ".PDF"
    delay 0.2 (* don't remove it *)
    do shell script "mv " & quoted form of POSIX path of cheminPDFtemporaire & space & quoted form of POSIX path of contenant -- <<<<<<<< THE ERROR was here !!!

    my wait4File(cheminPDFfinal)

    tell application "System Events" to if exists file cheminPDFtemporaire then delete file cheminPDFtemporaire

    tell application "AppleWorks 6"
        close document docName without saving
    end tell
end traiteUnFichier

--=====
(* Wait that the file is completely written on disk 
*)
on wait4File(p) (*
• p must be Unicode text *)
    local oldSize, newSize
    delay 0.5
    set oldSize to 0
    repeat --10 times
        try
            tell application "System Events"
                set newSize to (get physical size of file p)
            end tell -- System Events
            if oldSize < newSize then
                set oldSize to newSize
            else
                exit repeat
            end if -- oldSize < newSize
        end try
    end repeat

end wait4File

--=====

on getPlistValue(valName, default)
    local thePlist, u
    set thePlist to (path to preferences folder as Unicode text) & "com.apple.appleworks.plist"
    tell application "System Events"
        if exists file thePlist then
            tell contents of property list file thePlist
                try
                    set u to (value of property list item valName) (* Unicode Text *)
                on error (*
On est là si AppleWorks n'a rien enregistré avec des préférences neuves
• Here if AppleWorks never saved with the new preferences file. *)
                    set u to default
                end try
            end tell -- to contents of…
        else (*
On est là s'il n'y a pas de fichier de préférences
• Here if there is no preferences file. *)
            set u to default
        end if
    end tell -- to system events
    return u
end getPlistValue

--=====

on getDossierParDefaut()
    local u

    try
        set u to my getPlistValue("NSNavLastRootDirectory", "~/Documents")
        set u to "" & POSIX file (do shell script "echo " & u)
        if u ends with ":" then
            return u
        else
            return (u & ":")
        end if
    on error
        return ("" & (path to documents folder))
    end try
end getDossierParDefaut

--=====
(* Build a stamp from the modification date_time
*)
on horoDateur(date_de_modification)
    local les_secondes
    set les_secondes to time of date_de_modification
    return "_" & text -2 thru -1 of ("0" & (month of date_de_modification as integer)) & text -2 thru -1 of ("0" & day of date_de_modification) & "_" & text -2 thru -1 of ("0" & les_secondes div 3600) & text -2 thru -1 of ("0" & (les_secondes mod 3600) div 60) & text -2 thru -1 of ("0" & les_secondes mod 60)
    (* 
• Here, the stamp is  "_YYYYMMDD_hhmmss" *)
end horoDateur

--=====
(*
replaces every occurences of d1 by d2 in the text t
*)
on remplace(t, d1, d2)
    local oTIDs, l
    set oTIDs to AppleScript's text item delimiters
    set AppleScript's text item delimiters to d1
    set l to text items of t
    set AppleScript's text item delimiters to d2
    set t to "" & l
    set AppleScript's text item delimiters to oTIDs
    return t
end remplace

--=====

on activateGUIscripting()
    tell application "System Events"
        if not (UI elements enabled) then set (UI elements enabled) to true (* to be sure than GUI scripting will be active *)
    end tell
end activateGUIscripting

--===== 

on nettoie() (*
pour ne pas stocker dans le fichier script
• So it will not be stored in the script file *)
    set msg3 to ""
    set msg90 to ""
    set msg91 to ""
    set msg94 to ""
    set msg97 to ""
    set rapport to ""
    set dossierParDefaut to ""
end nettoie

--===== 

on |prépareMessages|()
    if (do shell script "defaults read 'Apple Global Domain' AppleLocale") starts with "fr_" then
        set msg3 to "Terminé !"
        set msg90 to "Conversion réussie sans incident."
        set msg91 to " est un Package !"
        set msg94 to " n’est pas un fichier AppleWorks !"
        set msg97 to " n’a pu être identifié !"
    else
        set msg3 to "Done !"
        set msg90 to "No problem during the recoding process."
        set msg91 to " is a Package !"
        set msg94 to " is not an AppleWorks file !"
        set msg97 to " can’t be identified !"
    end if
end |prépareMessages|

--=====
--[/SCRIPT]
--{code}

答案1

Yvan 确实回复了我的询问,并编辑了他的原始脚本以保存源文件夹中的文件并绕过任何丢失资源分支的文件。

我明天将在我的项目中测试他的新脚本的最终运行,一旦完成,在征得他的同意后,我将发布该脚本以便其他人可以使用它。

如果有人立即需要脚本,请告诉我。

答案2

Yvan 在此主题中发布了他原始剧本的链接

https://discussions.apple.com/thread/3815407?start=0&tstart=0

相关内容