©
. Document créé le 17 août 2010 , mis à jour le 22 août 2010.Il n'est point de secret que le temps ne révèle. Jean Racine
Accueil du site > Outils > Renommer les fichiers en tout ascii par lot
Dans la série des automates de conversion, voici un utilitaire écrit en AppleScript et PHP (version ligne de commande) qui permet de renommer vos fichiers par lots par simple glisser/déposer (drag and drop). Une droplet.
L’application Nom de fichier en ascii.app transforme le nom du fichier en remplaçant tout caractère non-ASCII (7 bits, de a à z, de A à Z, de 0 à 9 et le point) en un seul espace souligné (_).
Télécharger la droplet ? Le lien est en bas de page.
A noter que pour Unix, un dossier est un fichier, d’un type différent. L’action fonctionnera donc sur son nom. Mais la fonction n’est pas récursive. Les fichiers contenus dans ce dossier ne seront pas traités automatiquement.
Cet exemple illustre entre autres, l’emploi d’un script PHP intégré en tant que ressource dans la droplet.
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
L’élaboration d’une droplet peut être simplement réalisée via l’Editeur AppleScript (Applications/Utilitaires).
Il faut simplement enregistrer son script en tant qu’Application.
Dans cet exemple, le script AppleScript ne sert qu’à transmettre la sélection de l’utilisateur à un script en PHP qui a la charge de renommer les fichiers.
Le code AppleScript :
(*
Renommer fichier et dossier
en remplacant tout ce qui n'est pas
ascii par espace souligné (_)
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/Renommer-les-fichiers-en-tout-ascii-par-lot
@version: Mon Aug 16 17:18:41 CEST 2010
*)
property version : 1.0
property app_name : "RenameInAscii"
on log (msg)
do shell script ("logger " & app_name & " " & msg)
end log
-- input contient la sélection de fichiers
on open (input)
-- récupère la liste des fichiers déposés sur l'icône
if (class of input) is not equal to list then set input to {input}
set files_nb to 0
set files_args to ""
set renamer to path to resource "filename2ascii.php" in directory "Scripts"
set renamer to quoted form of (POSIX path of renamer)
-- pour chaque nom de fichier transmis
repeat with from_file in input
-- le chemin POSIX du fichier source pour navigation shell
set files_args to files_args & quoted form of (POSIX path of from_file as string) & " "
set files_nb to files_nb + 1
end repeat
-- Appel du script PHP
set nb_ok to do shell script (renamer & " " & files_args)
-- Préparer le message de fin
set title_msg to nb_ok & "/" & files_nb
if (files_nb > 1) then
set title_msg to title_msg & " fichiers renommés"
else
set title_msg to title_msg & " fichier renommé"
end if
set title_msg to title_msg 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
Le script PHP est à intégrer dans les ressources de la droplet. Pour réaliser cette intégration :
Le code du script PHP :
#!/usr/bin/php
<?php
// @author: Christian Paulus
// @see: http://www.quesaco.org/Renommer-les-fichiers-en-tout-ascii-par-lot
// @version: Mon Aug 16 17:18:41 CEST 2010
if($argc > 1) {
function _log($msg, $priority = LOG_NOTICE) {
openlog('filename2ascii', LOG_PID | LOG_PERROR, LOG_LOCAL0);
syslog($priority, $msg);
closelog();
}
$ok = 0;
// Le premier arg est le nom de ce script
// Passer à l'argument suivant
while ($arg = next($argv)) {
if(is_writable($arg)) {
$d = dirname($arg);
$b = basename($arg);
// remplacer tout ce qui n'est pas ascii
// par un '_' en évitant les doublons
$to = preg_replace('/[^a-z0-9\.]+/i', '_', $b);
$to = trim($to, "_");
// si identique, ignorer
if($b == $to) continue;
_log('Rename '.$d.'/'.$b.' -> '.$to);
// Aller dans le directory
if(chdir($d)) {
// Renommer le fichier
if(rename($b, $to)) {
$ok++;
}
else{
_log('Error: rename '.$b.' -> '.$to, LOG_ERR);
}
}
else {
_log('Error: chdir '.$d, LOG_ERR);
}
}
else {
_log('Error: no writable '.$arg, LOG_ERR);
}
}
// Renvoyer le nombre de fichiers traités
echo($ok);
}
Passer directement à l’application ? Voici la droplet, à décompresser avant usage :
Les forums sont fermés.