©
. Document créé le 2 août 2010 , mis à jour le 2 août 2010.L'idéal quand on veut être admiré, c'est d'être mort. Michel Audiard
Accueil du site > Astuces > MacOSX > Convertir document texte en html (automate)
Le script applescript text2html permet d’automatiser la conversion de fichiers “texte” (doc, rtf, etc.) en HTML, sans les images.
text2html est une droplet. Il vous suffit de déposer vos fichiers texte sur son icone pour lancer la conversion..
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 text2html, un fichier au format HTML y sera copié.
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
text2html utilise la commande interne textutil. Les formats supportés par cette commande sont txt, html, rtf, rtfd, doc, docx, wordml, odt et webarchive
text2html écrit ses résultats dans des fichiers, sur votre disque dur. Consultez le source applescript de text2html pour comprendre son fonctionnement. Et surtout : sauvegardez vos données !
(*
Convert any text (doc, odt, rtf) to html
using textutil tool (so, whithout pictures)
Copyright © 2010 Christian Paulus
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 13:11:00 CEST 2010
*)
-- my_folder et le dossier de réception des autres dossiers
property my_folder : "exports_HTML"
-- le moteur de conversion
property my_app : "/usr/bin/textutil"
-- le format de destination
property to_format : "html"
property app_name : "text2html"
-- 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}
-- 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)
-- 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
set nb_fichiers to 0
-- pour chaque nom de fichier transmis
repeat with from_file in input
set to_file to ""
-- récuperer info sur le fichier
tell application "Finder" to set file_info ¬
to document file from_file
-- son nom
set the_name to name of file_info
-- son extension
set the_ext to name extension of file_info
-- si extension, la supprimer du nom
if (length of the_ext) ≥ 1 then
set to_file ¬
to characters 1 thru ¬
((length of the_name) - ((length of the_ext) + 1)) ¬
of the_name as string
end if
-- vérifier si le fichier de destination existe
-- si oui, incrémenter le nom
set my_incr to 0
repeat
set tmp_file to root_folder & ¬
":" & to_file
if (my_incr > 0) then
set ii to " " & my_incr
else
set ii to ""
end if
set tmp_file to tmp_file & ¬
ii & ".html"
tell application "Finder"
if not (exists tmp_file) then
set to_file to tmp_file
exit repeat
end if
end tell
set my_incr to my_incr + 1
end repeat
if to_file is not "" then
try
-- le chemin POSIX pour navigation shell
-- le fichier source
set from_file_p to (POSIX path of from_file)
-- le fichier destination
set to_file_p to (POSIX path of to_file)
-- la commande et ses paramètres
set my_cde to my_app & ¬
" -convert " & to_format & ¬
" -strip " & ¬
" -output " & (quoted form of to_file_p) & ¬
" " & (quoted form of from_file_p)
-- pour debug
-- message dans /var/log/system.log
-- do shell script "logger " & app_name & " " & my_cde
-- lancer la commande
do shell script my_cde
-- compter le nombre de fichiers
set nb_fichiers to nb_fichiers + 1
on error the errstr number the errnum
display dialog "logger " & app_name & ¬
" Error:" & errnum & ":" & ¬
(quoted form of errstr)
end try
end if
end repeat
-- Préparer le 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
-- message en syslog
do shell script "logger " & app_name & ": " & title_msg & text_msg
return input
end open
En voici la version compressée, à décompresser avant utilisation :
Les forums sont fermés.