My name is Clayton McIlrath and I am an entrepreneur currently living in CO. I personally enjoy the process of learning, exploring, and doing all things creative as well as sharing my experiences with others. Being an entrepreneur and business owner, I hope that my experiences may help someone else start their own venture and find success and freedom as I have! Feel free to contact me anytime for questions or opportunities.

close
more

»

«


AppleScript: YUI Compression

I love learning new programming languages, especially if it solves a need that I have and automates my process a bit more. I spent a couple hours learning AppleScript which is a programming language for Mac that let’s you access the API and make basic function calls to cocoa based programs and applications. One such need that I had is compressing my JavaScript and CSS files before uploading to the web server. There are many applications and ways to this online, but they all are three or more steps to get the compressed file. A while back I downloaded the YUI java applet to do the compression on my Mac via Terminal, which sped up my process a bit. However, since I only used it once in a great while, I’d always have to look up the parameters that I needed to pass to the compressor. So today when I went to look up the syntax, I decided I was going to be adventurous and try something new. That adventure led me to an applescript solution that I’m happy with, and you may enjoy as well.

Download the YUI Compression AppleScript

download the YUI compressor applescript

The AppleScript Source:

on run
	tell application "Finder"
		set this_item to choose file with prompt "Choose CSS or JS file:"
		set dir to the selection as alias
	end tell
	set mypath to posix_path(this_item)
	set dirpath to posix_path(dir)
 
	runCompression(mypath, dirpath)
end run
 
on posix_path(mac_path)
	set mac_path to (mac_path as text)
	set root to (offset of ":" in mac_path)
	set rootdisk to (characters 1 thru (root - 1) of mac_path)
	tell application "Finder"
		if (disk (rootdisk as string) is the startup disk) then
			set unixpath to "/" & (characters (root + 1) thru end of mac_path)
		else
			set unixpath to "/Volumes:" & mac_path
		end if
	end tell
	set chars to every character of unixpath
	repeat with i from 2 to length of chars
		if item i of chars as text is equal to "/" then
			set item i of chars to ":"
		else if item i of chars as text is equal to ":" then
			set item i of chars to "/"
		else if item i of chars as text is equal to "'" then
			set item i of chars to "\\'"
		else if item i of chars as text is equal to "\"" then
			set item i of chars to "\\" & "\""
		else if item i of chars as text is equal to "*" then
			set item i of chars to "\\*"
		else if item i of chars as text is equal to "?" then
			set item i of chars to "\\?"
		else if item i of chars as text is equal to " " then
			set item i of chars to "\\ "
		else if item i of chars as text is equal to "\\" then
			set item i of chars to "\\\\"
		end if
	end repeat
	return every item of chars as string
end posix_path
 
on runCompression(mypath, dirpath)
	tell application "Terminal"
		do shell script "java -jar " & dirpath & "yuicompressor-2.4.2.jar " & mypath & " -o " & mypath & ".min.js"
	end tell
end runCompression

If you like this YUI Compression AppleScript please do share that with me, as praise is my sole motivation for producing scripts for others. I’ve only skimmed the surface of applescript, but with so few scripts out there that meet the needs that I have, I’m sure I could quickly make some other web developer tools for Mac as well.

  • Geoff

    Hey, that’s very helpful. I’d been looking for something like this. I actually cobbled together a Perl script to compress certain raw JS files in my project prior to deployment, but this is a great, generally applicable solution. Thanks!

    – geoff

  • BobMarche

    Thanks for the useful info. It’s so interesting

  • Neil Maclean

    This looks good, thanks very much. Could you do me a big favour and tell me how to use it? It’s downloaded but I don’t know what to do next.
    Many thanks
    Neil

  • Aaron

    Hi there, great website design and much thanks for this script it really helped me get a jump start!

    I was tweaking out on your code and I have it successfully adding the correct extension based on if it’s a .js or .css file (initially all files are saved with .js extension)

    Also, the original code re-writes the file names with the original file extension still intact (eg. styles.css.min.css or scripts.js.min.js). This is fixed now. Here is the working code.

    [code]
    on run
    tell application "Finder"
    set this_item to choose file with prompt "Choose CSS or JS file:"
    set dir to the selection as alias
    set dir to container of dir as text
    set ext to name extension of this_item
    set newName to this_item as text
    set newName to text 1 thru ((offset of "." & ext in newName) - 1) of newName

    end tell
    set newPath to posix_path(newName)
    set mypath to posix_path(this_item)
    set dirpath to posix_path(dir)

    runCompression(mypath, dirpath, newPath, ext)
    end run

    on posix_path(mac_path)
    set mac_path to (mac_path as text)
    set root to (offset of ":" in mac_path)
    set rootdisk to (characters 1 thru (root - 1) of mac_path)
    tell application "Finder"
    if (disk (rootdisk as string) is the startup disk) then
    set unixpath to "/" & (characters (root + 1) thru end of mac_path)
    else
    set unixpath to "/Volumes:" & mac_path
    end if
    end tell
    set chars to every character of unixpath
    repeat with i from 2 to length of chars
    if item i of chars as text is equal to "/" then
    set item i of chars to ":"
    else if item i of chars as text is equal to ":" then
    set item i of chars to "/"
    else if item i of chars as text is equal to "'" then
    set item i of chars to "\'"
    else if item i of chars as text is equal to """ then
    set item i of chars to "\" & """
    else if item i of chars as text is equal to "*" then
    set item i of chars to "\*"
    else if item i of chars as text is equal to "?" then
    set item i of chars to "\?"
    else if item i of chars as text is equal to " " then
    set item i of chars to "\ "
    else if item i of chars as text is equal to "\" then
    set item i of chars to "\\"
    end if
    end repeat
    return every item of chars as string
    end posix_path

    on runCompression(mypath, dirpath, newPath, ext)
    tell application "Terminal"
    do shell script "java -jar " & dirpath & "yuicompressor-2.4.2.jar " & mypath & " -o " & newPath & ".min." & ext
    end tell
    end runCompression [/code]

  • Sasasa

    good…..

close