AppleScript: Problem Saving Files in the correct place

Discussion in 'Mac Programming' started by mrnohands2004, Mar 2, 2013.

  1. macrumors newbie

    Joined:
    Feb 23, 2013
    #1
    Hi all,
    I am writing a script for work and I'm having some difficulties in saving the files to the correct place. The script function is to basically operate as a droplet and open a series of files in Photoshop and then perform sizing operations upon them. That bit is fine but the bit i'm having a problem with is the saving and folder creation. Before I explain here is my code so far.
    Code:
    --Start Droplet
    on open these_items
    	--set location as a variable
    	set the_location to "Macintosh HD:Users:tomfinnane:Desktop:222222:"
    	--create folder structure
    	tell application "Finder"
    		if not (exists folder "HoFOut" of folder the_location) then
    			make new folder at the_location with properties {name:"HoFOut"}
    		end if
    		if not (exists folder "DEBOut" of folder the_location) then
    			make new folder at the_location with properties {name:"DEBOut"}
    		end if
    		if not (exists folder "PEOut" of folder the_location) then
    			make new folder at the_location with properties {name:"PEOut"}
    		end if
    		set archive_folder1 to the_location & "PEOut:"
    		if not (exists folder "Mannequins" of folder archive_folder1) then
    			set first_folder to make new folder at archive_folder1 with properties {name:"Mannequins"}
    		end if
    		if not (exists folder "Models" of folder archive_folder1) then
    			set second_folder to make new folder at archive_folder1 with properties {name:"Models"}
    		end if
    	end tell
    	--start loop to open multiple files
    	repeat with i from 1 to the count of these_items
    		set this_item to item i of these_items as string
    		set this_item_string to this_item as string
    		set the_item_name to do shell script "sed -E 's|:$||;s|.*:||;s|\\.[^.]*$||' <<< " & quoted form of this_item_string
    		set the_sku to text 1 thru 9 of the_item_name
    		tell application "Finder"
    			if the_item_name contains "FG" then
    				set archive_folder2 to the_location & "PEOut:Mannequins:"
    				if not (exists folder the_sku of folder archive_folder2) then
    					set third_folder to make new folder at archive_folder2 with properties {name:the_sku}
    				end if
    				set archive_folder3 to the_location & "PEOut:Mannequins:" & the_sku
    				if not (exists folder "Large" of folder archive_folder3) then
    					set fourth_folder to make new folder at archive_folder3 with properties {name:"Large"}
    				end if
    				if not (exists folder "Medium" of folder archive_folder3) then
    					set fifth_folder to make new folder at archive_folder3 with properties {name:"Medium"}
    				end if
    				if not (exists folder "Small" of folder archive_folder3) then
    					set sixth_folder to make new folder at archive_folder3 with properties {name:"Small"}
    				end if
    				if not (exists folder "Square" of folder archive_folder3) then
    					set seventh_folder to make new folder at archive_folder3 with properties {name:"Square"}
    				end if
    				if not (exists folder "Swatch" of folder archive_folder3) then
    					set eight_folder to make new folder at archive_folder3 with properties {name:"Swatch"}
    				end if
    				if not (exists folder "Thumb" of folder archive_folder3) then
    					set ninth_folder to make new folder at archive_folder3 with properties {name:"Thumb"}
    				end if
    				if not (exists folder "X-Large" of folder archive_folder3) then
    					set tenth_folder to make new folder at archive_folder3 with properties {name:"X-Large"}
    				end if
    				if not (exists folder "Zoom" of folder archive_folder3) then
    					set eleventh_folder to make new folder at archive_folder3 with properties {name:"Zoom"}
    				end if
    			end if
    		end tell
    		my HoF(the_item_name, this_item, 1500, 2000, 300, the_location & "HoFOut:" & the_sku & ".jpg")
    		my DEB(the_item_name, this_item, 2000, 300, the_location & "DEBOut:" & "54510_" & the_sku & ".jpg")
    		my Large(the_item_name, this_item, 161, 201, 300, the_location & "PEOut:Mannequins:" & the_sku & "_1.png")
    	end repeat
    end open
    
    --start algorithms
    on HoF(the_name, the_file, the_width, the_height, the_res, the_fileplace)
    	tell application "Finder"
    		if the_name contains "FG" then
    			tell application "Adobe Photoshop CS4"
    				activate
    				open alias the_file as TIFF
    				set ruler units of settings to pixel units
    				set the_document to current document
    				tell the_document
    					delete (every path item)
    					do javascript "function deleteGuides()
            {
            var allGuides = new ActionDescriptor();
            var guidesRef = new ActionReference();
                guidesRef.putEnumerated(charIDToTypeID(\"Gd  \"), charIDToTypeID(\"Ordn\"), charIDToTypeID(\"Al  \"));
                allGuides.putReference(charIDToTypeID(\"null\"), guidesRef);
            executeAction(charIDToTypeID(\"Dlt \"), allGuides, DialogModes.NO);
            }
            deleteGuides();"
    					delete (every channel whose kind is not component channel)
    					flatten
    					resize image height the_height resolution the_res resample method bicubic
    					resize canvas width the_width anchor position middle center
    				end tell
    				set save_doc to save the_document in file the_fileplace as JPEG with options {class:JPEG save options, embed color profile:false, quality:12}
    				close save_doc
    			end tell
    		end if
    	end tell
    end HoF
    
    on DEB(the_name, the_file, the_size, the_res, the_fileplace)
    	tell application "Finder"
    		if the_name contains "FG" then
    			tell application "Adobe Photoshop CS4"
    				activate
    				open alias the_file as TIFF
    				set ruler units of settings to pixel units
    				set the_document to current document
    				tell the_document
    					delete (every path item)
    					do javascript "function deleteGuides()
            {
            var allGuides = new ActionDescriptor();
            var guidesRef = new ActionReference();
                guidesRef.putEnumerated(charIDToTypeID(\"Gd  \"), charIDToTypeID(\"Ordn\"), charIDToTypeID(\"Al  \"));
                allGuides.putReference(charIDToTypeID(\"null\"), guidesRef);
            executeAction(charIDToTypeID(\"Dlt \"), allGuides, DialogModes.NO);
            }
            deleteGuides();"
    					delete (every channel whose kind is not component channel)
    					trim basing trim on transparent pixels
    					set trim_width to (width of the_document)
    					set trim_height to (height of the_document)
    					if trim_width > trim_height then
    						resize canvas width trim_width height trim_width anchor position middle center
    					else
    						resize canvas width trim_height height trim_height anchor position middle center
    					end if
    					resize image width the_size height the_size resolution the_res resample method bicubic
    					flatten
    				end tell
    				set save_doc to save the_document in file the_fileplace as JPEG with options {class:JPEG save options, embed color profile:false, quality:12}
    				close save_doc
    			end tell
    		end if
    	end tell
    end DEB
    
    on Large(the_name, the_file, the_width, the_height, the_res, the_fileplace)
    	tell application "Finder"
    		if the_name contains "FG" then
    			tell application "Adobe Photoshop CS4"
    				activate
    				open alias the_file as TIFF
    				set ruler units of settings to pixel units
    				set the_document to current document
    				tell the_document
    					delete (every path item)
    					do javascript "function deleteGuides()
            {
            var allGuides = new ActionDescriptor();
            var guidesRef = new ActionReference();
                guidesRef.putEnumerated(charIDToTypeID(\"Gd  \"), charIDToTypeID(\"Ordn\"), charIDToTypeID(\"Al  \"));
                allGuides.putReference(charIDToTypeID(\"null\"), guidesRef);
            executeAction(charIDToTypeID(\"Dlt \"), allGuides, DialogModes.NO);
            }
            deleteGuides();"
    					delete (every channel whose kind is not component channel)
    					make new art layer
    					merge visible layers
    					trim basing trim on transparent pixels
    					resize image height the_height resolution the_res resample method bicubic
    					resize canvas width the_width anchor position middle center
    				end tell
    				set the_savedoc to save the_document in file the_fileplace as PNG with options {class:PNG save options, interlaced:false}
    				close the_savedoc
    			end tell
    		end if
    	end tell
    end Large
    As you can see I've created a folder on my desktop called "222222" to save the files to. Inside this folder the script will create three folders "DEBOut", "HofOut" and "PEOut". Inside "PEOut" two more folders are created called "Mannequins" and "Models". Now this is where it gets complex. Each file has a unique 9 digit number called a "SKU Number". The files are grouped into three's by this SKU Number and each file is identified by an "_FG", "_DS" or an "_CS". eg:
    "123456789_FG.tif"
    "123456789_DS.tif"
    "123456789_CS.tif"
    My script takes this SKU number and creates a folder within "Mannequins" named after the SKU. Inside the SKU named folder 8 more folders are created named "Large", "Medium", "Small", "Square", "Swatch", "Thumb", "X-Large" and "Zoom". Now we get to the crux of the problem. Once I perform a sizing operation on a file, let's say Large, I can't get it to save to the correct folder simply because the SKU named folder's name changes and I don't know what to put in my handler description as the save path. I've written a few scripts for my company before and have found handlers a solid way to do so. Our clients constantly change file sizes and this is the easiest way to change that information without breaking the script so I would like to keep the handlers if possible. Please understand I am a Photo Retoucher and Scripting is only a hobby so please be gentle. Any help would be greatly appreciated.

    Thanks

    Kavin
     

Share This Page