カラースペースで画像にタグをつける
AppleScript サンプルコード
行番号 | ソース |
---|---|
001 | #!/usr/bin/env osascript |
002 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
003 | # |
004 | # 画像データに対して カラーデータとプロファイルの有無をチェック |
005 | # tagを付与して カラーインデクスをつけます |
006 | # ドロップレットになります |
007 | # 書き出しからアプリケーションで保存してください |
008 | # com.cocolog-nifty.quicktimer.icefloe |
009 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
010 | use AppleScript version "2.6" |
011 | use framework "Foundation" |
012 | use framework "AppKit" |
013 | use scripting additions |
014 | |
015 | property refMe : a reference to current application |
016 | #ここで指定するUTIは親要素が望ましい |
017 | property strUTI : "public.image" as text |
018 | |
019 | |
020 | ################################# |
021 | #【1】クリックでオープンした場合 |
022 | on run |
023 | #ダイアログ を前面に |
024 | set strName to (name of current application) as text |
025 | if strName is "osascript" then |
026 | tell application "Finder" to activate |
027 | else |
028 | tell current application to activate |
029 | end if |
030 | #デフォルトパス |
031 | set appFileManager to refMe's NSFileManager's defaultManager() |
032 | set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask)) |
033 | set ocidDesktopDirPathURL to ocidURLsArray's firstObject() |
034 | set aliasDefaultLocation to (ocidDesktopDirPathURL's absoluteURL()) as alias |
035 | set strMes to ("画像ファイルを選んでください") as text |
036 | set strPrompt to ("画像ファイルを選んでください") as text |
037 | try |
038 | #ダイアログ |
039 | set listChooseAliasFilePath to (choose file strMes with prompt strPrompt default location (aliasDefaultLocation) of type listUTI with multiple selections allowed without invisibles and showing package contents) as list |
040 | on error |
041 | log "エラーしました" |
042 | return "エラーしました" |
043 | end try |
044 | #ファイルリストを次の処理に渡す |
045 | set boolDone to doPathSender(listChooseAliasFilePath) |
046 | |
047 | #戻り値チェック |
048 | if boolDone is false then |
049 | display alert "エラーが発生しました" message "エラーが発生しました" |
050 | return "エラー終了run" |
051 | else |
052 | return "処理終了run" |
053 | end if |
054 | end run |
055 | |
056 | ################################# |
057 | #【2】ドロップした場合 |
058 | on open listDropAliasFilePath |
059 | #########【2−1】UTIチェック |
060 | #親要素(この属性なら処理する)のUTType |
061 | set ocidChkUTType to refMe's UTType's typeWithIdentifier:(strUTI) |
062 | #サブルーチンに渡すリスト |
063 | set listAliasFilePath to {} as list |
064 | #ドロップされたアイテムの数だけ繰り返す |
065 | repeat with itemDropAliasFilePath in listDropAliasFilePath |
066 | #エイリアス |
067 | set aliasItemFilePath to itemDropAliasFilePath as alias |
068 | set strName to (name of current application) as text |
069 | if strName is "osascript" then |
070 | tell application "Finder" |
071 | #Finder情報を取得して |
072 | set recordInfoFor to info for aliasItemFilePath |
073 | end tell |
074 | else |
075 | tell current application |
076 | set recordInfoFor to info for aliasItemFilePath |
077 | end tell |
078 | end if |
079 | #UTIを取得 |
080 | set strItemUIT to (type identifier of recordInfoFor) as text |
081 | set ocidUTType to (refMe's UTType's typeWithIdentifier:(strItemUIT)) |
082 | set ocidParentUTIArray to (ocidUTType's supertypes())'s allObjects() |
083 | #含まれているか?チェックする |
084 | set boolContain to (ocidParentUTIArray's containsObject:(ocidChkUTType)) |
085 | if boolContain is true then |
086 | log "含まれているので処理対象" |
087 | copy aliasItemFilePath to end of listAliasFilePath |
088 | else if boolContain is false then |
089 | log "含まれてない" |
090 | end if |
091 | end repeat |
092 | |
093 | #########【2−1】KINDチェック |
094 | set listSendAliasFilePath to {} as list |
095 | repeat with itemAliasFilePath in listAliasFilePath |
096 | set aliasFilePath to itemAliasFilePath as alias |
097 | #処理する判定 |
098 | set boolChkAliasPath to true as boolean |
099 | try |
100 | tell application "Finder" |
101 | set strKind to (kind of aliasFilePath) as text |
102 | end tell |
103 | |
104 | if strKind is "アプリケーション" then |
105 | log "アプリケーションは処理しない" |
106 | set boolChkAliasPath to false as boolean |
107 | else if strKind is "ボリューム" then |
108 | log "ボリュームは処理しない" |
109 | set boolChkAliasPath to false as boolean |
110 | else if strKind is "エイリアス" then |
111 | log "エイリアスは処理しない" |
112 | set boolChkAliasPath to false as boolean |
113 | else if strKind is "フォルダ" then |
114 | log "フォルダは処理しない" |
115 | set boolChkAliasPath to false as boolean |
116 | end if |
117 | on error |
118 | log "シンボリックリンク等kindを取得できないファイルは処理しない" |
119 | set boolChkAliasPath to false as boolean |
120 | end try |
121 | if boolChkAliasPath is true then |
122 | copy aliasFilePath to end of listSendAliasFilePath |
123 | end if |
124 | end repeat |
125 | |
126 | #########【2−3】次工程に渡す |
127 | set boolDone to doPathSender(listSendAliasFilePath) |
128 | if boolDone is not true then |
129 | return false |
130 | else |
131 | return "処理終了open" |
132 | end if |
133 | end open |
134 | |
135 | ################################# |
136 | #【3】1と2からファイルパスのエイリアスを受け取り |
137 | #処理に順番に渡す |
138 | to doPathSender(argListAliasFilePath) |
139 | set appFileManager to refMe's NSFileManager's defaultManager() |
140 | #1回でいい処理はここに記述する |
141 | |
142 | ####1ファイルパス毎本処理に渡す |
143 | repeat with itemAliasFilePath in argListAliasFilePath |
144 | #■本処理に渡す |
145 | set boolDone to doAction(itemAliasFilePath) |
146 | #戻り値がエラーだったら? |
147 | if boolDone is false then |
148 | tell application "Finder" |
149 | set strFileName to (name of itemAliasFilePath) as text |
150 | end tell |
151 | set strMes to (strFileName & "でエラーになりました") as text |
152 | display alert "エラーが発生しました" message strMes |
153 | return strMes |
154 | #エラーになったところで止める |
155 | end if |
156 | end repeat |
157 | return true |
158 | end doPathSender |
159 | |
160 | ################################# |
161 | #【4】実際の処理は全てここ |
162 | to doAction(argAliasFilePath) |
163 | set appFileManager to refMe's NSFileManager's defaultManager() |
164 | set aliasFilePath to argAliasFilePath as alias |
165 | set strFilePath to (POSIX path of aliasFilePath) as text |
166 | set ocidFilePathStr to refMe's NSString's stringWithString:(strFilePath) |
167 | set ocidFilePath to ocidFilePathStr's stringByStandardizingPath() |
168 | set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:(false)) |
169 | ##タグを取り出しておく |
170 | set listResponse to (ocidFilePathURL's getResourceValue:(reference) forKey:(refMe's NSURLTagNamesKey) |error| :(reference)) |
171 | if (item 1 of listResponse) is true then |
172 | if (item 2 of listResponse) ≠ (missing value) then |
173 | set ocidTagNamesKeyArray to (item 2 of listResponse) |
174 | else if (item 2 of listResponse) = (missing value) then |
175 | set ocidTagNamesKeyArray to refMe's NSMutableArray's alloc()'s initWithCapacity:(0) |
176 | end if |
177 | else |
178 | return false |
179 | end if |
180 | ##現在のラベルNOを取り出しておく |
181 | set listResponse to (ocidFilePathURL's getResourceValue:(reference) forKey:(refMe's NSURLLabelNumberKey) |error| :(reference)) |
182 | if (item 1 of listResponse) is true then |
183 | set ocidLabelNo to (item 2 of listResponse) |
184 | else |
185 | return false |
186 | end if |
187 | |
188 | ##NSDATAに読み込む |
189 | set ocidOption to (refMe's NSDataReadingMappedIfSafe) |
190 | set listResponse to refMe's NSData's alloc()'s initWithContentsOfURL:(ocidFilePathURL) options:(ocidOption) |error| :(reference) |
191 | if (item 2 of listResponse) = (missing value) then |
192 | set ocidReadData to (item 1 of listResponse) |
193 | else |
194 | return false |
195 | end if |
196 | ##NSIMAGE |
197 | set ocidReadImage to refMe's NSImage's alloc()'s initWithData:(ocidReadData) |
198 | #ImageRep |
199 | set ocidImageRepArray to ocidReadImage's representations() |
200 | set ocidImageRep to ocidImageRepArray's firstObject() |
201 | |
202 | #################### |
203 | #プロファイルの有無 |
204 | set ocidColorProfileData to ocidImageRep's valueForProperty:(refMe's NSImageColorSyncProfileData) |
205 | set ocidColorSpace to refMe's NSColorSpace's alloc()'s initWithICCProfileData:(ocidColorProfileData) |
206 | # |
207 | if ocidColorProfileData ≠ (missing value) then |
208 | set ocidProfileName to ocidColorSpace's localizedName() |
209 | set strProfileName to ocidProfileName as text |
210 | else |
211 | #カラープロファイルが無い場合 |
212 | set ocidTagName to (refMe's NSString's stringWithString:("ColorProfile無")) |
213 | (ocidTagNamesKeyArray's addObject:(ocidTagName)) |
214 | #重複削除 |
215 | set ocidArraySet to refMe's NSSet's setWithArray:(ocidTagNamesKeyArray) |
216 | set ocidSetTagNamesKeyArray to ocidArraySet's allObjects() |
217 | set listDone to (ocidFilePathURL's setResourceValue:(ocidSetTagNamesKeyArray) forKey:(refMe's NSURLTagNamesKey) |error| :(reference)) |
218 | if listDone is false then |
219 | return false |
220 | end if |
221 | #ラベルを赤にする |
222 | set ocidLabelNo to refMe's NSNumber's numberWithInteger:(6) |
223 | set listDone to (ocidFilePathURL's setResourceValue:(ocidLabelNo) forKey:(refMe's NSURLLabelNumberKey) |error| :(reference)) |
224 | if listDone is false then |
225 | return false |
226 | end if |
227 | end if |
228 | #################### |
229 | #カラースペース |
230 | set ocidColorSpaceName to ocidImageRep's colorSpaceName() |
231 | set strColorSpaceName to ocidColorSpaceName as text |
232 | # |
233 | if strColorSpaceName contains "DeviceCMYK" then |
234 | set strSetColorName to ("DeviceCMYK") as text |
235 | set numIndexNo to 5 as integer |
236 | else if strColorSpaceName contains "CalibratedCMYK" then |
237 | set strSetColorName to ("CalibratedCMYK") as text |
238 | set numIndexNo to 5 as integer |
239 | else if strColorSpaceName contains "DeviceRGB" then |
240 | set strSetColorName to ("DeviceRGB") as text |
241 | set numIndexNo to 2 as integer |
242 | else if strColorSpaceName contains "CalibratedRGB" then |
243 | set strSetColorName to ("CalibratedRGB") as text |
244 | set numIndexNo to 2 as integer |
245 | else if strColorSpaceName contains "DeviceWhite" then |
246 | set strSetColorName to ("DeviceWhite") as text |
247 | set numIndexNo to 1 as integer |
248 | else if strColorSpaceName contains "CalibratedWhite" then |
249 | set strSetColorName to ("CalibratedWhite") as text |
250 | set numIndexNo to 1 as integer |
251 | else if strColorSpaceName contains "NamedColor" then |
252 | set strSetColorName to ("NamedColor") as text |
253 | set numIndexNo to 6 as integer |
254 | else if strColorSpaceName contains "Custom" then |
255 | set strSetColorName to ("Custom") as text |
256 | set numIndexNo to 6 as integer |
257 | else if strColorSpaceName contains "Pattern" then |
258 | set strSetColorName to ("Pattern") as text |
259 | set numIndexNo to 6 as integer |
260 | end if |
261 | # |
262 | set ocidTagName to (refMe's NSString's stringWithString:(strSetColorName)) |
263 | (ocidTagNamesKeyArray's addObject:(ocidTagName)) |
264 | #重複削除 |
265 | set ocidArraySet to refMe's NSSet's setWithArray:(ocidTagNamesKeyArray) |
266 | set ocidSetTagNamesKeyArray to ocidArraySet's allObjects() |
267 | set listDone to (ocidFilePathURL's setResourceValue:(ocidSetTagNamesKeyArray) forKey:(refMe's NSURLTagNamesKey) |error| :(reference)) |
268 | if (item 1 of listDone) is false then |
269 | return false |
270 | end if |
271 | # |
272 | set ocidLabelNo to refMe's NSNumber's numberWithInteger:(numIndexNo) |
273 | set listDone to (ocidFilePathURL's setResourceValue:(ocidLabelNo) forKey:(refMe's NSURLLabelNumberKey) |error| :(reference)) |
274 | if (item 1 of listDone) is false then |
275 | return false |
276 | end if |
277 | |
278 | ### |
279 | try |
280 | log strFilePath |
281 | on error |
282 | #エラーになったらfalseを戻す |
283 | return false |
284 | end try |
285 | |
286 | return true |
287 | end doAction |
AppleScriptで生成しました |
| 固定リンク