Applescript resize 1 image to 10 + run terminal on folder

Hello,


I made an Automator script that i can drop a 1024x1024 png on that then makes a "New-Icon.iconset" folder on the desktop and then resizes the big png to:

icon_512x512@2x.png (1024x1024)

icon_512x512.png

icon_256x256@2x.png (128x128)

icon_256x256.png

icon_128x128@2x.png (256x256)

icon_128x128.png

icon_32x32@2x.png (64x64)

icon_32x32.png

icon_16x16@2x.png (32x32)

icon_16x16.png

Then runs the terminal with "iconutil -c icns ~/Desktop/New-Icon.iconset" to make the new-icon.icns and finally deletes the "New-Icon.iconset" folder leaving just the new icon file.


The automator script is over 2MB so now to my question....


How to set up an Applescript that does the above?


Best regards

Posted on Jul 27, 2015 6:49 PM

Reply
4 replies

Jul 27, 2015 9:48 PM in response to Jakerlund

This is pretty straightforward. Hopefully the following script is self-explanatory:


global destFolder


set theImage to (choose file with prompt "Select an image to iconize:")

tell application "Finder"

if exists folder "New-Icon.iconset" of desktop then

display dialog "A New-Icon.iconset folder already exists" buttons {"Cancel", "Overwrite"} default button "Cancel"

delete folder "New-Icon.iconset" of desktop

end if

set destFolder to (make new folder at desktop with properties {name:"New-Icon.iconset"}) as text

end tell


set newImage to my ImageResize(theImage, 1024, "icon_512x512@2x.png")

set newImage to my ImageResize(theImage, 512, "icon_512x512.png")

set newImage to my ImageResize(theImage, 512, "icon_256x256@2x.png")

set newImage to my ImageResize(theImage, 256, "icon_256x256.png")

set newImage to my ImageResize(theImage, 256, "icon_128x128@2x.png")

set newImage to my ImageResize(theImage, 128, "icon_128x128.png")

set newImage to my ImageResize(theImage, 64, "icon_32x32@2x.png")

set newImage to my ImageResize(theImage, 32, "icon_32x32.png")

set newImage to my ImageResize(theImage, 32, "icon_16x16@2x.png")

set newImage to my ImageResize(theImage, 16, "icon_16x16.png")


-- copy the above lines as many times as you need

-- just make sure you pass in the image, the largest dimension and target file name


do shell script "/usr/bin/iconutil -c icns " & (quoted form of text 1 through -2 of POSIX path of destFolder)


tell application "Finder" to delete destFolder

on ImageResize(img, newSize, name)

tell application "Image Events"

set sourceImage to openimg


scalesourceImageto sizenewSize


savesourceImageinfile ((destFolder as text) & name)

end tell

end ImageResize


I do get an error when running the iconutil process, though, but I don't know the specifics of that command to know if it's a problem with the images - as far as I can tell this creates the images as expected/wanted.

Jul 28, 2015 10:55 AM in response to Jakerlund

How do i also make it run by just drop an image onto the application?


To respond to dropped files, just wrap the main body of code in an 'on open' handler:


global destFolder


on open of theFiles


-- Executed when files are dropped on the script



-- iterate through each dropped file


-- (note there are no checks that the dropped file is an image)

repeat with theImage in theFiles


tell application "Finder"

if exists folder "New-Icon.iconset" of desktop then

display dialog "A New-Icon.iconset folder already exists" buttons {"Cancel", "Overwrite"} default button "Cancel"

delete folder "New-Icon.iconset" of desktop

end if

set destFolder to (make new folder at desktop with properties {name:"New-Icon.iconset"}) as text

end tell


set newImage to my ImageResize(theImage, 1024, "icon_512x512@2x.png")

set newImage to my ImageResize(theImage, 512, "icon_512x512.png")

set newImage to my ImageResize(theImage, 512, "icon_256x256@2x.png")

set newImage to my ImageResize(theImage, 256, "icon_256x256.png")

set newImage to my ImageResize(theImage, 256, "icon_128x128@2x.png")

set newImage to my ImageResize(theImage, 128, "icon_128x128.png")

set newImage to my ImageResize(theImage, 64, "icon_32x32@2x.png")

set newImage to my ImageResize(theImage, 32, "icon_32x32.png")

set newImage to my ImageResize(theImage, 32, "icon_16x16@2x.png")

set newImage to my ImageResize(theImage, 16, "icon_16x16.png")



-- copy the above lines as many times as you need


-- just make sure you pass in the image, the largest dimension and target file name


do shell script "/usr/bin/iconutil -c icns " & (quoted form of text 1 through -2 of POSIX path of destFolder)


tell application "Finder" to delete destFolder


end repeat

end open


on ImageResize(img, newSize, name)

tell application "Image Events"

set sourceImage to openimg


scalesourceImageto sizenewSize


savesourceImageinfile ((destFolder as text) & name)

end tell

end ImageResize


Note that this will respond to multiple dropped files, but because you always save the output in the same file, you might have trouble getting the .icns for all but the last (technically, the other files will be created, then deleted before the next file is processed). This may or may not be a problem for you.


I don't get any errors, it does everything perfect.

That's probably something to do with the image I used for testing, but as long as it works for you, I'm happy 🙂

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Applescript resize 1 image to 10 + run terminal on folder

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.