[NSGraphicsContext]画像からアイコンセットiconsetとアイコンファイルicnsを作成する (元画像の縦横比のまま作成)
AppleScript サンプルコード
行番号 | ソース |
---|---|
001 | #!/usr/bin/env osascript |
002 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
003 | # |
004 | # 読み込んだ画像の縦横比のままアイコンファイルを作成します |
005 | # はみ出す部分は透過されます |
006 | #com.cocolog-nifty.quicktimer.icefloe |
007 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
008 | use AppleScript version "2.8" |
009 | use framework "Foundation" |
010 | use framework "UniformTypeIdentifiers" |
011 | use framework "AppKit" |
012 | use scripting additions |
013 | |
014 | property refMe : a reference to current application |
015 | |
016 | set appShardWorkspace to refMe's NSWorkspace's sharedWorkspace() |
017 | set appFileManager to refMe's NSFileManager's defaultManager() |
018 | #設定項目作成するアイコンサイズ |
019 | set listPxSize to {32, 40, 58, 60, 64, 76, 80, 87, 114, 120, 128, 136, 152, 167, 180, 192, 256, 512, 1024, 2048} as list |
020 | |
021 | ###ダイアログ |
022 | set strName to (name of current application) as text |
023 | if strName is "osascript" then |
024 | tell application "Finder" to activate |
025 | else |
026 | tell current application to activate |
027 | end if |
028 | set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask)) |
029 | set ocidDesktopDirPathURL to ocidURLsArray's firstObject() |
030 | set aliasDefaultLocation to (ocidDesktopDirPathURL's absoluteURL()) as alias |
031 | set listUTI to {"public.image"} as list |
032 | set strMes to ("ファイルを選んでください") as text |
033 | set strPrompt to ("ファイルを選んでください") as text |
034 | try |
035 | set aliasFilePath to (choose file strMes with prompt strPrompt default location (aliasDefaultLocation) of type listUTI with invisibles and showing package contents without multiple selections allowed) as alias |
036 | on error |
037 | log "エラーしました" |
038 | return "エラーしました" |
039 | end try |
040 | |
041 | set strFilePath to (POSIX path of aliasFilePath) as text |
042 | set ocidFilePathStr to refMe's NSString's stringWithString:(strFilePath) |
043 | set ocidFilePath to ocidFilePathStr's stringByStandardizingPath() |
044 | set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:false) |
045 | set ocidFileName to ocidFilePathURL's lastPathComponent() |
046 | set ocidBaseFileName to ocidFileName's stringByDeletingPathExtension() |
047 | set ocidSaveDirName to ocidBaseFileName's stringByAppendingPathExtension:("iconset") |
048 | set ocidSaveIcnsFileName to ocidBaseFileName's stringByAppendingPathExtension:("icns") |
049 | #保存先 |
050 | set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSPicturesDirectory) inDomains:(refMe's NSUserDomainMask)) |
051 | set ocidPicturesDirPathURL to ocidURLsArray's firstObject() |
052 | set ocidIconDirPathURL to ocidPicturesDirPathURL's URLByAppendingPathComponent:("Icons/MakeIcon") isDirectory:(true) |
053 | #iconsetディレクトリ |
054 | set ocidSaveDirPathURL to ocidIconDirPathURL's URLByAppendingPathComponent:(ocidSaveDirName) isDirectory:(true) |
055 | set strSaveDirPath to ocidSaveDirPathURL's |path| as text |
056 | #icnsファイルパス |
057 | set ocidSaveIcnsFilePathURL to ocidIconDirPathURL's URLByAppendingPathComponent:(ocidSaveIcnsFileName) isDirectory:(false) |
058 | set strSaveIcnsFilePath to ocidSaveIcnsFilePathURL's |path| as text |
059 | #iconsetのフォルダを作っておく |
060 | set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0 |
061 | ocidAttrDict's setValue:(448) forKey:(refMe's NSFilePosixPermissions) |
062 | set listDone to appFileManager's createDirectoryAtURL:(ocidSaveDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error| :(reference) |
063 | |
064 | #カラー |
065 | set strIccFilePath to ("/System/Library/ColorSync/Profiles/sRGB Profile.icc") as text |
066 | set ocidIccFilePathStr to refMe's NSString's stringWithString:(strIccFilePath) |
067 | set ocidIccFilePath to ocidIccFilePathStr's stringByStandardizingPath |
068 | set ocidIccFilePathURL to refMe's NSURL's alloc()'s initFileURLWithPath:(ocidIccFilePath) isDirectory:(false) |
069 | set ocidProfileData to refMe's NSData's alloc()'s initWithContentsOfURL:(ocidIccFilePathURL) |
070 | |
071 | #NSADATA |
072 | set ocidOption to (refMe's NSDataReadingMappedIfSafe) |
073 | set listResponse to refMe's NSData's alloc()'s initWithContentsOfURL:(ocidFilePathURL) options:(ocidOption) |error| :(reference) |
074 | set ocidReadData to (item 1 of listResponse) |
075 | |
076 | #NSIMAGE |
077 | set ocidReadImage to refMe's NSImage's alloc()'s initWithData:(ocidReadData) |
078 | set ocidReadImgRep to (ocidReadImage's representations)'s firstObject() |
079 | set numpixelsWidth to ocidReadImgRep's pixelsWide() |
080 | set numpixelsHeight to ocidReadImgRep's pixelsHigh() |
081 | set ocidSetSize to refMe's NSSize's NSMakeSize(numpixelsWidth, numpixelsHeight) |
082 | ocidReadImgRep's setSize:(ocidSetSize) |
083 | set ocidFromCopyRect to refMe's NSRect's NSMakeRect(0, 0, numpixelsWidth, numpixelsHeight) |
084 | |
085 | #元になる2048pxの画像作成 |
086 | set ocidFullImageRep to (refMe's NSBitmapImageRep's alloc()'s initWithBitmapDataPlanes:(missing value) pixelsWide:(2048) pixelsHigh:(2048) bitsPerSample:8 samplesPerPixel:4 hasAlpha:(true) isPlanar:(false) colorSpaceName:(refMe's NSCalibratedRGBColorSpace) bitmapFormat:(refMe's NSBitmapFormatAlphaFirst) bytesPerRow:0 bitsPerPixel:32) |
087 | ocidFullImageRep's setProperty:(refMe's NSImageColorSyncProfileData) withValue:(ocidProfileData) |
088 | #編集開始 |
089 | refMe's NSGraphicsContext's saveGraphicsState() |
090 | set ocidSetImageContext to (refMe's NSGraphicsContext's graphicsContextWithBitmapImageRep:(ocidFullImageRep)) |
091 | ocidSetImageContext's setImageInterpolation:(refMe's NSImageInterpolationHigh) |
092 | ocidSetImageContext's setColorRenderingIntent:(refMe's NSColorRenderingIntentSaturation) |
093 | ocidSetImageContext's setCompositingOperation:(refMe's NSCompositingOperationMultiply) |
094 | ocidSetImageContext's setShouldAntialias:(true) |
095 | refMe's NSGraphicsContext's setCurrentContext:(ocidSetImageContext) |
096 | set ocidDrawPasteRect to refMe's NSRect's NSMakeRect(0, 0, 2048, 2048) |
097 | (ocidReadImgRep's drawInRect:(ocidDrawPasteRect) fromRect:(ocidFromCopyRect) operation:(refMe's NSCompositingOperationSourceOver) fraction:(1.0) respectFlipped:(false) hints:(missing value)) |
098 | #編集終了 |
099 | refMe's NSGraphicsContext's restoreGraphicsState() |
100 | #編集後の元画像の矩形 |
101 | set ocidFromCopyRect to refMe's NSRect's NSMakeRect(0, 0, 2048, 2048) |
102 | |
103 | #################### |
104 | #iconset作成開始 |
105 | #################### |
106 | #16サイズだけ個別で作成 |
107 | set itemPxSize to 16 as integer |
108 | set ocidIconImageRep to (refMe's NSBitmapImageRep's alloc()'s initWithBitmapDataPlanes:(missing value) pixelsWide:(itemPxSize) pixelsHigh:(itemPxSize) bitsPerSample:8 samplesPerPixel:4 hasAlpha:(true) isPlanar:(false) colorSpaceName:(refMe's NSCalibratedRGBColorSpace) bitmapFormat:(refMe's NSBitmapFormatAlphaFirst) bytesPerRow:0 bitsPerPixel:32) |
109 | ocidIconImageRep's setProperty:(refMe's NSImageColorSyncProfileData) withValue:(ocidProfileData) |
110 | #縦型 横型でコピー位置調整 |
111 | if numpixelsWidth = numpixelsHeight then |
112 | set ocidDrawPasteRect to current application's NSRect's NSMakeRect(0, 0, itemPxSize, itemPxSize) |
113 | else if numpixelsWidth > numpixelsHeight then |
114 | set numRatio to (numpixelsHeight / numpixelsWidth) as number |
115 | set numSetH to (itemPxSize * numRatio) as integer |
116 | set numSetY to ((itemPxSize - numSetH) / 2) as integer |
117 | set ocidDrawPasteRect to current application's NSRect's NSMakeRect(0, numSetY, itemPxSize, numSetH) |
118 | else if numpixelsWidth < numpixelsHeight then |
119 | set numRatio to (numpixelsWidth / numpixelsHeight) as number |
120 | set numSetW to (itemPxSize * numRatio) as integer |
121 | set numSetX to ((itemPxSize - numSetW) / 2) as integer |
122 | set ocidDrawPasteRect to current application's NSRect's NSMakeRect(numSetX, 0, numSetW, itemPxSize) |
123 | end if |
124 | |
125 | #編集開始 |
126 | refMe's NSGraphicsContext's saveGraphicsState() |
127 | set ocidSetImageContext to (refMe's NSGraphicsContext's graphicsContextWithBitmapImageRep:(ocidIconImageRep)) |
128 | ocidSetImageContext's setImageInterpolation:(refMe's NSImageInterpolationHigh) |
129 | ocidSetImageContext's setColorRenderingIntent:(refMe's NSColorRenderingIntentSaturation) |
130 | ocidSetImageContext's setCompositingOperation:(refMe's NSCompositingOperationMultiply) |
131 | ocidSetImageContext's setShouldAntialias:(true) |
132 | refMe's NSGraphicsContext's setCurrentContext:(ocidSetImageContext) |
133 | (ocidFullImageRep's drawInRect:(ocidDrawPasteRect) fromRect:(ocidFromCopyRect) operation:(refMe's NSCompositingOperationSourceOver) fraction:(1.0) respectFlipped:(false) hints:(missing value)) |
134 | #編集終了 |
135 | refMe's NSGraphicsContext's restoreGraphicsState() |
136 | #解像度設定72ppi |
137 | set ocidSetSize to refMe's NSSize's NSMakeSize(itemPxSize, itemPxSize) |
138 | ocidIconImageRep's setSize:(ocidSetSize) |
139 | #PNG保存オプション |
140 | set ocidProperty to (refMe's NSMutableDictionary's alloc()'s initWithCapacity:0) |
141 | (ocidProperty's setObject:(refMe's NSNumber's numberWithBool:false) forKey:(refMe's NSImageInterlaced)) |
142 | (ocidProperty's setObject:(refMe's NSNumber's numberWithDouble:(1 / 2.2)) forKey:(refMe's NSImageGamma)) |
143 | #保存 |
144 | set ocidNSInlineData to (ocidIconImageRep's representationUsingType:(refMe's NSBitmapImageFileTypePNG) |properties|:(ocidProperty)) |
145 | set ocidSaveFilePathURL to ocidSaveDirPathURL's URLByAppendingPathComponent:("icon_16x16.png") isDirectory:(false) |
146 | set boolDone to (ocidNSInlineData's writeToURL:(ocidSaveFilePathURL) options:(refMe's NSDataWritingAtomic) |error| :(reference)) |
147 | #保存画像を一旦クリア |
148 | set ocidIconImageRep to "" |
149 | |
150 | #設定項目のサイズに従って順番に処理 |
151 | repeat with itemPxSize in listPxSize |
152 | #72ppito144ppiでファイル名を設定 |
153 | set numPxSize to itemPxSize as integer |
154 | set numHighResPtSize to (numPxSize / 2) as integer |
155 | set strFileName to ("icon_" & numPxSize & "x" & numPxSize & ".png") as text |
156 | set strHighResFileName to ("icon_" & numHighResPtSize & "x" & numHighResPtSize & "@2x.png") as text |
157 | #設定サイズで画像を作成 |
158 | set ocidIconImageRep to (refMe's NSBitmapImageRep's alloc()'s initWithBitmapDataPlanes:(missing value) pixelsWide:(numPxSize) pixelsHigh:(numPxSize) bitsPerSample:8 samplesPerPixel:4 hasAlpha:(true) isPlanar:(false) colorSpaceName:(refMe's NSCalibratedRGBColorSpace) bitmapFormat:(refMe's NSBitmapFormatAlphaFirst) bytesPerRow:0 bitsPerPixel:32) |
159 | (ocidIconImageRep's setProperty:(refMe's NSImageColorSyncProfileData) withValue:(ocidProfileData)) |
160 | if numpixelsWidth = numpixelsHeight then |
161 | set ocidDrawPasteRect to current application's NSRect's NSMakeRect(0, 0, numPxSize, numPxSize) |
162 | else if numpixelsWidth > numpixelsHeight then |
163 | set numRatio to (numpixelsHeight / numpixelsWidth) as number |
164 | set numSetH to (numPxSize * numRatio) as integer |
165 | set numSetY to ((numPxSize - numSetH) / 2) as integer |
166 | set ocidDrawPasteRect to current application's NSRect's NSMakeRect(0, numSetY, numPxSize, numSetH) |
167 | else if numpixelsWidth < numpixelsHeight then |
168 | set numRatio to (numpixelsWidth / numpixelsHeight) as number |
169 | set numSetW to (numPxSize * numRatio) as integer |
170 | set numSetX to ((numPxSize - numSetW) / 2) as integer |
171 | set ocidDrawPasteRect to current application's NSRect's NSMakeRect(numSetX, 0, numSetW, numPxSize) |
172 | end if |
173 | #編集開始 |
174 | refMe's NSGraphicsContext's saveGraphicsState() |
175 | set ocidSetImageContext to (refMe's NSGraphicsContext's graphicsContextWithBitmapImageRep:(ocidIconImageRep)) |
176 | (ocidSetImageContext's setImageInterpolation:(refMe's NSImageInterpolationHigh)) |
177 | (ocidSetImageContext's setColorRenderingIntent:(refMe's NSColorRenderingIntentSaturation)) |
178 | (ocidSetImageContext's setCompositingOperation:(refMe's NSCompositingOperationMultiply)) |
179 | (ocidSetImageContext's setShouldAntialias:(true)) |
180 | (refMe's NSGraphicsContext's setCurrentContext:(ocidSetImageContext)) |
181 | (ocidFullImageRep's drawInRect:(ocidDrawPasteRect) fromRect:(ocidFromCopyRect) operation:(refMe's NSCompositingOperationSourceOver) fraction:(1.0) respectFlipped:(false) hints:(missing value)) |
182 | #編集終了 |
183 | refMe's NSGraphicsContext's restoreGraphicsState() |
184 | #72ppi ptサイズ設定 |
185 | set ocidSetSize to refMe's NSSize's NSMakeSize(numPxSize, numPxSize) |
186 | (ocidIconImageRep's setSize:(ocidSetSize)) |
187 | #72ppiで保存 |
188 | set ocidNSInlineData to (ocidIconImageRep's representationUsingType:(refMe's NSBitmapImageFileTypePNG) |properties|:(ocidProperty)) |
189 | set ocidSaveFilePathURL to (ocidSaveDirPathURL's URLByAppendingPathComponent:(strFileName) isDirectory:(false)) |
190 | set boolDone to (ocidNSInlineData's writeToURL:(ocidSaveFilePathURL) options:(refMe's NSDataWritingAtomic) |error| :(reference)) |
191 | #144ppiでptサイズ設定 |
192 | set ocidSetSize to refMe's NSSize's NSMakeSize(numHighResPtSize, numHighResPtSize) |
193 | (ocidIconImageRep's setSize:(ocidSetSize)) |
194 | #144ppiで保存 |
195 | set ocidNSInlineData to (ocidIconImageRep's representationUsingType:(refMe's NSBitmapImageFileTypePNG) |properties|:(ocidProperty)) |
196 | set ocidSaveFilePathURL to (ocidSaveDirPathURL's URLByAppendingPathComponent:(strHighResFileName) isDirectory:(false)) |
197 | set boolDone to (ocidNSInlineData's writeToURL:(ocidSaveFilePathURL) options:(refMe's NSDataWritingAtomic) |error| :(reference)) |
198 | #生成した画像をクリア |
199 | set ocidIconImageRep to "" |
200 | end repeat |
201 | set ocidFullImageRep to "" |
202 | |
203 | ################## |
204 | #ICNSファイル生成 |
205 | set strCommandText to ("/usr/bin/iconutil --convert icns \"" & strSaveDirPath & "\" -o \"" & strSaveIcnsFilePath & "\"") |
206 | log "\r" & strCommandText & "\r" |
207 | set ocidComString to refMe's NSString's stringWithString:(strCommandText) |
208 | set ocidTermTask to refMe's NSTask's alloc()'s init() |
209 | ocidTermTask's setLaunchPath:("/bin/zsh") |
210 | set ocidArgumentsArray to refMe's NSMutableArray's alloc()'s initWithCapacity:(0) |
211 | ocidArgumentsArray's addObject:("-c") |
212 | ocidArgumentsArray's addObject:(ocidComString) |
213 | ocidTermTask's setArguments:(ocidArgumentsArray) |
214 | set ocidOutPut to refMe's NSPipe's pipe() |
215 | set ocidError to refMe's NSPipe's pipe() |
216 | ocidTermTask's setStandardOutput:(ocidOutPut) |
217 | ocidTermTask's setStandardError:(ocidError) |
218 | ocidTermTask's setCurrentDirectoryURL:(ocidIconDirPathURL) |
219 | set listDoneReturn to ocidTermTask's launchAndReturnError:(reference) |
220 | if (item 1 of listDoneReturn) is (false) then |
221 | log "エラーコード:" & (item 2 of listDoneReturn)'s code() as text |
222 | log "エラードメイン:" & (item 2 of listDoneReturn)'s domain() as text |
223 | log "Description:" & (item 2 of listDoneReturn)'s localizedDescription() as text |
224 | log "FailureReason:" & (item 2 of listDoneReturn)'s localizedFailureReason() as text |
225 | end if |
226 | ################## |
227 | #終了待ち |
228 | ocidTermTask's waitUntilExit() |
229 | |
230 | set appSharedWorkspace to refMe's NSWorkspace's sharedWorkspace() |
231 | set boolDone to appSharedWorkspace's selectFile:(ocidSaveIcnsFilePathURL's |path|()) inFileViewerRootedAtPath:(ocidIconDirPathURL's |path|()) |
232 | |
233 | return "終了" |
AppleScriptで生成しました |
| 固定リンク