Hello
Here is an old script which I wrote to batch convert AppleWorks documents.
I no longer have a system allowing me to run it.
Maybe it may help you.
--[SCRIPT AW batch BOBO2Office8]
(*
Exécuter ce script ou déposer l'icône d'un dossier
sur son icône (version application)
ouvre les documents AppleWorks du dossier
et les enregistreSous en fichier Office
dans leur dossier d'origine.
Run the script or drag and drop a folder icon
on its icon (application version)
open AppleWorks documents stored
in the folder and save them as Office file
in their original folder.
Yvan KOENIG (VALLAURIS, France)
2004/06/11
2004/06/12
2004/06/13
2004/06/14 modifié selon suggestions de Hiroto TAOKA
2004/06/15 traité bug AW6, (merci Hiroto)
2004/06/17
2004/06/18
2004/06/19
2004/11/25
2004/12/03
2008/08/08 (problème Mac OS X 10.5. Merci Hiroto)
2009/08/28 removed the property EnglishMsg
2009/08/28 store in original folder
2011/04/23 added the handler text2alias to take care of a 10.6 feature
2011/04/28 replaced the error message linked to non recognized AW doc by a report entry
2011/12/05 no longer use file type and file creator which may be ""
*)
property drelin_drelin : true (* false pour éviter le signal sonore *)
property extensionWP : ".doc" -- globale
property extensionSS : ".xls" -- globale
property extensionDB : ".doc" -- globale
property extensionGR : "" -- globale
property rapport : "" -- globale
property msg0 : "" -- globale
property msg1 : "" -- globale
property msg5 : "" -- globale
property msg7 : "" -- globale
property msg8 : "" -- globale
property msg91 : "" -- globale
property msg92 : "" -- globale
property msg93 : "" -- globale
property msg94 : "" -- globale
property msg95 : "" -- globale
property msg96 : "" -- globale
property msg97 : "" -- globale
property msg98 : "" -- globale
property msg99 : "" -- globale
property NoTranslation : "" -- globale
property traducteurWP : "" -- globale
property traducteurSS : "" -- globale
property traducteurDB : "" -- globale
property traducteurGR : "" -- globale
property nbDocsOuverts : 0 -- globale
property Bttns1 : {"", "", ""} -- globale
property listeGraphiques : {}
--=====
(* deux lignes exécutées si on double clique
sur l'icône du script application
¢ two lines executed if one double click
the application script's icon *)
if (do shell script "defaults read 'Apple Global Domain' AppleLocale") starts with "fr_" then
set msg00 to "Choisir un dossier ."
else
set msg00 to "Choose a folder ."
end if
tell application "Finder"
choose folder with prompt msg00
set listeFichiers to every item in result
end tell
if (count of listeFichiers) = 0 then return
open listeFichiers
--=========
on open (sel) (* sel contient une liste d'alias des éléments
qu'on a déposés sur l'icône du script (la sélection)
¢ sel contains a list of aliases of the items
dropped on the script's icon (the selection) *)
try
if msg0 is "" then my prepareMessages()
set {rapport, listeGraphiques} to {"", {}}
tell application "AppleWorks 6"
activate
if "6." is not in (version as text) then error msg0 number 8000
set nbDocsOuverts to (count each document)
end tell -- to AppleWorks
set traducteurs to my construitListeTraducteurs()
repeat
tell application (path to frontmost application as string) to set dd to (display dialog ("WP - " & traducteurWP & return & ¬
"SS - " & traducteurSS & return & ¬
"DB - " & traducteurDB & return & ¬
"GR - " & traducteurGR) buttons Bttns1 with icon 1)
if dd's button returned is Bttns1's item 1 then error number -128
(* On a cliqué Annuler/Cancel
utile si on exécute en messages français sur un système anglais.
¢ Click was on Annuler/Cancel
useful if French messages messages are used on an english OS *)
if dd's button returned is Bttns1's item 3 then exit repeat (* Modifier/Reset *)
(* ici si bouton 2 (Modifier)
¢ Here if button 2 (Reset) *)
set {tradWP, tradSS, tradDB, tradGR} to {{NoTranslation}, {NoTranslation}, {NoTranslation}, {NoTranslation}}
repeat with tr in traducteurs
if tr contains "WORD" then
set end of tradWP to tr
else if tr contains "EXCEL" then
set end of tradSS to tr
else if tr contains "ASCII" then
set end of tradDB to tr
else if ((tr contains "JPEG") and not (tr contains "2000")) or (tr contains "TIFF") then
set end of tradGR to tr
end if
end repeat -- with tr
set traducteurWP to my quelTraducteur(tradWP, msg95, traducteurWP)
set traducteurSS to my quelTraducteur(tradSS, msg96, traducteurSS)
set traducteurDB to my quelTraducteur(tradDB, msg97, traducteurDB)
set traducteurGR to my quelTraducteur(tradGR, msg98, traducteurGR)
end repeat
if traducteurGR contains "TIFF" then
set extensionGR to ".tif"
else if (traducteurGR contains "JPEG") then
set extensionGR to ".jpg"
end if
repeat with elem in sel
set elemA to elem as alias -- NOUVEAU
my exploreTraite(elemA, (info for elemA), "")
end repeat
tell application "AppleWorks 6"
activate
if listeGraphiques is not {} then
set traducteurs to my construitListeTraducteurs()
(* indispensable *)
if drelin_drelin is true then beep 3
(* signale qu'il faut intervenir pour fichiers graphiques
¢ claims that the user must click for graphic documents *)
repeat with descripteur in listeGraphiques
set {leFichier, leTraducteur, nouveauChemin} to descripteur
(*
CAUTION ! Under 10.6, we can't coerce a string to an alias in a tell application block
So I moved the coercion in a handler.
*)
open my text2alias(leFichier) (* pas besoin de "try", celà a déjà fonctionné
¢ no need to "try", already worked at first attempt *)
my enregistreSous(leFichier, leTraducteur, nouveauChemin) (* 2 *)
close document 1 without saving
end repeat
end if -- listeGraphiques.
if rapport > "" then make new document with data (msg8 & return & return & rapport)
end tell -- to AppleWorks
on error MsgErr number NroErr
if NroErr is not -128 then
beep 2
activate
display dialog "" & NroErr & " : " & MsgErr with icon 0 buttons {msg99} giving up after 20
end if -- NroErr is.
return
end try
end open
--===== Routines
on creeNomUnique(dossier_, nom_, extension_)
set {suffixe, i} to {extension_, 1}
set {laFin, chemin_} to {suffixe, ""}
repeat
try
set chemin_ to "" & dossier_ & nom_ & laFin
chemin_ as alias (* un fichier de même nom existe-t-il ?
si oui (pas d'erreur) incrémenter l'index *)
set i to i + 1
set laFin to space & "#" & i & suffixe
on error (* s'il n'existe pas (erreur) c'est bon. *)
exit repeat
end try
end repeat
return nom_ & laFin
end creeNomUnique
--=====
on quelTraducteur(listeTrad, msg_, traducteur_)
activate
set monChoix to choose from list listeTrad with prompt msg_ default items traducteur_ as list
if monChoix is false then error msg5 number 8005
return (monChoix as text)
end quelTraducteur
--=====
on enregistreSous(fichier_, traducteur_, chemin_)
if my quelOS() is not less than "1050" then set traducteur_ to my textAsStringData(traducteur_)
tell application "AppleWorks 6"
activate
try
save document 1 in file chemin_ using translator traducteur_
(* enregistre dans le dossier sur le bureau,
¢ save in the folder on the desktop *)
on error errMsg number errNum
set rapport to rapport & errMsg & " -- " & errNum & return & msg94 & fichier_ & return
end try
end tell -- to AppleWorks
repeat 10 times
try
tell application "Finder" to update file chemin_
set existe to true
on error errMsg number errNum
set existe to false
end try
if existe is true then exit repeat
-- delay 5
end repeat
try
chemin_ as alias
on error -- chemin_ is not present
set rapport to rapport & errMsg & " -- " & errNum & return & msg94 & fichier_ & return
end try
--tell application "Finder" to get properties of file chemin_
end enregistreSous
--=====
on exploreTraite(elem, les_infos, ptree)
set elem_ to elem as text
if (folder of les_infos) then
try
set pf to (package folder of les_infos) as boolean
on error
set pf to (package of les_infos) as boolean
end try
if pf is true then (* amandine *)
set rapport to rapport & msg93 & elem_ & return
(* Attention, un package EST un dossier "spécial".
¢ Caution, a package IS a "special" folder. *)
else
my ExploreUnDossier(elem_, ptree)
end if -- (package.
else
(*
No longer use file creator which maybe "" *)
read elem from 5 for 4
if result is "BOBO" then
--if file creator of les_infos is "BOBO" then
my TraiteUnFichier(elem_, les_infos, ptree)
else
set rapport to rapport & msg93 & elem_ & return
(* Rejette fichier non Claris/AppleWorks.
¢ Skip non Claris/AppleWorks files. *)
end if -- file type.
end if
end exploreTraite
--=====
on ExploreUnDossier(dossier, ptree)
repeat with nomElement in list folder dossier without invisibles
set cheminElement to dossier & nomElement
tell application "Finder" to set C to name of (dossier as alias)
my exploreTraite(cheminElement as alias, (info for file cheminElement), ptree & C & ":")
end repeat
end ExploreUnDossier
--=====
on TraiteUnFichier(leFichier, les_infos, ptree)
tell application "Finder"
set leVraiNom to (name of alias leFichier)
set leDossier to (container of alias leFichier) as text
end tell
if leVraiNom ends with ".cwk" then set leVraiNom to (text 1 thru -5 of leVraiNom) as text
(* élimine l'extension .cwk, laisse l'extension ".cws" des modèles
¢ strip .cwk extension, keep extension ".cws" of template files *)
(*
No longer use file type which may be "" *)
try
set nil to (character id 0) & (character id 0) & (character id 0) & (character id 0)
on error
set nil to (ASCII character 0) & (ASCII character 0) & (ASCII character 0) & (ASCII character 0)
end try
set tDoc to file type of les_infos
if tDoc is nil then set tDoc to my quelAW(leFichier)
if tDoc is in {"CWWP", "TEXT", "sWWP"} then
set {leTraducteur, monExtension} to {traducteurWP, extensionWP}
else if tDoc is in {"CWSS", "sWSS"} then
set {leTraducteur, monExtension} to {traducteurSS, extensionSS}
else if tDoc is in {"CWDB", "sWDB"} then
set {leTraducteur, monExtension} to {traducteurDB, extensionDB}
else if tDoc is in {"CWPT", "sWPT", "CWGR", "sWGR"} then
set {leTraducteur, monExtension} to {traducteurGR, extensionGR}
else
--error msg7 number 8007
set {leTraducteur, monExtension} to {"????", "????"}
end if -- tDoc.
if leTraducteur is not in {NoTranslation, "????"} then
tell application "AppleWorks 6"
(*
CAUTION ! Under 10.6, we can't coerce a string to an alias in a tell application block
So I move the coercion in a handler.
*)
try
open my text2alias(leFichier)
on error errMsg number errNbr
--
end try
set nb_Ouverts to (count each document)
end tell -- to AppleWorks
if nb_Ouverts > nbDocsOuverts then
(* Le fichier a pu être ouvert.
¢ The file is really open. *)
(* ici il faut créer un nom unique *)
set nouvoNom to my creeNomUnique(leDossier, leVraiNom, monExtension)
set nouveauChemin to leDossier & nouvoNom
if leTraducteur is traducteurGR then
set end of listeGraphiques to {leFichier, leTraducteur, nouveauChemin}
(* on enregistreSousra les graphiques plus tard
¢ will save graphics later *)
else
my enregistreSous(leFichier, leTraducteur, nouveauChemin) (* 1 *)
end if -- leTraducteur is traducteurGR
tell application "AppleWorks 6" to close document 1 without saving
else
set rapport to rapport & msg92 & leFichier & return
end if -- nb_Ouverts.
else
set rapport to rapport & errMsg & " -- " & errNum & return & msg91 & leFichier & return
end if -- leTraducteur.
end TraiteUnFichier
--=====
on quelAW(le_fichier)
local flag, deb280, versionNum, marqueur
set deb280 to (read file le_fichier from 1 for 280)
if (text 5 thru 8 of deb280) is "BOBO" then (* it's an AppleWorks document *)
set flag to 1
set versionNum to ASCII number of (character 1 of deb280)
if versionNum = 6 then
set marqueur to 279 -- cas de la version AW6
set flag to 3
else if versionNum = 5 then
set marqueur to 269 -- cas de la version AW5/CW5
else if versionNum = 4 then
set marqueur to 258 -- cas de la version CW4
else if versionNum = 3 then
set marqueur to 250 -- cas de la version CW3
else if versionNum = 2 then
set marqueur to 250 -- cas de la version CW2
else if versionNum = 1 then
set marqueur to 244 -- cas de la version CW1
else
set marqueur to 0
end if -- character 1 .
(* The character at offset marqueur is a type signature
0 = CWGR
1 = CWWP
2 = CWSS
3 = CWDB
4 = CWPT
5 = CWPR
*)
if marqueur > 0 then
set maybe to ASCII number (character marqueur of deb280)
if maybe > 5 then
set ftype to "????"
else
set ftype to item (maybe + 1) of {"CWGR", "CWWP", "CWSS", "CWDB", "CWPT", "CWPR"}
end if -- maybe > 5
else
set ftype to "????"
end if -- marqueur > 0
else
set ftype to "????"
end if -- (text 5 thru.
log ftype
return ftype
end quelAW
--=====
on text2alias(t)
return t as alias
end text2alias
--=====
on createDirTree(rc, p)
(*
string rc : root container path where to start directory tree
string p : path string such as "aaa:bbb:ccc:"
return string (rc & p) : path to created directory, such as "rc:aaa:bbb:ccc:"
*)
try
(rc & p) as alias
return (rc & p) -- rc & p is already present
on error --
end try
try
set AppleScript's text item delimiters to ":"
set cc to text items of p
set AppleScript's text item delimiters to ""
set cc to items 1 thru -2 of cc
on error
set AppleScript's text item delimiters to ""
return rc
end try
set x to rc
repeat with C in cc
try
set y to x & C & ":"
y as alias -- check its presence
set x to y
on error
tell application "Finder" to set x to (make new folder at item x with properties {name:C}) as text
end try
end repeat
return x
end createDirTree
--=====
on quelOS()
set the hexData to system attribute "sysv"
set hexString to {}
repeat 4 times
set hexString to ((hexData mod 16) as string) & hexString
set hexData to hexData div 16
end repeat
--log hexString
return hexString as string
end quelOS
--=====
(* Merci/Thanks Hiroto *)
on textAsStringData(t)
(*
text t : source text
return data : «data TEXT...», where ... is hexadecimal representation of t as MacRoman string.
*)
set enTexte to do shell script "printf " & quoted form of t & " | iconv -f utf-8 -t macroman | perl -0777 -ne 'print unpack \"H*\", $_;'"
run (run script "
script
return «data TEXT" & enTexte & "»
end script")
end textAsStringData
--=====
on loadQTTranslators() (* Merci/Thanks Hiroto *)
(*
Une bidouille pour s'affranchir d'un bug d'AW6
Ce dernier "perd" les traducteurs [QT]
après l'ouverture d'un document quelconque.
Cette routine aberrante force AW6 Ã reconstruire
une liste de traducteurs complète (sans le relancer).
¢ A workaround of AW6's bug
AW6 will lose QT origin translators in export/import
translators list after opening a document of any kind.
This absurd handler is to make AW6 recall them in the list
(without restarting AW6)
*)
set n to ((time of (current date)) as string) & ".cwk"
set f to (path to desktop as string) & n
tell application "AppleWorks 6"
activate
make new document with properties {document kind:drawing document, name:n}
tell drawing area of document n to make new oval at front with properties {bounds:{50, 50, 200, 100}}
close document n saving in f
end tell
-- cleaning up dummy document
try
tell application "Finder" to delete f as alias
end try
end loadQTTranslators
--=====
on construitListeTraducteurs()
tell application "AppleWorks 6"
set traducteurs_ to export translators
if "[QT]" is not in (traducteurs_ as text) then
my loadQTTranslators() (* Merci/Thanks Hiroto *)
set traducteurs_ to export translators
end if
end tell -- to AppleWorks
return traducteurs_
end construitListeTraducteurs
--=====
on prepareMessages()
if (do shell script "defaults read 'Apple Global Domain' AppleLocale") starts with "fr_" then
set msg0 to "Ce script n'est pas compatible" & ¬
return & "avec cette version d˜AppleWorks." & return & ¬
"Veuillez utiliser une version 6.0" & return & ¬
"ou plus récente..."
set msg1 to "Préférences AppleWorks corrompues."
set msg5 to "Au revoir !"
set msg7 to "Format non géré !"
set msg8 to "Fichiers non enregistrés."
set msg91 to "Pas traité"
set msg92 to "Pas ouvert"
set msg93 to "Refusé"
set msg94 to "Avorté"
set msg95 to "Choisir un traducteur pour" & ¬
return & "Traitement de textes"
set msg96 to "Choisir un traducteur pour" & ¬
return & "Tableurs"
set msg97 to "Choisir un traducteur pour" & ¬
return & "Base de données"
set msg98 to "Choisir un traducteur pour" & ¬
return & "documents Graphiques"
set msg99 to " Vu "
set NoTranslation to " FORMAT ORIGINAL"
set Bttns1 to {"Annuler", "Modifier", "Accepter"}
else
set msg0 to "This script is not compatible" & return & ¬
"with this version of AppleWorks." & return & ¬
"Please use version 6.0" & return & "or higher..."
set msg1 to "Aworks prefs are corrupted."
set msg5 to "Good bye !"
set msg7 to "this document kind is not treated !"
set msg8 to "Unsaved files."
set msg91 to "Not treated"
set msg92 to "Not open"
set msg93 to "Rejected"
set msg94 to "Failed"
set msg95 to "Choose a translator for" & ¬
return & "Word processing"
set msg96 to "Choose a translator for" & ¬
return & "Spreadsheets"
set msg97 to "Choose a translator for" & ¬
return & "Database"
set msg98 to "Choose a translator for" & ¬
return & "graphic docs"
set msg99 to "Oops"
set NoTranslation to " No translation"
set Bttns1 to {"Cancel", "Reset", " OK "}
end if
set msg91 to "### " & msg91 & " ### "
set msg92 to "### " & msg92 & " ### "
set msg93 to "### " & msg93 & " ### "
set msg94 to "### " & msg94 & " ### "
set {traducteurWP, traducteurSS, traducteurDB, traducteurGR} to {NoTranslation, NoTranslation, NoTranslation, NoTranslation}
end prepareMessages
--[/SCRIPT]
I know that some readers dislike varnames built from French words.
I didn’t made any change to the script because I can’t compile it as AppleWorks is no longer available on my machine.
Yvan KOENIG (VALLAURIS, France) mercredi 3 septembre 2014 17:31:13