Je n’avais pas réalisé que la solution proposée était quasiment la même que la mienne. Désolé donc….
Voici pour les utilisateurs de mac OS le script qui permet de comprimer les pdf en les glissant sur l’icône du droplet. On obtient aussi les statistiques de taux de compression, ce qui peut aider à toruver le réglage idéal.
– DEBUT SCRIPT
(* Projet: Script de compresssion de fichiers pdf.
*
- Version: 0.9.0 initiale
- Compatibilité: Mac OS X
- Auteur: Yves
- Fichier:
- Fichiers/Dossiers annexes:
- Libs:
- Osaxen:
- Outils Unix: oui
- Apps:
- Divers:
- Description: L’utilisateur choisit un fichier pdf, puis des options de compression. Le fichier source n’est pas affecté.
-
- Utilisation:
-
...Lancer le script...
- Historique:
- 0.9.3
- 2012-10-19 Version alpha Droplet
- 2012-10-21 Ajouté vérif installation Ghoscript et taux de compression*)
property ATID : “” – Standard delimiters
property NomDuFichierACompresser : missing value
property FileExtension : missing value
property OptionForCompression : missing value
property PXpathOfContainingFolder : missing value
property PFilesize1 : missing value
property PFilesize2 : missing value
property PXPathOfTargetFile : missing value
property PNomDuFichierDeSortie : missing value
–property PLabelIndex : 3 – Code couleur icône fichier cible: 3=Jaune
property pThisitem : missing value
property pStartTime : missing value
on run
display dialog “Pour utiliser ce script, faites glisser sur l’icône le document pdf à comprimer”
end run
on open draggedItems
try
if my GhostscriptInstalled() is not true then
DisplayDialog(“Ce script ne fonctionne pas sans installer d’abord Ghostscript !”)
error number -128
end if
repeat with pThisitem in draggedItems
tell application "Finder"
set k to type identifier of (info for pThisitem)
if k is not "com.adobe.pdf" then
set d to "Désolé! Ce fichier n'est pas un fichier pdf!"
my DisplayDialog(d)
error number -128
end if
end tell
my compress(pThisitem)
end repeat
end try
end open
on init(f)
–try
–set f to choose file with prompt “Veuillez choisir le fichier .pdf à comprimer” of type {“com.adobe.pdf”} without invisibles – on demande de choisir le fichier à traiter
set OptionForCompression to choose from list {“screen”, “ebook”, “printer”, “prepress”} — On demande l’OptionForCompression de compression
set NomDuFichierACompresser to name of (info for f) --On extrait le nom du fichier
set FileExtension to name extension of (info for f) as string
set PFilesize1 to size of (info for f) as number – On obtient la taille du fichier de départ
CreateNewFileName(NomDuFichierACompresser, FileExtension)
set PXpathOfContainingFolder to quoted form of GetParentPath(f) --On détermine le chemin POSIX du répertoire de travail
set PXPathOfTargetFile to GetParentPath(f) & "/" & PNomDuFichierDeSortie --On détermine le chemin POSIX du fichier comprimé à créer
set pStartTime to my ReturnTime()
-- end try
end init
on compress(pThisitem)
try
init(pThisitem)
set AppleScript’s text item delimiters to “.” & FileExtension – On change temporairement le délimiteur
set NomSansExtension to text item 1 of (NomDuFichierACompresser) --On obtient le nom fichier sans l’extension
set NomDuFichierDeSortie to NomSansExtension & “-modifié.” & FileExtension – On crée le nom du fichier cible
set AppleScript’s text item delimiters to ATID --On rétablit le délimiteur par défaut
do shell script CommandeShell(PXpathOfContainingFolder, OptionForCompression, NomDuFichierDeSortie, NomDuFichierACompresser) – On execute la commande
set PFilesize2 to size of (info for POSIX file PXPathOfTargetFile as alias) --On détermine la taille du fichier comprimé cible
--tell application "Finder" to set label index of (POSIX file PXPathOfTargetFile as alias) to PLabelIndex --Coloration icône fichier cible
DisplayDialog(SetDialog()) --Dialogue de fin
on error
set AppleScript's text item delimiters to ATID --On rétablit le délimiteur par défaut
end try
end compress
on CommandeShell(PXpathOfContainingFolder, OptionForCompression, NomDuFichierDeSortie, NomDuFichierACompresser)
set cmd0 to “cd” & space & PXpathOfContainingFolder & space & “;” & space – Changement de répertoire et debut de la creation commande shell
set cmd1 to “/usr/local/bin/gs -q -dSAFER -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -dUseCIEColor=true -dPDFSETTINGS=/” – Version pdf couleur
–set cmd1 to “/usr/local/bin/gs -q -dSAFER -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sColorConversionStrategy=Gray -dProcessColorModel=/DeviceGray -dPDFSETTINGS=/” – Pdf en nuances de gris
set cmd2 to OptionForCompression & " -sOUTPUTFILE="
set cmd3 to quoted form of NomDuFichierDeSortie & space & " -f" & space & quoted form of NomDuFichierACompresser
set cmd to cmd0 & cmd1 & cmd2 & cmd3 – commande shell
return cmd
end CommandeShell
on GetParentPath(TheAlias)
tell application “System Events”
set ContainerPXPath to POSIX path of (container of TheAlias)
end tell
return ContainerPXPath
end GetParentPath
on CreateNewFileName(NomDuFichierACompresser, FileExtension)
set AppleScript’s text item delimiters to “.” & FileExtension – On change temporairement le délimiteur
set NomSansExtension to text item 1 of (NomDuFichierACompresser) --On obtient le nom fichier sans l’extension
set PNomDuFichierDeSortie to NomSansExtension & “-modifié” & “.” & FileExtension – On crée le nom du fichier cible
set AppleScript’s text item delimiters to ATID --On rétablit le délimiteur par défaut
end CreateNewFileName
on SetDialog()
set dlg1 to “Compression terminée !” & return
set dlg2 to my CompressionRatio(PFilesize1, PFilesize2) & return & return
set dlg3 to “Temps de l’opération:” & space & ((my ReturnTime()) - pStartTime) & space & “seconde(s)”
return dlg1 & dlg2 & dlg3
end SetDialog
on DisplayDialog(d)
activate
display dialog d giving up after 15
end DisplayDialog
on CompressionRatio(Size1, Size2)
set ratio to round 100 * ((Size1 - Size2) / Size1)
if ratio < 0 then
set resultat to (“La taille du nouveau fichier n’est pas réduite,” & return & “mais augmentée de” & space & (-1 * ratio as text)) & “% !”
else
set resultat to “Le taux de compression est de:” & space & ratio & space & “%” & return & "La taille du fichier comprimé est de: " & (round 1.0E-3 * (Size2 as number)) & space & “Ko.”
end if
return resultat
end CompressionRatio
on GetFileSize(TheAlias)
return size of (info for TheAlias)
end GetFileSize
on GhostscriptInstalled()
set TestPath to “/usr/local/bin/gs”
tell application “Finder”
if exists (TestPath as POSIX file) then
return true
else
set TestPath to “/usr/bin/gs”
if exists (TestPath as POSIX file) then
return true
else
return false
end if
end if
end tell
end GhostscriptInstalled
on ReturnTime()
return time of (current date) as number
end ReturnTime
–FIN SCRIPT