I wrote this script to convert a png into a icns. The script performs two operations. First it re-samples the png into the highest possible expected size, padding the smaller of the two dimensions in white to create a square. Second, it converts the png format into an icns format. I wanted to know if it was possible to combine the two functions in one sips command.
do shell script "sips " & aFile & " -Z " & W & " -p " & W & space & W & " --padColor FFFFFF -i -s format icns --out " & quoted form of newPath
seems to work but the resulting file is a different size as the one from the two-step script below. Am I missing something?
property expectedSizes : {16, 32, 48, 128, 256, 512, 1024, 9999999}
set myFiles to choose file with multiple selections allowed
repeat with aFile in myFiles
tell application "System Events" to set bPath to POSIX path of (container of aFile)
set newPath to bPath & "/" & bName(aFile) & ".icns" as text
set aFile to quoted form of (POSIX path of aFile)
set {W, H} to {paragraph 1, paragraph 2} of (do shell script "sips -g pixelWidth -g pixelHeight " & aFile & " | grep -Eo [0-9]*$")
set {W, H} to {W as number, H as number}
if W > H then
set W to eSize(W)
do shell script "sips " & aFile & " -Z " & W & " -p " & W & space & W & " --padColor FFFFFF -i"
delay 1
else if H > W then
set H to eSize(H)
do shell script "sips " & aFile & " -Z " & H & " -p " & H & space & H & " --padColor FFFFFF -i"
delay 1
-- H = W but not in expected sizes
else if H is not in expectedSizes then
set H to eSize(H)
do shell script "sips " & aFile & " -Z " & H & " -p " & H & space & H & " --padColor FFFFFF -i"
delay 1
end if
do shell script "sips -s format icns " & aFile & " --out " & quoted form of newPath
end repeat
on bName(theFile)
tell application "Finder" to set {name:fileName, name extension:nameExtension} to theFile
set baseName to text 1 thru ((get offset of "." & nameExtension in fileName) - 1) of fileName
end bName
on eSize(lDimen)
repeat with i from 1 to 8
if lDimen < item i of expectedSizes then return item (i - 1) of expectedSizes
end repeat
end eSize
2 Cents: The script below re-samples the png into the highest possible expected size [i]based on the largest side of the png, whether it be height or width.
That way I you always scale the image downwards, never upwards, if you got what I mean. But maybe the Width is the largest size of your png? That is the only thing I can see here. That maybe you haven’t scaled with the largest side as your “pivot”.
Correct me if I am wrong. I think the best way is to scale the largest side down to the closest expected size and then pad the smaller side with white to make the square.
I believe it should work if either the width or the height is larger because it always uses the larger side to scale to the expected size. Again, correct me If I am wrong. My question is if it is possible to combine the scaling function with the format function in one sips command.
do shell script "sips " & aFile & " -Z " & W & " -p " & W & space & W & " --padColor FFFFFF -i -s format icns --out " & quoted form of newPath
However, it produces a file that is a different size than the method used in the lower script. Is there a way to verify that the icns file is formatted properly? For example, you can simply change the file name from .png to .icns and kind will display Apple icon image.
Apple has a whole document covering icons, I think the basics is that the icns file, contains icnons of different resolutions, expressed by sizes. That is, the icns file contains different sizes. That is a valid icon file by Apple’s Standards.
Otherwise, I think the size of the icon is the factor , besides the format that will render an icon valid or invalid.
You should skim Apple Human Interface guidelines, at least that document will have a pointer towards icon creation.
The resampling and padding command does not include an --out and the original png is overwritten.
do shell script "sips " & aFile & " -Z " & H & " -p " & H & space & H & " --padColor FFFFFF -i"
The format command includes the out to the new .icns file.
do shell script "sips -s format icns " & aFile & " --out " & quoted form of newPath
Ideally, I would like to combine these commands, which I may have with:
do shell script "sips " & aFile & " -Z " & W & " -p " & W & space & W & " --padColor FFFFFF -i -s format icns --out " & quoted form of newPath
However, the differing file sizes make me think something is wrong with the combined command. I would like to verify if the resulting file is indeed in the proper .icns format or if it simply changed the name extension.
If anyone has a better idea of how to combine the commands, I am game. I was considering piping one to another, but it seems that would produce the same output as the original script.
With my text png, the resolution of the combined-command script result is exactly the same (according to Finder information windows) as that of the other script, but the file has more bytes and isn’t credited with a colour profile. I’m not exactly sure why, but I can get the combined-command script to produce an identical result to the other (again according to the information windows) if I move the icns conversion up to the front:
do shell script "sips -s format icns " & aFile & " -Z " & W & " -p " & W & space & W & " --padColor FFFFFF -i --out " & quoted form of newPath