001 | #! /usr/bin/env osascript |
---|
002 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
---|
003 | # |
---|
004 | # com.cocolog-nifty.quicktimer.icefloe |
---|
005 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
---|
006 | use AppleScript version "2.8" |
---|
007 | use framework "Foundation" |
---|
008 | use framework "AppKit" |
---|
009 | use framework "UniformTypeIdentifiers" |
---|
010 | use scripting additions |
---|
011 | |
---|
012 | property refMe : a reference to current application |
---|
013 | |
---|
014 | ################################### |
---|
015 | #設定項目 |
---|
016 | set strBundleID to ("com.apple.finder.Open-Recents") as text |
---|
017 | |
---|
018 | ################################### |
---|
019 | #URLを求める |
---|
020 | set ocidAppFilePathURL to doGetBundleID2AppURL(strBundleID) |
---|
021 | if ocidAppFilePathURL = (missing value) then |
---|
022 | log "バンドルIDからアプリケーションが見つかりませんでした" |
---|
023 | return "バンドルIDからアプリケーションが見つかりませんでした" |
---|
024 | else |
---|
025 | set strAppURL to ocidAppFilePathURL's absoluteString() as text |
---|
026 | ################################### |
---|
027 | #開く |
---|
028 | set appSharedWorkspace to refMe's NSWorkspace's sharedWorkspace() |
---|
029 | set boolDone to appSharedWorkspace's openURL:(ocidAppFilePathURL) |
---|
030 | end if |
---|
031 | |
---|
032 | ################################### |
---|
033 | #このスクリプトのURL |
---|
034 | set aliasPathToMe to (path to me) as alias |
---|
035 | set strPathToMePath to (POSIX path of aliasPathToMe) as text |
---|
036 | set ocidPathToMeFilePathStr to refMe's NSString's stringWithString:(strPathToMePath) |
---|
037 | set ocidPathToMeFilePath to ocidPathToMeFilePathStr's stringByStandardizingPath() |
---|
038 | set ocidPathToMeFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidPathToMeFilePath) isDirectory:false) |
---|
039 | |
---|
040 | ################################### |
---|
041 | #アイコンパスを求める |
---|
042 | #パス部品 |
---|
043 | set ocidAppFileName to ocidAppFilePathURL's lastPathComponent() |
---|
044 | set strBaseAppName to (ocidAppFileName's stringByDeletingPathExtension()) as text |
---|
045 | set strAppFilePath to (ocidAppFilePathURL's |path|()) as text |
---|
046 | ############################## |
---|
047 | # アプリケーションのICONファイル名を取得します |
---|
048 | set ocidIconPathURL to doGetAppIconPath(ocidAppFilePathURL) |
---|
049 | if ocidIconPathURL is (missing value) then |
---|
050 | set ocidIconPathURL to doGetIosAppIconPath(ocidAppFilePathURL) |
---|
051 | if ocidIconPathURL is (missing value) then |
---|
052 | log "ICNSがない" |
---|
053 | ############################## |
---|
054 | # 保存先フォルダ作成 |
---|
055 | set ocidSaveDirPathURL to doMakeDir(strBaseAppName) |
---|
056 | if ocidSaveDirPathURL is false then |
---|
057 | return "フォルダ生成に失敗しました" |
---|
058 | end if |
---|
059 | ############################## |
---|
060 | #CARファイルのURLを取得 |
---|
061 | set ocidCarFilePathURL to doMakeIcnsFile(ocidAppFilePathURL) |
---|
062 | if ocidCarFilePathURL is false then |
---|
063 | return "CARファイルのURLの取得に失敗しました" |
---|
064 | end if |
---|
065 | |
---|
066 | ############################## |
---|
067 | #ファイルのコピー |
---|
068 | set ocidSaveCarFilePathURL to doFileCopy(ocidCarFilePathURL, ocidSaveDirPathURL) |
---|
069 | |
---|
070 | ############################## |
---|
071 | #ICONSET書き出し |
---|
072 | set ocidIconPathURL to doMakeIconset(ocidSaveCarFilePathURL) |
---|
073 | if ocidIconPathURL is false then |
---|
074 | return "ICNSファイルの生成に失敗しました" |
---|
075 | end if |
---|
076 | end if |
---|
077 | end if |
---|
078 | |
---|
079 | |
---|
080 | ################################### |
---|
081 | #アイコンを付与する |
---|
082 | #読み込みNSDATA |
---|
083 | set ocidOption to (refMe's NSDataReadingMappedIfSafe) |
---|
084 | set listResponse to refMe's NSData's alloc()'s initWithContentsOfURL:(ocidIconPathURL) options:(ocidOption) |error| :(reference) |
---|
085 | if (item 2 of listResponse) = (missing value) then |
---|
086 | log "正常処理" |
---|
087 | set ocidReadData to (item 1 of listResponse) |
---|
088 | else if (item 2 of listResponse) ≠ (missing value) then |
---|
089 | log (item 2 of listResponse)'s code() as text |
---|
090 | log (item 2 of listResponse)'s localizedDescription() as text |
---|
091 | return "エラーしました" |
---|
092 | end if |
---|
093 | #NSDATAをNSIMAGEに |
---|
094 | set ocidImageData to refMe's NSImage's alloc()'s initWithData:(ocidReadData) |
---|
095 | |
---|
096 | set appSharedWorkspace to refMe's NSWorkspace's sharedWorkspace() |
---|
097 | set ocidPathToMeFilePath to ocidPathToMeFilePathURL's |path|() |
---|
098 | ###アイコン付与 |
---|
099 | set ocidOption to (refMe's NSExclude10_4ElementsIconCreationOption) |
---|
100 | set boolDone to (appSharedWorkspace's setIcon:(ocidImageData) forFile:(ocidPathToMeFilePath) options:(ocidOption)) |
---|
101 | if boolDone is true then |
---|
102 | log "正常処理" |
---|
103 | else if boolDone is false then |
---|
104 | return "エラーしました" |
---|
105 | end if |
---|
106 | |
---|
107 | return strAppURL |
---|
108 | |
---|
109 | |
---|
110 | |
---|
111 | |
---|
112 | |
---|
113 | |
---|
114 | ############################## |
---|
115 | #ICONSET書き出し |
---|
116 | to doMakeIconset(argCarFilePathURL) |
---|
117 | set ocidSaveDirPathURL to argCarFilePathURL's URLByAppendingPathExtension:("iconset") |
---|
118 | # |
---|
119 | set appFileManager to refMe's NSFileManager's defaultManager() |
---|
120 | set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0 |
---|
121 | ocidAttrDict's setValue:(448) forKey:(refMe's NSFilePosixPermissions) |
---|
122 | set listDone to appFileManager's createDirectoryAtURL:(ocidSaveDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error| :(reference) |
---|
123 | if (item 1 of listDone) is true then |
---|
124 | log "正常処理" |
---|
125 | else if (item 2 of listDone) ≠ (missing value) then |
---|
126 | log (item 2 of listDone)'s code() as text |
---|
127 | log (item 2 of listDone)'s localizedDescription() as text |
---|
128 | log "エラーしました" |
---|
129 | return false |
---|
130 | end if |
---|
131 | #書き出す |
---|
132 | set strAssetsCarFilePath to (argCarFilePathURL's |path|()) as text |
---|
133 | set strIconsetDirPath to (ocidSaveDirPathURL's |path|()) as text |
---|
134 | try |
---|
135 | ###AppIcon指定時 |
---|
136 | set strCommandText to ("/usr/bin/iconutil --convert iconset \"" & strAssetsCarFilePath & "\" AppIcon -o \"" & strIconsetDirPath & "\"") as text |
---|
137 | log strCommandText |
---|
138 | do shell script strCommandText |
---|
139 | on error |
---|
140 | ###システム設定の指定例 |
---|
141 | try |
---|
142 | set strCommandText to ("/usr/bin/iconutil --convert iconset \"" & strAssetsCarFilePath & "\" PrefApp -o \"" & strIconsetDirPath & "\"") as text |
---|
143 | log strCommandText |
---|
144 | do shell script strCommandText |
---|
145 | on error |
---|
146 | log "AssetsにAppIconが無いか 特殊なPACK指定されている" |
---|
147 | return false |
---|
148 | end try |
---|
149 | end try |
---|
150 | # |
---|
151 | set ocidContainerDirPathURL to argCarFilePathURL's URLByDeletingLastPathComponent() |
---|
152 | set ocidAppIconFilePathURL to ocidContainerDirPathURL's URLByAppendingPathComponent:("AppIcon.icns") isDirectory:(false) |
---|
153 | set strAppIconFilePath to (ocidAppIconFilePathURL's |path|()) as text |
---|
154 | try |
---|
155 | set strCommandText to ("/usr/bin/iconutil --convert icns \"" & strIconsetDirPath & "\" -o \"" & strAppIconFilePath & "\"") as text |
---|
156 | log strCommandText |
---|
157 | do shell script strCommandText |
---|
158 | on error |
---|
159 | log "AssetsにAppIconが無いか 特殊なPACK指定されている" |
---|
160 | return false |
---|
161 | end try |
---|
162 | return ocidAppIconFilePathURL |
---|
163 | |
---|
164 | end doMakeIconset |
---|
165 | |
---|
166 | |
---|
167 | ############################## |
---|
168 | #ファイルのコピー |
---|
169 | to doFileCopy(argFilePathURL, argDistDirPathURL) |
---|
170 | set ocidFileName to argFilePathURL's lastPathComponent() |
---|
171 | set ocidDistFilePathURL to argDistDirPathURL's URLByAppendingPathComponent:(ocidFileName) isDirectory:(false) |
---|
172 | set appFileManager to refMe's NSFileManager's defaultManager() |
---|
173 | set listDone to (appFileManager's copyItemAtURL:(argFilePathURL) toURL:(ocidDistFilePathURL) |error| :(reference)) |
---|
174 | if (item 1 of listDone) is true then |
---|
175 | log "正常処理" |
---|
176 | else if (item 2 of listDone) ≠ (missing value) then |
---|
177 | log (item 2 of listDone)'s code() as text |
---|
178 | log (item 2 of listDone)'s localizedDescription() as text |
---|
179 | log "エラーしました" |
---|
180 | end if |
---|
181 | return ocidDistFilePathURL |
---|
182 | end doFileCopy |
---|
183 | |
---|
184 | ################################### |
---|
185 | #Assets.carのファイルの取得 |
---|
186 | to doMakeIcnsFile(argAppFileURL) |
---|
187 | set ocidCarFilePathURL to argAppFileURL's URLByAppendingPathComponent:("WrappedBundle/Assets.car") isDirectory:(false) |
---|
188 | #あるか?確認 |
---|
189 | set ocidCarFilePath to ocidCarFilePathURL's |path|() |
---|
190 | set appFileManager to refMe's NSFileManager's defaultManager() |
---|
191 | set boolFileExists to appFileManager's fileExistsAtPath:(ocidCarFilePath) isDirectory:(false) |
---|
192 | if boolFileExists is true then |
---|
193 | #あるので ocidCarFilePathURL がCarファイル |
---|
194 | return ocidCarFilePathURL |
---|
195 | else if boolFileExists is false then |
---|
196 | set ocidCarFilePathURL to argAppFileURL's URLByAppendingPathComponent:("Contents/Resources/Assets.car") isDirectory:(false) |
---|
197 | set ocidCarFilePath to ocidCarFilePathURL's |path|() |
---|
198 | set boolFileExists to appFileManager's fileExistsAtPath:(ocidCarFilePath) isDirectory:(false) |
---|
199 | if boolFileExists is true then |
---|
200 | #あるので ocidCarFilePathURL がCarファイル |
---|
201 | return ocidCarFilePathURL |
---|
202 | else if boolFileExists is false then |
---|
203 | return false |
---|
204 | end if |
---|
205 | end if |
---|
206 | end doMakeIcnsFile |
---|
207 | |
---|
208 | ################################### |
---|
209 | #アイコンデータの保存先フォルダを作る |
---|
210 | to doMakeDir(argSubPathStr) |
---|
211 | set appFileManager to refMe's NSFileManager's defaultManager() |
---|
212 | set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSPicturesDirectory) inDomains:(refMe's NSUserDomainMask)) |
---|
213 | set ocidPicturesDirPathURL to ocidURLsArray's firstObject() |
---|
214 | set strSubPath to ("Icons/AppIcon/" & argSubPathStr) as text |
---|
215 | set ocidSaveDirPathURL to ocidPicturesDirPathURL's URLByAppendingPathComponent:(strSubPath) isDirectory:(true) |
---|
216 | #アイコンコピー先フォルダを作る |
---|
217 | set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0 |
---|
218 | ocidAttrDict's setValue:(448) forKey:(refMe's NSFilePosixPermissions) |
---|
219 | set listDone to appFileManager's createDirectoryAtURL:(ocidSaveDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error| :(reference) |
---|
220 | if (item 1 of listDone) is true then |
---|
221 | log "正常処理" |
---|
222 | return ocidSaveDirPathURL |
---|
223 | else if (item 2 of listDone) ≠ (missing value) then |
---|
224 | log (item 2 of listDone)'s code() as text |
---|
225 | log (item 2 of listDone)'s localizedDescription() as text |
---|
226 | log "エラーしました" |
---|
227 | return false |
---|
228 | end if |
---|
229 | end doMakeDir |
---|
230 | |
---|
231 | ################################### |
---|
232 | # 通常IOSタイプのアプリケーション |
---|
233 | to doGetIosAppIconPath(argAppFileURL) |
---|
234 | #PLISTパス |
---|
235 | set ocidPlistFilePathURL to argAppFileURL's URLByAppendingPathComponent:("WrappedBundle/Info.plist") |
---|
236 | #NSDATA |
---|
237 | set ocidOption to (refMe's NSDataReadingMappedIfSafe) |
---|
238 | set listResponse to refMe's NSData's alloc()'s initWithContentsOfURL:(ocidPlistFilePathURL) options:(ocidOption) |error| :(reference) |
---|
239 | if (item 2 of listResponse) = (missing value) then |
---|
240 | log "正常処理" |
---|
241 | set ocidPlistData to (item 1 of listResponse) |
---|
242 | else if (item 2 of listResponse) ≠ (missing value) then |
---|
243 | log (item 2 of listResponse)'s code() as text |
---|
244 | log (item 2 of listResponse)'s localizedDescription() as text |
---|
245 | log "エラーしました" |
---|
246 | return (missing value) |
---|
247 | end if |
---|
248 | #PLIST |
---|
249 | set ocidFormat to (refMe's NSPropertyListXMLFormat_v1_0) |
---|
250 | set appSerialization to (refMe's NSPropertyListSerialization) |
---|
251 | set ocidOption to (refMe's NSPropertyListMutableContainersAndLeaves) |
---|
252 | set listResponse to appSerialization's propertyListWithData:(ocidPlistData) options:(ocidOption) format:(ocidFormat) |error| :(reference) |
---|
253 | if (item 2 of listResponse) = (missing value) then |
---|
254 | log "正常処理" |
---|
255 | set ocidPlistDict to (item 1 of listResponse) |
---|
256 | else if (item 2 of listResponse) ≠ (missing value) then |
---|
257 | log (item 2 of listResponse)'s code() as text |
---|
258 | log (item 2 of listResponse)'s localizedDescription() as text |
---|
259 | log "エラーしました" |
---|
260 | return (missing value) |
---|
261 | end if |
---|
262 | #アイコンのファイルパスを取得 |
---|
263 | set ocidIconFileName to ocidPlistDict's valueForKey:("CFBundleIconFile") |
---|
264 | #Iconファイルの指定がない場合 |
---|
265 | if ocidIconFileName = (missing value) then |
---|
266 | return (missing value) |
---|
267 | end if |
---|
268 | #Iconファイルの指定がある場合 |
---|
269 | set boolIcns to (ocidIconFileName's hasSuffix:("icns")) as boolean |
---|
270 | if boolIcns is true then |
---|
271 | #拡張子まで指定されている場合 |
---|
272 | set strSubPath to ("Contents/Resources/" & ocidIconFileName) as text |
---|
273 | set ocidIconFilePathURL to ocidAppFilePathURL's URLByAppendingPathComponent:(strSubPath) |
---|
274 | else |
---|
275 | #拡張子指定がない場合は拡張子をつけておく |
---|
276 | set strSubPath to ("Contents/Resources/" & ocidIconFileName) as text |
---|
277 | set ocidIconFilePathURL to ocidAppFilePathURL's URLByAppendingPathComponent:(strSubPath) |
---|
278 | set ocidIconFilePathURL to ocidIconFilePathURL's URLByAppendingPathExtension:("icns") |
---|
279 | end if |
---|
280 | return ocidIconFilePathURL |
---|
281 | end doGetIosAppIconPath |
---|
282 | |
---|
283 | ################################### |
---|
284 | # 通常MacOSタイプのアプリケーション |
---|
285 | to doGetAppIconPath(argAppFileURL) |
---|
286 | #PLISTパス |
---|
287 | set ocidPlistFilePathURL to argAppFileURL's URLByAppendingPathComponent:("Contents/Info.plist") |
---|
288 | #NSDATA |
---|
289 | set ocidOption to (refMe's NSDataReadingMappedIfSafe) |
---|
290 | set listResponse to refMe's NSData's alloc()'s initWithContentsOfURL:(ocidPlistFilePathURL) options:(ocidOption) |error| :(reference) |
---|
291 | if (item 2 of listResponse) = (missing value) then |
---|
292 | log "正常処理" |
---|
293 | set ocidPlistData to (item 1 of listResponse) |
---|
294 | else if (item 2 of listResponse) ≠ (missing value) then |
---|
295 | log (item 2 of listResponse)'s code() as text |
---|
296 | log (item 2 of listResponse)'s localizedDescription() as text |
---|
297 | log "エラーしました" |
---|
298 | return (missing value) |
---|
299 | end if |
---|
300 | #PLIST |
---|
301 | set ocidFormat to (refMe's NSPropertyListXMLFormat_v1_0) |
---|
302 | set appSerialization to (refMe's NSPropertyListSerialization) |
---|
303 | set ocidOption to (refMe's NSPropertyListMutableContainersAndLeaves) |
---|
304 | set listResponse to appSerialization's propertyListWithData:(ocidPlistData) options:(ocidOption) format:(ocidFormat) |error| :(reference) |
---|
305 | if (item 2 of listResponse) = (missing value) then |
---|
306 | log "正常処理" |
---|
307 | set ocidPlistDict to (item 1 of listResponse) |
---|
308 | else if (item 2 of listResponse) ≠ (missing value) then |
---|
309 | log (item 2 of listResponse)'s code() as text |
---|
310 | log (item 2 of listResponse)'s localizedDescription() as text |
---|
311 | log "エラーしました" |
---|
312 | return (missing value) |
---|
313 | end if |
---|
314 | #アイコンのファイルパスを取得 |
---|
315 | set ocidIconFileName to ocidPlistDict's valueForKey:("CFBundleIconFile") |
---|
316 | #Iconファイルの指定がない場合 |
---|
317 | if ocidIconFileName = (missing value) then |
---|
318 | return (missing value) |
---|
319 | end if |
---|
320 | #Iconファイルの指定がある場合 |
---|
321 | set boolIcns to (ocidIconFileName's hasSuffix:("icns")) as boolean |
---|
322 | if boolIcns is true then |
---|
323 | #拡張子まで指定されている場合 |
---|
324 | set strSubPath to ("Contents/Resources/" & ocidIconFileName) as text |
---|
325 | set ocidIconFilePathURL to argAppFileURL's URLByAppendingPathComponent:(strSubPath) |
---|
326 | else |
---|
327 | #拡張子指定がない場合は拡張子をつけておく |
---|
328 | set strSubPath to ("Contents/Resources/" & ocidIconFileName) as text |
---|
329 | set ocidIconFilePathURL to argAppFileURL's URLByAppendingPathComponent:(strSubPath) |
---|
330 | set ocidIconFilePathURL to ocidIconFilePathURL's URLByAppendingPathExtension:("icns") |
---|
331 | end if |
---|
332 | return ocidIconFilePathURL |
---|
333 | end doGetAppIconPath |
---|
334 | |
---|
335 | |
---|
336 | ################################### |
---|
337 | ### バンドルIDからアプリケーションURL |
---|
338 | ################################### |
---|
339 | to doGetBundleID2AppURL(argBundleID) |
---|
340 | set strBundleID to argBundleID as text |
---|
341 | set appSharedWorkspace to refMe's NSWorkspace's sharedWorkspace() |
---|
342 | ##バンドルIDからアプリケーションのURLを取得 |
---|
343 | set ocidAppBundle to (refMe's NSBundle's bundleWithIdentifier:(strBundleID)) |
---|
344 | if ocidAppBundle ≠ (missing value) then |
---|
345 | set ocidAppPathURL to ocidAppBundle's bundleURL() |
---|
346 | else if ocidAppBundle = (missing value) then |
---|
347 | set ocidAppPathURL to (appSharedWorkspace's URLForApplicationWithBundleIdentifier:(argBundleID)) |
---|
348 | end if |
---|
349 | ##予備(アプリケーションのURL) |
---|
350 | if ocidAppPathURL = (missing value) then |
---|
351 | tell application "Finder" |
---|
352 | try |
---|
353 | set aliasAppApth to (application file id strBundleID) as alias |
---|
354 | set strAppPath to (POSIX path of aliasAppApth) as text |
---|
355 | set strAppPathStr to refMe's NSString's stringWithString:(strAppPath) |
---|
356 | set strAppPath to strAppPathStr's stringByStandardizingPath() |
---|
357 | set ocidAppPathURL to refMe's NSURL's alloc()'s initFileURLWithPath:(strAppPath) isDirectory:true |
---|
358 | on error |
---|
359 | log "アプリケーションが見つかりませんでした" |
---|
360 | return (missing value) |
---|
361 | end try |
---|
362 | end tell |
---|
363 | end if |
---|
364 | return ocidAppPathURL |
---|
365 | end doGetBundleID2AppURL |
---|