Minify CSS with YUI Compressor and AppleScript
I like using YUI Compressor to minify CSS files for production websites but like a lot of people, I don’t like doing it from Terminal. I wrote an AppleScript today to Minify CSS files from TextWrangler. The script will be added to the next version of Vormplus Webtools. In this article I’m going to explain how the script works. The script requires YUI Compressor to be installed in the Applications folder of Mac OS X.
Getting the Path and Filename
tell application "TextWrangler"
set cssDoc to file of text document 1
end tell
set csspath to POSIX path of cssDoc as Unicode text
First part of this code snippet tells TextWrangler to set the filename of the CSS document to the variable cssDoc. Next we set the csspath variable to the
the POSIX path of the file. This is important to execute the shell script. AppleScript uses : as delimiter for paths, Terminal needs a / as delimiter.
New Filename
set newpath to createNewFileName(csspath)
The next line creates a new filename for the minified CSS file. I’m using YUI Compressor to save the minified CSS to a new file, so if something goes wrong, I still have my original CSS file. The AppleScript handler createNewFilename(oldFileName) changes the name from vormplus.css to vormplus-min.css.
-- create new filename from old filename
on createNewFileName(oldFileName)
set {atid, AppleScript's text item delimiters} to ¬
{AppleScript's text item delimiters, ".css"}
set tempText to text items of oldFileName
set AppleScript's text item delimiters to "-min.css"
set newFileName to tempText as Unicode text
set AppleScript's text item delimiters to atid
return newFileName
end createNewFileName
Execute the Shell Script
-- set path to YUI Compressor
set yuicompressor to "/Applications/yuicompressor-2.4.2/ ¬
build/yuicompressor-2.4.2.jar"
-- create shell script
set theShellScript to "java -jar " & yuiCompressorPath ¬
& " --type css " & csspath & " -o " & newpath
-- execute shell script
do shell script theShellScript
Last part of the script is telling AppleScript where to find YUI Compressor, put the shell script together and execute it.
Download
Download Minify CSS
Commenting is closed for this article.
0 Opinions posted so far. Now go post your own. To the comment form!