[ドロップレット]iWorkとMicrosoft Officeファイルから画像を取り出してファイルにする(画像をオフィスファイルから抽出)
ダウンロード - office2imageexport4drop.zip
AppleScript サンプルコード
行番号 | ソース |
---|---|
001 | #!/usr/bin/env osascript |
002 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
003 | # com.cocolog-nifty.quicktimer.icefloe |
004 | # 書き出しから アプリケーションでドロップレットになります |
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 | #設定項目 対象のUTI |
015 | property listUTI : {"org.openxmlformats.presentationml.presentation", "org.openxmlformats.spreadsheetml.sheet", "org.openxmlformats.wordprocessingml.document", "com.apple.iwork.keynote.sffkey", "com.apple.iwork.numbers.sffnumbers", "com.apple.iwork.pages.sffpages"} as list |
016 | # |
017 | property strUTI : ("public.image") as text |
018 | |
019 | |
020 | ################### |
021 | #Wクリックで実行 |
022 | on run |
023 | ###ダイアログを前面に出す |
024 | tell current application |
025 | set strName to name as text |
026 | end tell |
027 | if strName is "osascript" then |
028 | tell application "Finder" to activate |
029 | else |
030 | tell current application to activate |
031 | end if |
032 | #ダイアログ |
033 | set appFileManager to refMe's NSFileManager's defaultManager() |
034 | set ocidUserDesktopPathArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask)) |
035 | set ocidUserDesktopPath to ocidUserDesktopPathArray's firstObject() |
036 | # |
037 | set strPromptText to "Microsoft Officeファイルを選んでください" as text |
038 | set strMesText to "Microsoft Officeファイルを選んでください" as text |
039 | set listChooseAliasFilePath to (choose file strMesText with prompt strPromptText default location (ocidUserDesktopPath as alias) of type listUTI with invisibles and multiple selections allowed without showing package contents) as list |
040 | #サブルーチンに渡す |
041 | set boolDone to doAction(listChooseAliasFilePath) |
042 | #戻り値がエラーだったか? |
043 | if boolDone is false then |
044 | display alert "エラーが発生しました" message "エラーが発生しました" |
045 | return |
046 | end if |
047 | return "処理終了RUN" |
048 | end run |
049 | |
050 | ################### |
051 | #ドロップ |
052 | on open listDropAliasFilePath |
053 | #サブルーチンに渡すリスト |
054 | set listAliasFilePath to {} as list |
055 | #ドロップされたアイテムの数だけ繰り返す |
056 | repeat with itemDropAliasFilePath in listDropAliasFilePath |
057 | #エイリアス |
058 | set aliasItemFilePath to itemDropAliasFilePath as alias |
059 | tell application "Finder" |
060 | #Finder情報を取得して |
061 | set recordInfoFor to info for aliasItemFilePath |
062 | end tell |
063 | #UTIを取得 |
064 | set strItemUIT to (type identifier of recordInfoFor) as text |
065 | #UTIが対象ファイルならリストに追加 |
066 | if listUTI contains strItemUIT then |
067 | copy aliasItemFilePath to end of listAliasFilePath |
068 | end if |
069 | end repeat |
070 | set numCntAliasList to (count of listAliasFilePath) as integer |
071 | if numCntAliasList > 0 then |
072 | #サブルーチンに渡す |
073 | set boolDone to doAction(listAliasFilePath) |
074 | else |
075 | display alert "エラーが発生しました対象のファイルではありません" |
076 | return "エラー終了open" |
077 | end if |
078 | #戻り値がエラーだったか? |
079 | if boolDone is false then |
080 | display alert "エラーが発生しました" message "エラーが発生しました" |
081 | return "エラー終了open" |
082 | end if |
083 | return "処理終了open" |
084 | |
085 | end open |
086 | |
087 | ################### |
088 | #実行されるのはこれ |
089 | to doAction(argListAliasFilePath) |
090 | ###### |
091 | set appFileManager to refMe's NSFileManager's defaultManager() |
092 | #テンポラリ |
093 | set ocidTempDirURL to appFileManager's temporaryDirectory() |
094 | #フォルダ作成時の属性用 |
095 | set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0 |
096 | #ファイルエイリアスリストを順番の処理 |
097 | repeat with itemAliasFilePath in argListAliasFilePath |
098 | set aliasFilePath to itemAliasFilePath as alias |
099 | set strFilePath to (POSIX path of aliasFilePath) as text |
100 | set ocidFilePath to (refMe's NSString's stringWithString:strFilePath) |
101 | set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath)) |
102 | #テンポラリ |
103 | set ocidUUID to refMe's NSUUID's alloc()'s init() |
104 | set ocidUUIDString to ocidUUID's UUIDString |
105 | set ocidUnzipDirPathURL to (ocidTempDirURL's URLByAppendingPathComponent:(ocidUUIDString) isDirectory:true) |
106 | #テンポラリフォルダ生成 アクセス権777 |
107 | (ocidAttrDict's setValue:(511) forKey:(refMe's NSFilePosixPermissions)) |
108 | set listDone to (appFileManager's createDirectoryAtURL:(ocidUnzipDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error| :(reference)) |
109 | if (item 2 of listDone) ≠ (missing value) then |
110 | set strErrorNO to (item 2 of listDone)'s code() as text |
111 | set strErrorMes to (item 2 of listDone)'s localizedDescription() as text |
112 | refMe's NSLog("■:" & strErrorNO & strErrorMes) |
113 | log "移動:エラーしました" & strErrorNO & strErrorMes |
114 | refMe's NSLog("■■■: 移動でエラーになりました") |
115 | return false |
116 | end if |
117 | set strUnzipDirPath to (ocidUnzipDirPathURL's |path|()) as text |
118 | ######## |
119 | #解凍 |
120 | set strCommandText to ("/bin/zsh -c '/usr/bin/unzip \"" & strFilePath & "\" -d \"" & strUnzipDirPath & "\"'") as text |
121 | log strCommandText |
122 | try |
123 | do shell script strCommandText |
124 | on error |
125 | refMe's NSLog("■■■: コマンドでエラーになりました") |
126 | return false |
127 | end try |
128 | ######## |
129 | #ファイル名 |
130 | set ocidFileName to ocidFilePathURL's lastPathComponent() |
131 | #ベースファイル名 |
132 | set ocidBaseFileName to (ocidFileName's stringByAppendingPathExtension:("export-image")) |
133 | #コンテナ |
134 | set ocidContainerDirPathURL to ocidFilePathURL's URLByDeletingLastPathComponent() |
135 | #画像保存先 |
136 | set ocidSaveDirPathURL to (ocidContainerDirPathURL's URLByAppendingPathComponent:(ocidBaseFileName) isDirectory:(true)) |
137 | #フォルダ生成 |
138 | (ocidAttrDict's setValue:(448) forKey:(refMe's NSFilePosixPermissions)) |
139 | set listDone to (appFileManager's createDirectoryAtURL:(ocidSaveDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error| :(reference)) |
140 | if (item 2 of listDone) ≠ (missing value) then |
141 | set strErrorNO to (item 2 of listDone)'s code() as text |
142 | set strErrorMes to (item 2 of listDone)'s localizedDescription() as text |
143 | refMe's NSLog("■:" & strErrorNO & strErrorMes) |
144 | log "移動:エラーしました" & strErrorNO & strErrorMes |
145 | end if |
146 | ######## |
147 | #コンテンツの収集 |
148 | #プロパティ |
149 | set ocidPropertiesArray to (refMe's NSMutableArray's alloc()'s initWithCapacity:(0)) |
150 | (ocidPropertiesArray's addObject:(refMe's NSURLNameKey)) |
151 | (ocidPropertiesArray's addObject:(refMe's NSURLPathKey)) |
152 | (ocidPropertiesArray's addObject:(refMe's NSURLContentTypeKey)) |
153 | #オプション |
154 | set ocidOption to (refMe's NSDirectoryEnumerationSkipsHiddenFiles) |
155 | #収集 |
156 | set ocidEmuDict to (appFileManager's enumeratorAtURL:(ocidUnzipDirPathURL) includingPropertiesForKeys:(ocidPropertiesArray) options:(ocidOption) errorHandler:(reference)) |
157 | #出力用の空の可変リスト |
158 | set ocidImagePathURLAllArray to (refMe's NSMutableArray's alloc()'s initWithCapacity:(0)) |
159 | ##allObjectsでループ |
160 | set ocidEmuFileURLArray to ocidEmuDict's allObjects() |
161 | #ループ |
162 | repeat with itemURL in ocidEmuFileURLArray |
163 | #コンテンツタイプを取得 |
164 | set listResponse to (itemURL's getResourceValue:(reference) forKey:(refMe's NSURLContentTypeKey) |error| :(reference)) |
165 | if (item 1 of listResponse) is (true) then |
166 | #UTType |
167 | set ocidUTType to (item 2 of listResponse) |
168 | #UTTypeの属性全部をリストに |
169 | set ocidParentUTIArray to (ocidUTType's supertypes())'s allObjects() |
170 | #対象のUTI |
171 | set ocidContainUTType to (refMe's UTType's typeWithIdentifier:(strUTI)) |
172 | #UTTypeリストに対象のUTIが含まれているか? |
173 | set boolImagetype to (ocidParentUTIArray's containsObject:(ocidContainUTType)) as boolean |
174 | if boolImagetype is (true) then |
175 | #出力用のリストに追加していく |
176 | (ocidImagePathURLAllArray's addObject:(itemURL)) |
177 | end if |
178 | #エラー時はログのみ |
179 | else if (item 1 of listResponse) is (false) then |
180 | log (item 3 of listResponse)'s code() as text |
181 | log (item 3 of listResponse)'s localizedDescription() as text |
182 | end if |
183 | end repeat |
184 | ######## |
185 | #取り出したリストの数だけ繰り返し |
186 | set numCntArray to (ocidImagePathURLAllArray's |count|()) as integer |
187 | repeat with itemIntNo from 0 to (numCntArray - 1) by 1 |
188 | #順番にURLを取り出して |
189 | set ocidItemImageURL to (ocidImagePathURLAllArray's objectAtIndex:(itemIntNo)) |
190 | #拡張子 |
191 | set ocidSaveFileExtension to ocidItemImageURL's pathExtension() |
192 | #画像の読み込んで |
193 | set ocidImageRep to (refMe's NSImageRep's imageRepWithContentsOfURL:(ocidItemImageURL)) |
194 | #同名回避のために連番 |
195 | set strImageNo to (itemIntNo + 1) as text |
196 | if ocidImageRep ≠ (missing value) then |
197 | if (ocidSaveFileExtension as text) is ("svg") then |
198 | set strSaveFileName to ("image@" & strImageNo) as text |
199 | else |
200 | #解像度を取得 |
201 | set strImageW to (ocidImageRep's pixelsHigh()) as text |
202 | set strImageH to (ocidImageRep's pixelsWide()) as text |
203 | #移動先ファイル名 |
204 | set strSaveFileName to (strImageNo & "@" & strImageW & "x" & strImageH) as text |
205 | end if |
206 | else |
207 | set strSaveFileName to ("image@" & strImageNo) as text |
208 | end if |
209 | #移動先のパスを作って |
210 | set ocidSaveFileBasePathURL to (ocidSaveDirPathURL's URLByAppendingPathComponent:(strSaveFileName) isDirectory:(false)) |
211 | #拡張子つけて |
212 | set ocidSaveFilePathURL to (ocidSaveFileBasePathURL's URLByAppendingPathExtension:(ocidSaveFileExtension)) |
213 | #移動 |
214 | set listDone to (appFileManager's moveItemAtURL:(ocidItemImageURL) toURL:(ocidSaveFilePathURL) |error| :(reference)) |
215 | if (item 1 of listDone) is true then |
216 | # log "正常処理" |
217 | else if (item 2 of listDone) ≠ (missing value) then |
218 | set strErrorNO to (item 2 of listDone)'s code() as text |
219 | set strErrorMes to (item 2 of listDone)'s localizedDescription() as text |
220 | refMe's NSLog("■:" & strErrorNO & strErrorMes) |
221 | log "移動:エラーしました" & strErrorNO & strErrorMes |
222 | end if |
223 | end repeat |
224 | |
225 | end repeat |
226 | #全部エラーなく終わったらtrueを戻す |
227 | return true |
228 | end doAction |
AppleScriptで生成しました |
| 固定リンク