©
. Document créé le 28 juillet 2010 , mis à jour le 2 août 2010.Ce n'est pas le tout d'être une femme du monde. Encore faut-il savoir de quel monde il s'agit. Pierre Dac
Accueil du site > Astuces > MacOSX > Convertir document OpenOffice.org en HTML (automate)
Le script applescript OOo2html permet d’automatiser la conversion de fichiers “texte” (doc, rtf, etc.) en HTML, avec récupération des images.
Ce script nécessite la présence d’OpenOffice.org sur votre Mac, dans sa version 3 a minima.
Le script crée - si besoin - un répertoire de destination exports_HTML sur le bureau (desktop). Et pour chaque fichier glissé sur l’icone OOo2html, un répertoire nommé des trois premières lettres du nom du fichier concerné, suivi de la date, est créé. Dans ce dernier répertoire, un fichier index.html contient le texte du fichier concerné. Les images dudit fichier sont restituées dans le répertoire images, adjacent au susdit fichier index.html. Pff ! J’ai pas fait une phrase aussi alambiquée depuis longtemps !
Vous pouvez utiliser ce code, le redistribuer et / ou le modifier selon
les termes de la Licence Publique Générale GNU telle que publiée par
la Free Software Foundation version 3.
Ce code est distribué dans l’espoir qu’il sera utile,
mais SANS AUCUNE GARANTIE, sans même la garantie implicite
de COMMERCIALISATION ou D’ADAPTATION A UN USAGE PARTICULIER.
Voir la Licence Publique Générale GNU pour plus de détails
à http://www.gnu.org/licenses/gpl-3.0.html
OOo2html écrit ses résultats dans des fichiers, sur votre disque dur. Consultez le source applescript de OOo2html pour comprendre son fonctionnement. Et surtout, sauvegardez vos données !
OpenOffice.org n’est pas scriptable. Afin de faciliter le pilotage de cette application via les évènements système, vous devez ajouter deux raccourcis clavier à vos préférences. La marche à suivre pour ajouter ces deux raccourcis clavier à OpenOffice.org est explicitée dans ce billet.
(*
OOo2html
Convert any text (doc, odt, rtf) to html
using OpenOffice.org/MacosX Snow Leopard
Copyright © 2010 Christian Paulus
http://www.quesaco.org
You can use this code, redistribute it and/or modify it under
the terms of the GNU General Public License as published by
the Free Software Foundation version 3.
This code is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty
of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details
at http://www.gnu.org/licenses/gpl-3.0.html
@author: Christian Paulus
@see: http://www.quesaco.org
@version: Mon Aug 02 11:03:00 CEST 2010
*)
-- my_folder et le dossier de réception des autres dossiers
property my_folder : "exports_HTML"
-- temps d'attente.
-- Si machine lente, placer 5
-- sinon, 1 convient en général
property my_pause : 1
-- ce chiffre est parfois pondéré par la taille
-- du fichier
property weighting : (1024 * 8)
-- message sonore en fin de job
-- mettre 'false' pour empêcher
property say_message : true
-- le moteur de conversion
property my_app : "OpenOffice.org"
property app_name : "OOo2HTML"
-- input contient la sélection de fichiers
on open (input)
-- récupère la liste des fichiers dropés
if (class of input) is not equal to list then set input to {input}
if input = {} then return input
-- le dossier de réception principal sur le bureau
set root_folder to (path to desktop as text) & my_folder
-- logiquement: ~/Desktop/exports_HTML (dans son format POSIX)
--log "root= " & root_folder
-- le finder doit créer le dossier
tell application "Finder"
-- s'il n'existe pas ;-)
if not (exists root_folder) then
--log "le dossier n'existe pas. Tenter créer..."
try
tell application ¬
"Finder" to make new folder at desktop ¬
with properties {name:my_folder}
on error the errstr number the errnum
display dialog "Error:" & errnum & ": " & errstr
end try
else
--log "le dossier de réception est disponible"
end if
end tell
-- lancer l'application
tell application my_app
-- l'application en premier plan
activate
try
set nb_fichiers to 0
-- pour chaque nom de fichier transmis
repeat with this_file in input
-- récuperer le nom du fichier sans son extension
set the_name to this_file
set prev_delim to AppleScript's text item delimiters
try
set AppleScript's text item delimiters to {":"}
set with_ext to the last text item in (the_name as string) as string
set AppleScript's text item delimiters to {"."}
set the_name to the first text item in with_ext as string
on error the errstr number the errnum
display dialog "Error:" & errnum & ":" & errstr
end try
-- restituer le délimiteur
set AppleScript's text item delimiters to prev_delim
-- créer le nom du dossier de destination
set this_folder to ""
set n to count of the_name
repeat with i from 1 to n
set this_char to item i of the_name
-- conserver les trois premiers caractères
-- du nom de fichier transmis
if this_char ≥ ¬
"a" and this_char ≤ ¬
"z" or this_char ≥ ¬
"A" and this_char ≤ "Z" then
set this_folder to this_folder & this_char
if i > 2 then
exit repeat
end if
set n to n + 1
end if
end repeat
-- manque de caractères ?
if the length of this_folder = 0 then
set this_folder to "www"
end if
-- ajouter un petit tiret
set this_folder to this_folder & "-"
-- compléter par la date
set this_date to current date
set y to year of this_date
set m to month of this_date as integer
-- compléter par "0" si trop court
if m < 10 then set m to "0" & m
set d to day of this_date
-- compléter par "0" si trop court
if d < 10 then set d to "0" & d
set t to time of this_date
set this_folder to this_folder & y & m & d & "-" & t as string
-- créer le dossier de réception pour ce fichier
tell application "Finder" to make new folder at root_folder ¬
with properties {name:this_folder}
-- le chemin POSIX pour navigation
set posix_path_index to (POSIX path of root_folder) ¬
& "/" & this_folder & "/index.html"
-- taille du fichier
set info_file to info for file (this_file)
set file_size to size of info_file
-- pondérer les attentes
set long_pause to my_pause + my_pause * (file_size / weighting as integer)
-- ouvrir le fichier
tell application my_app to open this_file
do shell script "logger " & app_name & ¬
": ouverture " & this_file & " " & file_size & " octets. Pause " & long_pause
delay long_pause
tell application "System Events"
try
-- exporter le fichier
-- Attention à bien avoir suivi les instruction
-- http://www.quesaco.org/Ajouter-un-raccourci-clavier
-- afin de créer les raccourcis clavier nécessaires
keystroke "e" using {option down, command down}
delay 1
-- forcer le chemin...
-- Taper un '/' pour faire apparaitre la boite de sélection
key code 47 using {shift down}
delay 1
-- supprimer le /
key code 51 -- del
-- remplacer par le bon chemin
keystroke posix_path_index
delay 1
-- valider
key code 36 -- return
delay 1
-- forcer le format HTML
keystroke "e" using {shift down, option down, command down}
-- valider
key code 36 -- return
delay long_pause
do shell script "logger " & app_name & ": fermeture fichier "
keystroke "w" using {command down}
delay my_pause
on error the errstr number the errnum
display dialog "Error:" & errnum & ":" & errstr
end try
end tell
set nb_fichiers to nb_fichiers + 1
end repeat
-- message de fin
set title_msg to nb_fichiers
if (nb_fichiers > 1) then
set title_msg to title_msg & " fichiers convertis"
else
set title_msg to title_msg & " fichier converti"
end if
set title_msg to title_msg & " en HTML. " as string
set text_msg to "Travail terminé." as string
-- notification Growl si présent
tell application "System Events"
set isRunning to ¬
(count of ¬
(every process whose name is "GrowlHelperApp")) > 0
if (isRunning = true) then
tell application "GrowlHelperApp"
-- Liste des types de notification
set the allNotificationsList to ¬
{"Notification 1", "Notification 2"}
-- Notifications activées par défaut
set the enabledNotificationsList to ¬
{"Notification 1"}
register as application ¬
app_name all notifications allNotificationsList ¬
default notifications enabledNotificationsList ¬
icon of application "Finder"
-- Envoyer la notification:
notify with name ¬
"Notification 1" title ¬
title_msg description ¬
text_msg application name app_name
end tell
end if
end tell
-- notification orale
if say_message then
say title_msg & text_msg
end if
-- message en syslog
do shell script "logger " & app_name & ": " & title_msg & text_msg
quit
on error the error_message number the error_number
display dialog "Error: " & the error_number & ¬
". " & the error_message buttons {"OK"} default button 1
end try
end tell
return input
end open
Problème de copier/coller ? Voici la version compressée à télécharger :
Les forums sont fermés.