| 001 | #!/usr/bin/env osascript |
| 002 | #coding: utf-8 |
| 003 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7-- |
| 004 | (* |
| 005 |
|
| 006 | 複数選択した画像を横方向へ結合していく |
| 007 |
|
| 008 | v1 初回作成 |
| 009 |
|
| 010 | com.cocolog-nifty.quicktimer.icefloe *) |
| 011 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7-- |
| 012 | use AppleScript version "2.8" |
| 013 | use framework "Foundation" |
| 014 | use framework "AppKit" |
| 015 | use framework "UniformTypeIdentifiers" |
| 016 | use scripting additions |
| 017 | property refMe : a reference to current application |
| 018 |
|
| 019 |
|
| 020 | set appFileManager to refMe's NSFileManager's defaultManager() |
| 021 |
|
| 022 | ############################# |
| 023 | #ダイアログ |
| 024 | set appFileManager to refMe's NSFileManager's defaultManager() |
| 025 | set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask)) |
| 026 | set ocidDesktopDirPathURL to ocidURLsArray's firstObject() |
| 027 | set aliasDefaultLocation to (ocidDesktopDirPathURL's absoluteURL()) as alias |
| 028 | set listUTI to {"public.image"} as list |
| 029 | set strMes to ("画像ファイルを選んでください") as text |
| 030 | set strPrompt to ("画像ファイルを選んでください" & return & "ファイル名順に右方向へ結合します") as text |
| 031 | try |
| 032 | tell application "SystemUIServer" |
| 033 | activate |
| 034 | set listAliasFilePath to (choose file strMes with prompt strPrompt default location aliasDefaultLocation of type listUTI with invisibles and multiple selections allowed without showing package contents) as list |
| 035 | end tell |
| 036 | on error strErrMes number numErrNo |
| 037 | log strErrMes & numErrNo |
| 038 | return false |
| 039 | end try |
| 040 | if listAliasFilePath is {} then |
| 041 | return false |
| 042 | end if |
| 043 |
|
| 044 | ############################# |
| 045 | #ピクセルサイズ調査 |
| 046 |
|
| 047 | set numMaxHpx to 0 as integer |
| 048 | set numTotalWpx to 0 as integer |
| 049 |
|
| 050 | #最大高さPXの取得 と 幅PXの合計を取得 |
| 051 | repeat with itemAliasFilePath in listAliasFilePath |
| 052 | set aliasFilePath to itemAliasFilePath as alias |
| 053 | set strFilePath to (POSIX path of aliasFilePath) as text |
| 054 | set ocidFilePathStr to (refMe's NSString's stringWithString:(strFilePath)) |
| 055 | set ocidFilePath to ocidFilePathStr's stringByStandardizingPath() |
| 056 | set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:(false)) |
| 057 | set ocidImageRep to (refMe's NSImageRep's imageRepWithContentsOfURL:(ocidFilePathURL)) |
| 058 | set numHpx to ocidImageRep's pixelsHigh() |
| 059 | set numWpx to ocidImageRep's pixelsWide() |
| 060 | #最大高さの取得 |
| 061 | if numMaxHpx < numHpx then |
| 062 | set numMaxHpx to numHpx as integer |
| 063 | end if |
| 064 | #幅PXの合計 |
| 065 | set numTotalWpx to (numTotalWpx + numWpx) as integer |
| 066 | #解放 |
| 067 | set ocidImageRep to (missing value) |
| 068 | end repeat |
| 069 |
|
| 070 | log numMaxHpx |
| 071 | log numTotalWpx |
| 072 |
|
| 073 | ############################# |
| 074 | #出力画像保存先 |
| 075 | set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSPicturesDirectory) inDomains:(refMe's NSUserDomainMask)) |
| 076 | set ocidPicturesDirPathURL to ocidURLsArray's firstObject() |
| 077 | set ocidSaveDirPathURL to ocidPicturesDirPathURL's URLByAppendingPathComponent:("Image_Output") isDirectory:(true) |
| 078 | #フォルダ作っておく |
| 079 | set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s init() |
| 080 | ocidAttrDict's setValue:(448) forKey:(refMe's NSFilePosixPermissions) |
| 081 | set listDone to appFileManager's createDirectoryAtURL:(ocidSaveDirPathURL) withIntermediateDirectories:(true) attributes:(ocidAttrDict) |error|:(reference) |
| 082 | #出力画像URL |
| 083 | set strDateNo to doGetDateNo("yyyyMMdd_HHmmss") as text |
| 084 | set strSaveFileName to ("" & strDateNo & "-" & numTotalWpx & "x" & numMaxHpx & ".png") as text |
| 085 | set ocidSaveFilePathURL to ocidSaveDirPathURL's URLByAppendingPathComponent:(strSaveFileName) isDirectory:(false) |
| 086 |
|
| 087 | ############################# |
| 088 | #画像作成開始 |
| 089 | set intBPR to (4 * numTotalWpx) as integer |
| 090 | set ocidAardboardRep to refMe's NSBitmapImageRep's alloc()'s initWithBitmapDataPlanes:(missing value) pixelsWide:(numTotalWpx) pixelsHigh:(numMaxHpx) bitsPerSample:(8) samplesPerPixel:(4) hasAlpha:(true) isPlanar:(false) colorSpaceName:(refMe's NSCalibratedRGBColorSpace) bitmapFormat:(refMe's NSAlphaFirstBitmapFormat) bytesPerRow:(intBPR) bitsPerPixel:(32) |
| 091 |
|
| 092 |
|
| 093 |
|
| 094 | ######################################## |
| 095 | #合成 |
| 096 | set numSetWpx to (missing value) |
| 097 | #▼▼▼saveGraphicsState |
| 098 | ###初期化 |
| 099 | refMe's NSGraphicsContext's saveGraphicsState() |
| 100 | set ocidContext to (refMe's NSGraphicsContext's graphicsContextWithBitmapImageRep:(ocidAardboardRep)) |
| 101 | (ocidContext's setShouldAntialias:(true)) |
| 102 | (ocidContext's setImageInterpolation:(refMe's NSImageInterpolationHigh)) |
| 103 | (ocidContext's setColorRenderingIntent:(refMe's NSColorRenderingIntentRelativeColorimetric)) |
| 104 | ocidContext's setPatternPhase:(refMe's NSPoint's NSMakePoint(0, 0)) |
| 105 | (refMe's NSGraphicsContext's setCurrentContext:(ocidContext)) |
| 106 | ######################################## |
| 107 | #ペースト処理 |
| 108 | repeat with itemAliasFilePath in listAliasFilePath |
| 109 | #ファイル |
| 110 | set aliasFilePath to itemAliasFilePath as alias |
| 111 | set strFilePath to (POSIX path of aliasFilePath) as text |
| 112 | set ocidFilePathStr to (refMe's NSString's stringWithString:(strFilePath)) |
| 113 | set ocidFilePath to ocidFilePathStr's stringByStandardizingPath() |
| 114 | set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:(false)) |
| 115 | #NSDATA |
| 116 | set ocidOption to (refMe's NSDataReadingMappedIfSafe) |
| 117 | set listResponse to (refMe's NSData's alloc()'s initWithContentsOfURL:(ocidFilePathURL) options:(ocidOption) |error|:(reference)) |
| 118 | set ocidReadData to (first item of listResponse) |
| 119 | #NSIMAGE |
| 120 | set ocidReadImage to (refMe's NSImage's alloc()'s initWithData:(ocidReadData)) |
| 121 | #NSIMAGEREP |
| 122 | set ocidImageRepArray to ocidReadImage's representations() |
| 123 | set ocidImageRep to ocidImageRepArray's firstObject() |
| 124 | #ピクセルサイズ |
| 125 | set numPxW to ocidImageRep's pixelsWide() |
| 126 | set numPxH to ocidImageRep's pixelsHigh() |
| 127 | set ocidImageSizePx to refMe's NSSize's NSMakeSize(numPxW, numPxH) |
| 128 | (ocidImageRep's setSize:(ocidImageSizePx)) |
| 129 | set ocidFromRect to refMe's NSRect's NSMakeRect(0, 0, numPxW, numPxH) |
| 130 | #ペースト位置 |
| 131 | if numSetWpx = (missing value) then |
| 132 | set numSetWpx to 0 as integer |
| 133 | end if |
| 134 | set numSetHpx to (numMaxHpx - numPxH) as integer |
| 135 | set ocidDrawRect to refMe's NSRect's NSMakeRect(numSetWpx, numSetHpx, (numPxW), (numPxH)) |
| 136 | #元画像をペースト |
| 137 | set ocidOption to (refMe's NSCompositingOperationSourceOver) |
| 138 | (ocidImageRep's drawInRect:(ocidDrawRect) fromRect:(ocidFromRect) operation:(ocidOption) fraction:(1.0) respectFlipped:(true) hints:(missing value)) |
| 139 | #次の画像のペースト位置 |
| 140 | set numSetWpx to (numSetWpx + numPxW) as integer |
| 141 | #データは一旦解放 |
| 142 | set ocidImageRep to (missing value) |
| 143 | set ocidReadImage to (missing value) |
| 144 | set ocidReadData to (missing value) |
| 145 | end repeat |
| 146 |
|
| 147 | ####画像作成終了 |
| 148 | refMe's NSGraphicsContext's restoreGraphicsState() |
| 149 | #▲▲▲ restoreGraphicsState |
| 150 |
|
| 151 |
|
| 152 | ######################################## |
| 153 | #PNG変換 |
| 154 | set ocidProperty to refMe's NSMutableDictionary's alloc()'s init() |
| 155 | ocidProperty's setObject:(false) forKey:(refMe's NSImageInterlaced) |
| 156 | ocidProperty's setObject:(2.2) forKey:(refMe's NSImageGamma) |
| 157 | set ocidType to (refMe's NSBitmapImageFileTypePNG) |
| 158 | set ocidSaveData to (ocidAardboardRep's representationUsingType:(ocidType) |properties|:(ocidProperty)) |
| 159 |
|
| 160 | #保存 |
| 161 | set ocidOption to (refMe's NSDataWritingAtomic) |
| 162 | set listDone to ocidSaveData's writeToURL:(ocidSaveFilePathURL) options:(ocidOption) |error|:(reference) |
| 163 |
|
| 164 | ######################################## |
| 165 | #保存先を開く |
| 166 | set appSharedWorkspace to refMe's NSWorkspace's sharedWorkspace() |
| 167 | set boolDone to appSharedWorkspace's openURL:(ocidSaveDirPathURL) |
| 168 |
|
| 169 | return boolDone |
| 170 | #終了 |
| 171 |
|
| 172 | ############################# |
| 173 | #日付情報の取得 |
| 174 | to doGetDateNo(argFormatStrings) |
| 175 | set ocidDate to refMe's NSDate's |date|() |
| 176 | set ocidNSDateFormatter to refMe's NSDateFormatter's alloc()'s init() |
| 177 | ocidNSDateFormatter's setLocale:(refMe's NSLocale's localeWithLocaleIdentifier:"ja_JP_POSIX") |
| 178 | ocidNSDateFormatter's setDateFormat:(argFormatStrings) |
| 179 | set ocidDateAndTime to ocidNSDateFormatter's stringFromDate:(ocidDate) |
| 180 | set strDateAndTime to ocidDateAndTime as text |
| 181 | return strDateAndTime |
| 182 | end doGetDateNo |
| 183 |
|
| 184 |
|