Want to read Slashdot from your mobile device? Point it at m.slashdot.org and keep reading!

 



Forgot your password?
typodupeerror
User Journal

jcburns's Journal: Applescript to make thumbnails and javascript image swaps

Journal by jcburns

I've added to and expanded the Diane Kirkland website (http://dianekirklandphoto.com/)...she's really done some nice images. So, in order to make all this work, I had to learn some javascript that most people would find routine...and then, because I made thumbnails by hand for two of the pages, I got tired of that and cobbled together this applescript (a lot of chunks from Apple's site):

property type_list : {"TIFF", "JPEG", "PNGf", "PICT"}
property extension_list : {"tif", "tiff", "jpg", "jpeg", "png", "pict", "pct"}
set thehtml to ""
set thehtml2 to ""
 
tell application "Finder"
    activate
    try
        set the source_folder to choose folder with prompt "Pick a folder containing the images to process:"
        set these_files to every file of the source_folder whose file type is in the type_list or name extension is in the extension_list
        repeat with i from 1 to the count of these_files
            set this_path to (item i of these_files) as string
            tell application "Image Events"
                set this_image to open file this_path
                set theWidth to 100
                copy dimensions of this_image to {currentWidth, currentHeight}
                if currentWidth is greater than currentHeight then
                    -- image is wider than high so scale width
                    scale this_image to size theWidth
                else
                    -- image is taller than wide so scale height
                    set the theHeight to (currentHeight * theWidth) / currentWidth
                    scale this_image to size theHeight
                end if
                set new_name to my change_name(alias (this_path))
                set the new_image to save this_image as JPEG in file new_name of the source_folder
                set thehtml to thehtml & "img[" & (i - 1) & "] = new Image(); img[" & (i - 1) & "].src = " & quote & name of source_folder & "/" & the name of this_image & quote & ";" & return
                set thehtml2 to thehtml2 & "<a href=\"javascript:imageSwap(" & (i - 1) & ")\"><img class=\"imagethumb\" src=\"" & name of source_folder & "/" & new_name & "\" ></a>" & return
                close this_image
            end tell
        end repeat
        set the clipboard to thehtml & return & return & thehtml2
    on error error_message
        display dialog error_message buttons {"OK"} default button 1
    end try
end tell
 
on change_name(this_file)
    set this_info to the info for this_file
    set this_name to the name of this_info
    set this_extension to the name extension of this_info
    if this_extension is missing value then
        set the default_name to this_name
    else
        set the default_name to text 1 thru -((length of this_extension) + 2) of this_name
    end if
    return (the default_name & "_th." & the this_extension)
end change_name

And what's really nice about this for me is that I got it to put on the clipboard after making the thumbnails the chunks of the HTML/javascript I needed:

<script type="text/javascript">
var img = new Array();
img[0] = new Image(); img[0].src = "bw/Blueridge-17.jpg";
img[1] = new Image(); img[1].src = "bw/Blueridge-23.jpg";
img[2] = new Image(); img[2].src = "bw/Blueridge-31.jpg";
img[3] = new Image(); img[3].src = "bw/Coast-25.jpg";
img[4] = new Image(); img[4].src = "bw/Coast-34.jpg";
img[5] = new Image(); img[5].src = "bw/Piedmont-19.jpg";
img[6] = new Image(); img[6].src = "bw/Plains-27.jpg";
 
function imageSwap(x)
{
 
var imageObj = document.getElementById("imgbig");
imageObj.setAttribute("src", img[x].src);
 
}
</script>
 
<a href="javascript:imageSwap(0)"><img class="imagethumb" src="bw/Blueridge-17_th.jpg" ></a>
<a href="javascript:imageSwap(1)"><img class="imagethumb" src="bw/Blueridge-23_th.jpg" ></a>
<a href="javascript:imageSwap(2)"><img class="imagethumb" src="bw/Blueridge-31_th.jpg" ></a>
<a href="javascript:imageSwap(3)"><img class="imagethumb" src="bw/Coast-25_th.jpg" ></a>
<a href="javascript:imageSwap(4)"><img class="imagethumb" src="bw/Coast-34_th.jpg" ></a>
<a href="javascript:imageSwap(5)"><img class="imagethumb" src="bw/Piedmont-19_th.jpg" ></a>
<a href="javascript:imageSwap(6)"><img class="imagethumb" src="bw/Plains-27_th.jpg" ></a>

...so that I didn't have to cut and paste and increment a zillion times. Heh!

This discussion has been archived. No new comments can be posted.

Applescript to make thumbnails and javascript image swaps

Comments Filter:

Wernher von Braun settled for a V-2 when he coulda had a V-8.

Working...