# Syntax: AppleScript

## Hello World & Mode-line Support

    (*
    Copyright (C) 2009 Orion Transfer Ltd. All Rights Reserved. See http://www.oriontransfer.co.nz/ for more information.
    Drop files on top of this application to produce a zip archive on the desktop containing compressed JPEGs at a maximum size of 800px.
    *)

    on open some_items
    	-- If we drop a single folder onto the icon, we pull the name from the folder name
    	set target_name to "Images"

    	if (count of some_items) = 1 then
    		set first_item to get item 1 of some_items

    		tell application "System Events"
    			set target_item to disk item (first_item as string)

    			if class of target_item is folder then
    				set target_name to name of first_item
    			end if
    		end tell
    	end if

    	display dialog "Name of Archive" default answer target_name buttons {"OK"} default button 1
    	set target_name to text returned of the result

    	tell application "Finder"
    		set desktop_path to folder "Desktop" of home

    		make new folder at desktop_path with properties {name:target_name}

    		set destination to (folder target_name of desktop_path) as alias
    		set archive_path to (POSIX path of (desktop_path as text)) & target_name & ".zip"
    	end tell

    	repeat with this_item in some_items
    		tell application "System Events"
    			set source_item to disk item (this_item as string)
    		end tell

    		process_path(source_item, destination)
    	end repeat

    	set archive_command to "/usr/bin/ditto -c -k -rsrc " & quoted form of (POSIX path of destination) & " " & quoted form of archive_path

    	do shell script archive_command

    	tell application "Finder"
    		activate

    		delete folder target_name of desktop_path

    		reveal file (target_name & ".zip") of desktop_path
    	end tell
    end open

    to process_path(source, destination)
    	-- Figure out if it is a file or folder, and recurse appropriately
    	tell application "System Events"
    		set is_folder to class of source is folder
    		if is_folder then
    			set sub_items to items in source
    		end if
    	end tell

    	if is_folder then
    		repeat with this_item in sub_items
    			process_path(this_item, destination)
    		end repeat
    	else
    		tell application "System Events"
    			set source_alias to (path of source) as alias
    		end tell

    		resize_and_save(source_alias, destination)
    	end if
    end process_path

    to resize_and_save(image_alias, destination_alias)
    	tell application "Image Events"
    		launch

    		set opened_image to open image_alias

    		if class of opened_image is image and not dimensions of opened_image = {} then
    			set dim to dimensions of opened_image

    			-- We only resize if the dimension is bigger than 800
    			if (first item of dim > 800 or second item of dim > 800) then
    				scale opened_image to size 800
    			end if

    			save opened_image in destination_alias as JPEG with compression level medium

    			close opened_image
    		end if
    	end tell
    end resize_and_save