Last active
August 29, 2015 14:19
-
-
Save agassiyzh/d00f6938cf5363709151 to your computer and use it in GitHub Desktop.
Auto scale image for iOS develop folder action
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
property done_foldername : "Processed Images" | |
property original_foldername : "Original Images" | |
property error_foldername : "Error Images" | |
property error_list : {} | |
property type_list : {"JPEG", "TIFF", "GIFf", "PICT", "8BIM", "PNGf"} | |
property extension_list : {"jpg", "jpeg", "tif", "tiff", "gif", "pct", "pict", "psd", "png"} | |
on adding folder items to this_folder after receiving these_items | |
set error_list to {} | |
tell application "Finder" | |
if not (exists folder done_foldername of this_folder) then | |
make new folder at this_folder with properties {name:done_foldername} | |
end if | |
if not (exists folder original_foldername of this_folder) then | |
make new folder at this_folder with properties {name:original_foldername} | |
end if | |
if not (exists folder error_foldername of this_folder) then | |
make new folder at this_folder with properties {name:error_foldername} | |
end if | |
end tell | |
try | |
repeat with i from 1 to number of items in these_items | |
set this_item to item i of these_items | |
set the item_info to the info for this_item | |
if (alias of the item_info is false and the file type of the item_info is in the type_list) or (the name extension of the item_info is in the extension_list) then | |
process_item(this_item, this_folder) | |
end if | |
end repeat | |
if error_list is {} then | |
display notification "Image Scale Successful!" with title "Done" | |
else | |
display dialog joinList(error_list, ", ") with title "Image size error" | |
end if | |
on error error_message number error_number | |
display dialog error_message | |
end try | |
end adding folder items to | |
on process_item(this_item, this_folder) | |
tell application "Finder" | |
set the result_folder to (folder done_foldername of this_folder) as alias | |
set the original_folder to (folder original_foldername of this_folder) as alias | |
set the error_folder to (folder error_foldername of this_folder) as alias | |
end tell | |
tell application "Image Events" | |
launch | |
set this_image to open this_item | |
copy dimensions of this_image to {W, H} | |
if W is less than H then | |
set the scale_length to H | |
else | |
set the scale_length to W | |
end if | |
set file_name to my get_image_name(this_item) | |
if W mod 6 is not 0 or H mod 6 is not 0 then | |
move this_item to error_folder | |
set error_list to error_list & file_name | |
else | |
set fileExtension to name extension of this_item | |
scale this_image to size scale_length / 2 | |
set new_file_name to file_name & "@3x." & fileExtension | |
set new_file_path to (result_folder as string) & new_file_name | |
save this_image in new_file_path | |
scale this_image to size scale_length / 3 | |
set new_file_name to file_name & "@2x." & fileExtension | |
set new_file_path to (result_folder as string) & new_file_name | |
save this_image in new_file_path | |
move this_item to original_folder | |
end if | |
close this_image | |
end tell | |
end process_item | |
on get_image_name(this_item) | |
tell application "Finder" | |
set fileName to name of this_item | |
set sortName to text 1 thru ((offset of "." in fileName) - 1) of fileName | |
if sortName ends with "@6x" then | |
set sortName to text 1 thru ((offset of "@" in sortName) - 1) of sortName | |
end if | |
end tell | |
return sortName | |
end get_image_name | |
to joinList(aList, delimiter) | |
set retVal to "" | |
set prevDelimiter to AppleScript's text item delimiters | |
set AppleScript's text item delimiters to delimiter | |
set retVal to aList as string | |
set AppleScript's text item delimiters to prevDelimiter | |
return retVal | |
end joinList |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment