アイコン作成 v3

Finder上で選択したアスペクト比1:1のPNG画像からアイコンファイルを作成するAppleScriptです。

on main(myFiles)

set IconImage_size to {"16x16", "16x16@2x", "32x32", "32x32@2x", "128x128", "128x128@2x", "256x256", "256x256@2x", "512x512", "512x512@2x"}

repeat with myFile in myFiles
set aFile to contents of myFile
set fileName to file_NE(aFile, 1)
set fileExtension to file_NE(aFile, 2)
tell application "Finder" to set exportPath to (container of aFile) as alias

if fileExtension is equal to "png" then
set aspectRatio to checkAspectRatio(aFile) of me
if aspectRatio is 1.0 then
tell application "Finder"
tell folder exportPath
if not (exists file (fileName & ".icns")) then
makeIcon(aFile, fileName, IconImage_size, 2, exportPath) of me
else
beep
display dialog "同名のファイルが存在します。処理をスキップします。" with title (fileName & ".icns")
end if
end tell
end tell
else
beep
display dialog "アスペクト比 1:1 の画像を処理して下さい" with title (fileName & "." & fileExtension)
end if
else
beep
display dialog "PNG画像を処理して下さい" with title (fileName & "." & fileExtension)
end if
end repeat
end main

on makeIcon(aFile, fileName, IconImage_size, keyNum, exportPath)
set IconSize_list to {} --ハンドラで取得したアイコンサイズを格納するリスト。
set IconName_list to {} --作成したアイコンのファイル名を格納するリスト。
repeat with culItem in IconImage_size
set end of IconSize_list to ((as_size(culItem)) of me)
set end of IconName_list to ("icon_" & culItem & ".png")
end repeat
set folderPath to POSIX path of (exportPath & fileName & ".iconset:" as text)
tell application "Finder"
try
make new folder at exportPath with properties {name:fileName & ".iconset"}
on error msg
beep
display dialog msg with title (fileName & ".iconset")
return
end try
end tell
do shell script "cp -i " & (quoted form of (POSIX path of aFile)) & space & ¬
(quoted form of (folderPath & "cp_itm.png"))
set aRes to retExifAttributeData(aFile, "pixelWidth") of me as number
set bRes to retExifAttributeData(aFile, "pixelHeight") of me as number
set {aWidth, aHeight} to {aRes, bRes} -->{"1024", "1024"} 横縦サイズ
set NumOfTimes to (checkSize(aWidth, aHeight)) of me
repeat with i from 1 to NumOfTimes --アイコンにするために各サイズの画像を生成。
do shell script "cd " & (quoted form of folderPath) & "; sips --resampleWidth " & (IconSize_list's item i) & " --out " & (IconName_list's item i) & " cp_itm.png"
end repeat
do shell script "rm -f " & quoted form of (folderPath & "cp_itm.png")
if keyNum is 1 then
do shell script "iconutil -c icns " & (quoted form of folderPath) & "; sleep 1; rm -r " & (quoted form of folderPath)
else if keyNum is 2 then
do shell script "iconutil -c icns " & (quoted form of folderPath)
end if
end makeIcon

--アイコンサイズを取得するデリミター
on as_size(theString)
set tmpTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to "x" --"x"で区切る。
set tmpStr to theString's text items --テキストアイテムに。
set num to ("-" & (count tmpStr) as number) as text --末尾からどこまでを抹消するか。
set theString to (theString's text items 1 thru num)
set AppleScript's text item delimiters to tmpTID
if (tmpStr as string) contains "@2" then set theString to (theString * 2)
return theString as number
end as_size

--SIPSコマンドで指定のデジカメ画像のExif情報(プロパティ)を取得する
on retExifAttributeData(aFile, aParam)

set aPosix to quoted form of POSIX path of aFile

set aRes to do shell script "sips --getProperty " & aParam & " " & aPosix
set aList to paragraphs of aRes
set anItem to contents of last item of aList

set colonPos to (offset of ":" in anItem) + 2
set eRes to text colonPos thru -1 of anItem

return eRes

end retExifAttributeData

on file_NE(aFile, keyNum)
tell application "Finder"
set fileName to (aFile's name)
set fileExtension to (aFile's name extension)
if fileExtension is not "" then
set culNum to (count fileName's text item) - (count fileExtension's text item) - 1
set fileName to (fileName's text items 1 thru culNum) as text
end if
if keyNum is 1 then return fileName
if keyNum is 2 then return fileExtension
end tell
end file_NE


--生成するイメージの数を返す。
on checkSize(aWidth, aHeight)
if aWidthaHeight then set the_long_side to aWidth
if aWidth < aHeight then set the_long_side to aHeight
if the_long_side ≥ 16 and the_long_side < 32 then set n to 1
if the_long_side ≥ 32 and the_long_side < 64 then set n to 3
if the_long_side ≥ 64 and the_long_side < 128 then set n to 4
if the_long_side ≥ 128 and the_long_side < 256 then set n to 5
if the_long_side ≥ 256 and the_long_side < 512 then set n to 7
if the_long_side ≥ 512 and the_long_side < 1024 then set n to 9
if the_long_side ≥ 1024 then set n to 10
return n
end checkSize

--画像の縦横比を判定
on checkAspectRatio(aFile)
set aRes to retExifAttributeData(aFile, "pixelWidth") of me as number
set bRes to retExifAttributeData(aFile, "pixelHeight") of me as number
return (aRes / bRes)
end checkAspectRatio

on run
tell application "Finder"
set sel to selection as alias list
if sel is not {} then
my main(sel)
end if
end tell
end run

アイコン作成 v4

Finder上で選択したPNG画像からアイコンファイルを作成するAppleScriptです。

on main(myFiles)

set IconImage_size to {"16x16", "16x16@2x", "32x32", "32x32@2x", "128x128", "128x128@2x", "256x256", "256x256@2x", "512x512", "512x512@2x"}

repeat with myFile in myFiles
set aFile to contents of myFile
set fileName to file_NE(aFile, 1)
set fileExtension to file_NE(aFile, 2)

tell application "Finder" to set exportPath to (container of aFile) as alias

if fileExtension is equal to "png" then
set aspectRatio to checkAspectRatio(aFile) of me
if aspectRatio is 1.0 then
tell application "Finder"
tell folder exportPath
if not (exists file (fileName & ".icns")) then
makeIcon(aFile, fileName, IconImage_size, 2, exportPath) of me
else
beep
display dialog "同名のファイルが存在します。処理をスキップします。" with title (fileName & ".icns")
end if
end tell
end tell
else
tell application "Finder"
tell folder exportPath
if not (exists file (fileName & ".icns")) then
set fp to cropImageAndReturnPath(aFile, exportPath) of me
makeIcon(fp, fileName, IconImage_size, 2, exportPath) of me
else
beep
display dialog "同名のファイルが存在します。処理をスキップします。" with title (fileName & ".icns")
end if
end tell
end tell
end if
else
beep
display dialog "PNG画像を処理して下さい" with title (fileName & "." & fileExtension)
end if
end repeat
end main

on makeIcon(aFile, fileName, IconImage_size, keyNum, exportPath)
set IconSize_list to {} --ハンドラで取得したアイコンサイズを格納するリスト。
set IconName_list to {} --作成したアイコンのファイル名を格納するリスト。
repeat with culItem in IconImage_size
set end of IconSize_list to ((as_size(culItem)) of me)
set end of IconName_list to ("icon_" & culItem & ".png")
end repeat
set folderPath to POSIX path of (exportPath & fileName & ".iconset:" as text)
tell application "Finder"
try
make new folder at exportPath with properties {name:fileName & ".iconset"}
on error msg
beep
display dialog msg with title (fileName & ".iconset")
return
end try
end tell
do shell script "cp -i " & (quoted form of (POSIX path of aFile)) & space & ¬
(quoted form of (folderPath & "cp_itm.png"))
set aRes to retExifAttributeData(aFile, "pixelWidth") of me as number
set bRes to retExifAttributeData(aFile, "pixelHeight") of me as number
set {aWidth, aHeight} to {aRes, bRes} -->{"1024", "1024"} 横縦サイズ
set NumOfTimes to (checkSize(aWidth, aHeight)) of me
repeat with i from 1 to NumOfTimes --アイコンにするために各サイズの画像を生成。
do shell script "cd " & (quoted form of folderPath) & "; sips --resampleWidth " & (IconSize_list's item i) & " --out " & (IconName_list's item i) & " cp_itm.png"
end repeat
do shell script "rm -f " & quoted form of (folderPath & "cp_itm.png")
if keyNum is 1 then
do shell script "iconutil -c icns " & (quoted form of folderPath) & "; sleep 1; rm -r " & (quoted form of folderPath)
else if keyNum is 2 then
do shell script "iconutil -c icns " & (quoted form of folderPath)
end if
end makeIcon

--アイコンサイズを取得するデリミター
on as_size(theString)
set tmpTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to "x" --"x"で区切る。
set tmpStr to theString's text items --テキストアイテムに。
set num to ("-" & (count tmpStr) as number) as text --末尾からどこまでを抹消するか。
set theString to (theString's text items 1 thru num)
set AppleScript's text item delimiters to tmpTID
if (tmpStr as string) contains "@2" then set theString to (theString * 2)
return theString as number
end as_size

--SIPSコマンドで指定のデジカメ画像のExif情報(プロパティ)を取得する
on retExifAttributeData(aFile, aParam)

set aPosix to quoted form of POSIX path of aFile

set aRes to do shell script "sips --getProperty " & aParam & " " & aPosix
set aList to paragraphs of aRes
set anItem to contents of last item of aList

set colonPos to (offset of ":" in anItem) + 2
set eRes to text colonPos thru -1 of anItem

return eRes

end retExifAttributeData

on file_NE(aFile, keyNum)
tell application "Finder"
set fileName to (aFile's name)
set fileExtension to (aFile's name extension)
if fileExtension is not "" then
set culNum to (count fileName's text item) - (count fileExtension's text item) - 1
set fileName to (fileName's text items 1 thru culNum) as text
end if
if keyNum is 1 then return fileName
if keyNum is 2 then return fileExtension
end tell
end file_NE


--生成するイメージの数を返す。
on checkSize(aWidth, aHeight)
if aWidthaHeight then set the_long_side to aWidth
if aWidth < aHeight then set the_long_side to aHeight
if the_long_side ≥ 16 and the_long_side < 32 then set n to 1
if the_long_side ≥ 32 and the_long_side < 64 then set n to 3
if the_long_side ≥ 64 and the_long_side < 128 then set n to 4
if the_long_side ≥ 128 and the_long_side < 256 then set n to 5
if the_long_side ≥ 256 and the_long_side < 512 then set n to 7
if the_long_side ≥ 512 and the_long_side < 1024 then set n to 9
if the_long_side ≥ 1024 then set n to 10
return n
end checkSize

--画像の縦横比を判定
on checkAspectRatio(aFile)
set aRes to retExifAttributeData(aFile, "pixelWidth") of me as number
set bRes to retExifAttributeData(aFile, "pixelHeight") of me as number
return (aRes / bRes)
end checkAspectRatio


--“Cropped_image”フォルダに画像をコピーし、画像をクロップ。
on cropImageAndReturnPath(aFile, exportPath)
set aRes to retExifAttributeData(aFile, "pixelWidth") of me as number
set bRes to retExifAttributeData(aFile, "pixelHeight") of me as number
if aResbRes then
set longSide to bRes
else
set longSide to aRes
end if
tell application "Finder"
set fn to name of aFile
tell folder exportPath
if not (exists folder "Cropped_image") then
make new folder at exportPath with properties {name:"Cropped_image"}
end if
do shell script "cp -f " & (quoted form of (POSIX path of aFile)) & " " & (quoted form of (POSIX path of ((exportPath as text) & "Cropped_image:" & fn)))
delay 1
end tell
end tell
do shell script "sips -c " & longSide & " " & longSide & " " & quoted form of (POSIX path of ((exportPath as text) & "Cropped_image:" & fn))
delay 1
return ((exportPath as text) & "Cropped_image:" & fn) as alias
end cropImageAndReturnPath

on run
tell application "Finder"
set sel to selection as alias list
if sel is not {} then
my main(sel)
end if
end tell
end run

画像は、上記スクリプトをAutomatorアプリケーションに組み込んで実行した時の動作です。
macOS Mojave 10.14.6で動作を確認してます。

更新履歴

inserted by FC2 system