Safariで表示中のページから画像を収集する
AppleScript サンプルコード
行番号 | ソース |
---|---|
001 | #!/usr/bin/env osascript |
002 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
003 | # |
004 | (* |
005 | BASE SCRIPT |
006 | https://forum.latenightsw.com/t/list-of-links-on-webpage/1328 |
007 | |
008 | WEBページの画像のURLを取得する |
009 | natalieの写真のダウンロードに対応した |
010 | サインインが必要なページは取得できない |
011 | CSS指定の画像は取得できない等 |
012 | あまり実用的ではなかった…トホホ |
013 | CURLでHTML取得してファイルを解析した方が良さそう |
014 | *) |
015 | #com.cocolog-nifty.quicktimer.icefloe |
016 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
017 | use AppleScript version "2.8" |
018 | use framework "Foundation" |
019 | use framework "AppKit" |
020 | use scripting additions |
021 | property refMe : a reference to current application |
022 | set appFileManager to refMe's NSFileManager's defaultManager() |
023 | |
024 | |
025 | ###全面のタブのURLを取得して |
026 | tell application "Safari" |
027 | tell front window |
028 | tell current tab |
029 | set strURL to URL as text |
030 | end tell |
031 | end tell |
032 | end tell |
033 | ############################## |
034 | #URL |
035 | set ocidURL to refMe's NSURL's alloc()'s initWithString:(strURL) |
036 | #コンポーネント |
037 | set ocidURLcomp to refMe's NSURLComponents's alloc()'s init() |
038 | ocidURLcomp's setScheme:(ocidURL's |scheme|()) |
039 | ocidURLcomp's setHost:(ocidURL's |host|()) |
040 | set ocidBaseURL to ocidURLcomp's |URL|() |
041 | #スキーム+ホストのURL |
042 | set ocidBaseURLSTR to refMe's NSMutableString's alloc()'s initWithString:(ocidBaseURL's absoluteString()) |
043 | ocidURLcomp's |path|() |
044 | set ocidBaseURL to ocidURLcomp's |URL|() |
045 | #パスまで入れたURL |
046 | set ocidBaseURLPATH to refMe's NSMutableString's alloc()'s initWithString:(ocidBaseURL's absoluteString()) |
047 | |
048 | ############################## |
049 | #前面ドキュメントのHTMLソース |
050 | tell application "Safari" |
051 | tell front document |
052 | set strHTML to source as text |
053 | end tell |
054 | end tell |
055 | |
056 | ############################## |
057 | #戻り値整形 |
058 | set ocidResponseText to (refMe's NSString's stringWithString:(strHTML)) |
059 | ###タブと改行を除去しておく |
060 | set ocidTextM to refMe's NSMutableString's alloc()'s initWithCapacity:(0) |
061 | ocidTextM's appendString:(ocidResponseText) |
062 | ###除去する文字列リスト |
063 | set listRemoveChar to {"\n", "\r", "\t"} as list |
064 | ##置換 |
065 | repeat with itemChar in listRemoveChar |
066 | set strPattern to itemChar as text |
067 | set strTemplate to ("") as text |
068 | set ocidOption to (refMe's NSRegularExpressionCaseInsensitive) |
069 | set listResponse to (refMe's NSRegularExpression's regularExpressionWithPattern:(strPattern) options:(ocidOption) |error| :(reference)) |
070 | if (item 2 of listResponse) ≠ (missing value) then |
071 | log (item 2 of listResponse)'s localizedDescription() as text |
072 | return "正規表現パターンに誤りがあります" |
073 | else |
074 | set ocidRegex to (item 1 of listResponse) |
075 | end if |
076 | set numLength to ocidResponseText's |length|() |
077 | set ocidRange to refMe's NSRange's NSMakeRange(0, numLength) |
078 | set ocidResponseText to (ocidRegex's stringByReplacingMatchesInString:(ocidResponseText) options:0 range:(ocidRange) withTemplate:(strTemplate)) |
079 | end repeat |
080 | |
081 | ############################## |
082 | #NSADATA |
083 | set ocidHTMLData to ocidResponseText's dataUsingEncoding:(refMe's NSUTF8StringEncoding) |
084 | |
085 | ############################## |
086 | #XML |
087 | set ocidOption to (refMe's NSXMLNodePreserveAll) + (refMe's NSXMLDocumentTidyHTML) |
088 | |
089 | set listResponse to refMe's NSXMLDocument's alloc()'s initWithData:(ocidHTMLData) options:(ocidOption) |error| :(reference) |
090 | if (item 2 of listResponse) = (missing value) then |
091 | log "正常処理" |
092 | set ocidXMLDoc to (item 1 of listResponse) |
093 | else if (item 2 of listResponse) ≠ (missing value) then |
094 | log (item 2 of listResponse)'s code() as text |
095 | log (item 2 of listResponse)'s localizedDescription() as text |
096 | log "NSXMLDocumentエラー 警告がありました" |
097 | set ocidXMLDoc to (item 1 of listResponse) |
098 | end if |
099 | |
100 | ############################## |
101 | #次工程に渡すARRAY |
102 | set ocidNodeArray to refMe's NSMutableArray's alloc()'s initWithCapacity:(0) |
103 | #タイプA |
104 | set listResponse to (ocidXMLDoc's nodesForXPath:"//*[@src]/attribute::src" |error| :(reference)) |
105 | if (item 2 of listResponse) = (missing value) then |
106 | log "正常処理" |
107 | set ocidAttarURLArray to (item 1 of listResponse) |
108 | ocidNodeArray's addObjectsFromArray:(ocidAttarURLArray) |
109 | else if (item 2 of listResponse) ≠ (missing value) then |
110 | log (item 2 of listResponse)'s code() as text |
111 | log (item 2 of listResponse)'s localizedDescription() as text |
112 | return "エラーしました" |
113 | end if |
114 | #タイプB |
115 | set listResponse to (ocidXMLDoc's nodesForXPath:"//*[@data-src]/attribute::data-src" |error| :(reference)) |
116 | if (item 2 of listResponse) = (missing value) then |
117 | log "正常処理" |
118 | set ocidAttarURLArray to (item 1 of listResponse) |
119 | ocidNodeArray's addObjectsFromArray:(ocidAttarURLArray) |
120 | else if (item 2 of listResponse) ≠ (missing value) then |
121 | log (item 2 of listResponse)'s code() as text |
122 | log (item 2 of listResponse)'s localizedDescription() as text |
123 | return "エラーしました" |
124 | end if |
125 | |
126 | |
127 | ############################## |
128 | #####Attar to Array |
129 | ############################## |
130 | #次工程用のリスト |
131 | set ocidURLArray to refMe's NSMutableArray's alloc()'s initWithCapacity:(0) |
132 | set ocidURLAllArray to refMe's NSMutableArray's alloc()'s initWithCapacity:(0) |
133 | #次工程用拡張子リスト |
134 | set listExtension to {"jpg", "jpeg", "png", "webp", "gif"} as list |
135 | set ocidExtensionArray to refMe's NSArray's alloc()'s initWithArray:(listExtension) |
136 | #取得したアトリビュートの数だけ繰り返し |
137 | repeat with itemArray in ocidNodeArray |
138 | #テキストにして |
139 | set ocidValue to itemArray's stringValue() |
140 | (ocidURLAllArray's addObject:(ocidValue)) |
141 | set ocidFirstChr to (ocidValue's substringToIndex:(1)) |
142 | set ocidScheme to (ocidValue's substringToIndex:(4)) |
143 | #絶対パスなら |
144 | if (ocidFirstChr as text) is "/" then |
145 | (ocidBaseURLSTR's appendString:(ocidValue)) |
146 | set strValue to ocidBaseURLSTR as text |
147 | #相対パスなら |
148 | else if (ocidScheme as text) is not "http" then |
149 | (ocidBaseURLPATH's appendString:(ocidValue)) |
150 | set strValue to ocidBaseURLPATH as text |
151 | else |
152 | #URLなら |
153 | set strValue to ocidValue as text |
154 | end if |
155 | #テキストをURLにして |
156 | set ocidURLString to (refMe's NSString's stringWithString:(strValue)) |
157 | set ocidValueURL to (refMe's NSURL's alloc()'s initWithString:(ocidURLString)) |
158 | if ocidValueURL ≠ (missing value) then |
159 | #拡張子取得 |
160 | set ocidExtension to ocidValueURL's pathExtension() |
161 | #対象拡張子リストに含まれるなら |
162 | set boolContain to (ocidExtensionArray's containsObject:(ocidExtension)) |
163 | if boolContain is true then |
164 | #クエリーを除去 |
165 | set ocidUrlComponents to (refMe's NSURLComponents's componentsWithURL:(ocidValueURL) resolvingAgainstBaseURL:(false)) |
166 | (ocidUrlComponents's setQuery:(missing value)) |
167 | set ocidImgURL to ocidUrlComponents's |URL|() |
168 | #次工程リストに追加する |
169 | (ocidURLArray's addObject:(ocidImgURL)) |
170 | end if |
171 | end if |
172 | end repeat |
173 | |
174 | ############################## |
175 | #####ダウンロード |
176 | ############################## |
177 | set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDownloadsDirectory) inDomains:(refMe's NSUserDomainMask)) |
178 | set ocidDownloadsDirPathURL to ocidURLsArray's firstObject() |
179 | #フォルダ名はホスト名 |
180 | set ocidHostName to ocidURL's |host|() |
181 | set ocidSaveDirPathURL to ocidDownloadsDirPathURL's URLByAppendingPathComponent:(ocidHostName) |
182 | #フォルダアクセス権 |
183 | set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0 |
184 | ocidAttrDict's setValue:(493) forKey:(refMe's NSFilePosixPermissions) |
185 | #フォルダを作る |
186 | set listDone to appFileManager's createDirectoryAtURL:(ocidSaveDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error| :(reference) |
187 | if (item 1 of listDone) is true then |
188 | log "正常処理" |
189 | else if (item 2 of listDone) ≠ (missing value) then |
190 | log (item 2 of listDone)'s code() as text |
191 | log (item 2 of listDone)'s localizedDescription() as text |
192 | return "フォルダ作成エラーしました" |
193 | end if |
194 | set numCntArray to ocidURLArray's |count|() |
195 | ##URLを順番に処理 |
196 | repeat with itemNo from 0 to (numCntArray - 1) by 1 |
197 | set itemURL to (ocidURLArray's objectAtIndex:(itemNo)) |
198 | set ocidSaveFileName to itemURL's lastPathComponent() |
199 | set ocidSaveFilePathURL to (ocidSaveDirPathURL's URLByAppendingPathComponent:(ocidSaveFileName)) |
200 | #ダウンロード |
201 | set ocidOption to (refMe's NSDataReadingMappedIfSafe) |
202 | set listResponse to (refMe's NSData's alloc()'s initWithContentsOfURL:(itemURL) options:(ocidOption) |error| :(reference)) |
203 | if (item 2 of listResponse) = (missing value) then |
204 | log "正常処理" |
205 | set ocidGetData to (item 1 of listResponse) |
206 | else if (item 2 of listResponse) ≠ (missing value) then |
207 | log (item 2 of listResponse)'s code() as text |
208 | log (item 2 of listResponse)'s localizedDescription() as text |
209 | return "ダウンロードエラーしました" |
210 | end if |
211 | #保存 |
212 | set ocidOption to (refMe's NSDataWritingAtomic) |
213 | set listDone to (ocidGetData's writeToURL:(ocidSaveFilePathURL) options:(ocidOption) |error| :(reference)) |
214 | if (item 1 of listDone) is true then |
215 | log "正常処理" |
216 | else if (item 2 of listDone) ≠ (missing value) then |
217 | log (item 2 of listDone)'s code() as text |
218 | log (item 2 of listDone)'s localizedDescription() as text |
219 | return "ファイル保存エラーしました" |
220 | end if |
221 | end repeat |
222 | |
223 | #保存先を開く |
224 | set appSharedWorkspace to refMe's NSWorkspace's sharedWorkspace() |
225 | set boolDone to appSharedWorkspace's openURL:(ocidSaveDirPathURL) |
226 | if boolDone is true then |
227 | log "正常処理" |
228 | else if boolDone is false then |
229 | set strErrorNO to (item 2 of listDone)'s code() as text |
230 | set strErrorMes to (item 2 of listDone)'s localizedDescription() as text |
231 | refMe's NSLog("■:" & strErrorNO & strErrorMes) |
232 | return "エラーしました" & strErrorNO & strErrorMes |
233 | end if |
234 | |
235 | |
AppleScriptで生成しました |
| 固定リンク