YUI Compressor launcher (Finder Toolbar)

Lorsque vous développez en javascript et concevez des feuilles de style CSS, vous pouvez être emmené à utiliser l’outil mis à disposition par Yahoo afin d’optimiser les fichiers et réduire leur taille : YUI Compressor.

Je vous propose un script réalisé en AppleScript qui permet de lancer YUI Compressor et traiter vos fichiers.

Installation

Le programme Java de YUI Compressor est à installer dans le dossier /usr/local/bin. Le script lance le programme nommé : yuicompressor-2.4.2.jar.
Si vous souhaitez mettre à jour le fichier ou changer le dossier d’installation de YUI Compressor, vous pouvez adapter le programme à votre convenance en ouvrant l’application pour éditer le script.

Principe de YUI Compressor launcher :

  • Récupère les fichiers du Finder sélectionnés
  • Vérifie que le fichier comporte l’extension JS (Javascript) ou
    CSS (Cascading Style Sheet)
  • Exécute YUI Compressor et crée une nouvelle version du fichier
    sous la forme : nom-min.js

Code source (applescript) :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
tell application "Finder"
set myFiles to selection
set thisFolder to (target of front window) as Unicode text
set thisFolder to POSIX path of thisFolder

repeat with i from 1 to number of items in myFiles

set file_name to name of item i in myFiles
set file_ext to name extension of item i in myFiles
set file_path to item i in myFiles as string

if (file_ext = "js" or file_ext = "css") then
-- Get the file name without extension
set file_name_noext to name of item i in myFiles as string
set file_name_noext to (the reverse of every character of file_name_noext) as string
set x to the offset of "." in file_name_noext
set file_name_noext to (text (x + 1) thru -1 of file_name_noext) as string
set file_name_noext to (the reverse of every character of file_name_noext) as string

# YUI Compressor
if file_ext contains "js" then
do shell script "java -jar /usr/local/bin/yuicompressor-2.4.2.jar " & "-o " & quoted form of thisFolder & file_name_noext & "-min.js " & quoted form of thisFolder & file_name
else if file_ext contains "css" then
do shell script "java -jar /usr/local/bin/yuicompressor-2.4.2.jar " & "-o " & quoted form of thisFolder & file_name_noext & "-min.css " & quoted form of thisFolder & file_name
end if
else
#Uniquement des fichiers JS et CSS
display dialog "Le fichier traité doit être un fichier Javascript ou CSS." with title "YUI Compressor" with icon 1 buttons {"OK"} default button {"OK"}
end if
end repeat
end tell

Pour télécharger YUI Compressor, voici le lien sur le site de Yahoo Developer :
http://developer.yahoo.com/yui/compressor/



English Flag

If you use YUI compressor to optimize your Javascript and CSS files on Mac OSX. You can use this Finder toolbar script to launch the compressor and minimize your files. The program, made with applescript, open the selected files and create new one on the same folder with a different name (like name-min.js).
YUI compressor must be installed on the folder /usr/local/bin and the script search for yuicompressor-2.4.2.jar. If you have a different configuration, you can adapt the program itself.