« [StandardAdditions] system attribute | トップページ | [HTML解析]HTMLからURLを収集する(今一歩) »

HTMLから画像を収集する(期待はずれ)


あくまでも参考にしてください

【スクリプトエディタで開く】 |

サンプルソース(参考)
行番号ソース
001#! /usr/bin/env osascript
002----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
003#
004(*
005BASE SCRIPT
006https://forum.latenightsw.com/t/list-of-links-on-webpage/1328
007
008WEBページの画像のURLを取得する
009サインインが必要なページは取得できない
010CSS指定の画像は取得できない等
011あまり実用的ではなかった…トホホ
012CURLでHTML取得してファイルを解析した方が良さそう
013*)
014#com.cocolog-nifty.quicktimer.icefloe
015----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
016use AppleScript version "2.8"
017use framework "Foundation"
018use framework "AppKit"
019use scripting additions
020property refMe : a reference to current application
021set appFileManager to refMe's NSFileManager's defaultManager()
022
023set strMes to ("WEBページのURLをペーストしてください") as text
024
025########################
026## クリップボードの中身取り出し
027########################
028###初期化
029set appPasteboard to refMe's NSPasteboard's generalPasteboard()
030##格納されているタイプをリストにして
031set ocidPastBoardTypeArray to appPasteboard's types
032###テキストがあれば
033set boolContain to ocidPastBoardTypeArray's containsObject:("public.utf8-plain-text")
034if (boolContain as boolean) is true then
035  ###値を格納する
036  tell application "Finder"
037    set strReadString to (the clipboard as text) as text
038  end tell
039else
040  ###UTF8が無いなら
041  ##テキスト形式があるか?確認して
042  set boolContain to ocidPastBoardTypeArray's containsObject:(refMe's NSPasteboardTypeString)
043  ##テキスト形式があるなら
044  if (boolContain as boolean) is true then
045    set ocidTypeClassArray to refMe's NSMutableArray's alloc()'s initWithCapacity:(0)
046    ocidTypeClassArray's addObject:(refMe's NSString)
047    set ocidReadString to appPasteboard's readObjectsForClasses:(ocidTypeClassArray) options:(missing value)
048    set strReadString to ocidReadString as text
049  else
050    log "テキストなし"
051    set strReadString to strMes as text
052  end if
053end if
054##############################
055#####ダイアログ
056##############################
057###ダイアログを前面に出す
058set strName to (name of current application) as text
059if strName is "osascript" then
060  tell application "Finder" to activate
061else
062  tell current application to activate
063end if
064set aliasIconPath to POSIX file "/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/BookmarkIcon.icns" as alias
065try
066  set recordResult to (display dialog strMes with title "入力してください" default answer strReadString buttons {"OK", "キャンセル"} default button "OK" with icon aliasIconPath giving up after 20 without hidden answer) as record
067on error
068  log "エラーしました"
069  return
070end try
071if "OK" is equal to (button returned of recordResult) then
072  set strReturnedText to (text returned of recordResult) as text
073else if (gave up of recordResult) is true then
074  return "時間切れです"
075else
076  return "キャンセル"
077end if
078##############################
079#####戻り値整形
080##############################
081set ocidResponseText to (refMe's NSString's stringWithString:(strReturnedText))
082###タブと改行を除去しておく
083set ocidTextM to refMe's NSMutableString's alloc()'s initWithCapacity:(0)
084ocidTextM's appendString:(ocidResponseText)
085###除去する文字列リスト
086set listRemoveChar to {"\n", "\r", "\t"} as list
087##置換
088repeat with itemChar in listRemoveChar
089  set strPattern to itemChar as text
090  set strTemplate to ("") as text
091  set ocidOption to (refMe's NSRegularExpressionCaseInsensitive)
092  set listResponse to (refMe's NSRegularExpression's regularExpressionWithPattern:(strPattern) options:(ocidOption) |error| :(reference))
093  if (item 2 of listResponse) ≠ (missing value) then
094    log (item 2 of listResponse)'s localizedDescription() as text
095    return "正規表現パターンに誤りがあります"
096  else
097    set ocidRegex to (item 1 of listResponse)
098  end if
099  set numLength to ocidResponseText's |length|()
100  set ocidRange to refMe's NSRange's NSMakeRange(0, numLength)
101  set ocidResponseText to (ocidRegex's stringByReplacingMatchesInString:(ocidResponseText) options:0 range:(ocidRange) withTemplate:(strTemplate))
102end repeat
103##############################
104#####URL
105##############################
106#URL
107set ocidURL to refMe's NSURL's alloc()'s initWithString:(ocidResponseText)
108#コンポーネント
109set ocidURLcomp to refMe's NSURLComponents's alloc()'s init()
110ocidURLcomp's setScheme:(ocidURL's |scheme|())
111ocidURLcomp's setHost:(ocidURL's |host|())
112set ocidBaseURL to ocidURLcomp's |URL|()
113#スキーム+ホストのURL
114set ocidBaseURLSTR to refMe's NSMutableString's alloc()'s initWithString:(ocidBaseURL's absoluteString())
115ocidURLcomp's |path|()
116set ocidBaseURL to ocidURLcomp's |URL|()
117#パスまで入れたURL
118set ocidBaseURLPATH to refMe's NSMutableString's alloc()'s initWithString:(ocidBaseURL's absoluteString())
119
120
121##############################
122#####DATA
123##############################
124set ocidOption to (refMe's NSDataReadingMappedIfSafe)
125set listResponse to refMe's NSData's alloc()'s initWithContentsOfURL:(ocidURL) options:(ocidOption) |error| :(reference)
126if (item 2 of listResponse) = (missing value) then
127  log "正常処理"
128  set ocidHTMLData to (item 1 of listResponse)
129else if (item 2 of listResponse) ≠ (missing value) then
130  log (item 2 of listResponse)'s code() as text
131  log (item 2 of listResponse)'s localizedDescription() as text
132  return "NSDATAエラーしました"
133end if
134##############################
135#####XML
136##############################
137set ocidOption to (refMe's NSXMLNodePreserveAll) + (refMe's NSXMLDocumentTidyHTML)
138
139set listResponse to refMe's NSXMLDocument's alloc()'s initWithData:(ocidHTMLData) options:(ocidOption) |error| :(reference)
140if (item 2 of listResponse) = (missing value) then
141  log "正常処理"
142  set ocidXMLDoc to (item 1 of listResponse)
143else if (item 2 of listResponse) ≠ (missing value) then
144  log (item 2 of listResponse)'s code() as text
145  log (item 2 of listResponse)'s localizedDescription() as text
146  log "NSXMLDocumentエラー 警告がありました"
147  set ocidXMLDoc to (item 1 of listResponse)
148end if
149##############################
150#####Attar to Array
151##############################
152#次工程に渡すARRAY
153set ocidNodeArray to refMe's NSMutableArray's alloc()'s initWithCapacity:(0)
154#タイプA
155set listResponse to (ocidXMLDoc's nodesForXPath:"//*[@src]/attribute::src"  |error| :(reference))
156if (item 2 of listResponse) = (missing value) then
157  log "正常処理"
158  set ocidAttarURLArray to (item 1 of listResponse)
159  ocidNodeArray's addObjectsFromArray:(ocidAttarURLArray)
160else if (item 2 of listResponse) ≠ (missing value) then
161  log (item 2 of listResponse)'s code() as text
162  log (item 2 of listResponse)'s localizedDescription() as text
163  return "エラーしました"
164end if
165#タイプB
166(*
167set listResponse to (ocidXMLDoc's nodesForXPath:"//*[@href]/attribute::href" |error| :(reference))
168if (item 2 of listResponse) = (missing value) then
169  log "正常処理"
170  set ocidAttarURLArray to (item 1 of listResponse)
171  ocidNodeArray's addObjectsFromArray:(ocidAttarURLArray)
172else if (item 2 of listResponse) ≠ (missing value) then
173  log (item 2 of listResponse)'s code() as text
174  log (item 2 of listResponse)'s localizedDescription() as text
175  return "エラーしました"
176end if
177*)
178##############################
179#####Attar to Array
180##############################
181#次工程用のリスト
182set ocidURLArray to refMe's NSMutableArray's alloc()'s initWithCapacity:(0)
183set ocidURLAllArray to refMe's NSMutableArray's alloc()'s initWithCapacity:(0)
184#次工程用拡張子リスト
185set listExtension to {"jpg", "jpeg", "png", "webp", "gif"} as list
186set ocidExtensionArray to refMe's NSArray's alloc()'s initWithArray:(listExtension)
187#取得したアトリビュートの数だけ繰り返し
188repeat with itemArray in ocidNodeArray
189  #テキストにして
190  set ocidValue to itemArray's stringValue()
191  (ocidURLAllArray's addObject:(ocidValue))
192  set ocidFirstChr to (ocidValue's substringToIndex:(1))
193  set ocidScheme to (ocidValue's substringToIndex:(4))
194  #絶対パスなら
195  if (ocidFirstChr as text) is "/" then
196    (ocidBaseURLSTR's appendString:(ocidValue))
197    set strValue to ocidBaseURLSTR as text
198    #相対パスなら
199  else if (ocidScheme as text) is not "http" then
200    (ocidBaseURLPATH's appendString:(ocidValue))
201    set strValue to ocidBaseURLPATH as text
202  else
203    #URLなら
204    set strValue to ocidValue as text
205  end if
206  log strValue
207  #テキストをURLにして
208  set ocidURLString to (refMe's NSString's stringWithString:(strValue))
209  set ocidValueURL to (refMe's NSURL's alloc()'s initWithString:(ocidURLString))
210  if ocidValueURL ≠ (missing value) then
211    #拡張子取得
212    set ocidExtension to ocidValueURL's pathExtension()
213    #対象拡張子リストに含まれるなら
214    set boolContain to (ocidExtensionArray's containsObject:(ocidExtension))
215    if boolContain is true then
216      #次工程リストに追加する
217      (ocidURLArray's addObject:(ocidValueURL))
218    end if
219  end if
220end repeat
221
222##############################
223#####確認用 + テキストリストに
224##############################
225#数を数えて
226set numCntArray to ocidURLArray's |count|() as integer
227#取得出来なかったら
228if numCntArray = 0 then
229  #改行区切りのテキストにして
230  set ocidOutPutString to ocidURLAllArray's componentsJoinedByString:("\n")
231  set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDownloadsDirectory) inDomains:(refMe's NSUserDomainMask))
232  set ocidDownloadsDirPathURL to ocidURLsArray's firstObject()
233  #フォルダ名はホスト名
234  set ocidHostName to ocidURL's |host|()
235  set ocidSaveDirPathURL to ocidDownloadsDirPathURL's URLByAppendingPathComponent:(ocidHostName)
236  #フォルダアクセス権
237  set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
238  ocidAttrDict's setValue:(493) forKey:(refMe's NSFilePosixPermissions)
239  #フォルダを作る
240  set listDone to appFileManager's createDirectoryAtURL:(ocidSaveDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error| :(reference)
241  if (item 1 of listDone) is true then
242    log "正常処理"
243  else if (item 2 of listDone) ≠ (missing value) then
244    log (item 2 of listDone)'s code() as text
245    log (item 2 of listDone)'s localizedDescription() as text
246    return "フォルダ作成エラーしました"
247  end if
248  #テキスト保存ファイル
249  set ocidSaveFilePathURL to ocidSaveDirPathURL's URLByAppendingPathComponent:("urllist.txt")
250  set listDone to ocidOutPutString's writeToURL:(ocidSaveFilePathURL) atomically:(true) encoding:(refMe's NSUTF8StringEncoding) |error| :(reference)
251  if (item 1 of listDone) is true then
252    log "正常処理"
253  else if (item 2 of listDone) ≠ (missing value) then
254    log (item 2 of listDone)'s code() as text
255    log (item 2 of listDone)'s localizedDescription() as text
256    return "保存でエラーしました"
257  end if
258  #保存先を開く
259  set appSharedWorkspace to refMe's NSWorkspace's sharedWorkspace()
260  set boolDone to appSharedWorkspace's openURL:(ocidSaveDirPathURL)
261  
262  return "収集できませんでした"
263end if
264repeat with itemNo from 0 to (numCntArray - 1) by 1
265  log (ocidURLArray's objectAtIndex:(itemNo))'s absoluteString() as text
266end repeat
267#リストを改行区切りのテキストに
268set ocidOutPutString to ocidURLArray's componentsJoinedByString:("\n")
269
270##############################
271#####ダウンロード
272##############################
273set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDownloadsDirectory) inDomains:(refMe's NSUserDomainMask))
274set ocidDownloadsDirPathURL to ocidURLsArray's firstObject()
275#フォルダ名はホスト名
276set ocidHostName to ocidURL's |host|()
277set ocidSaveDirPathURL to ocidDownloadsDirPathURL's URLByAppendingPathComponent:(ocidHostName)
278#フォルダアクセス権
279set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
280ocidAttrDict's setValue:(493) forKey:(refMe's NSFilePosixPermissions)
281#フォルダを作る
282set listDone to appFileManager's createDirectoryAtURL:(ocidSaveDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error| :(reference)
283if (item 1 of listDone) is true then
284  log "正常処理"
285else if (item 2 of listDone) ≠ (missing value) then
286  log (item 2 of listDone)'s code() as text
287  log (item 2 of listDone)'s localizedDescription() as text
288  return "フォルダ作成エラーしました"
289end if
290
291##URLを順番に処理
292repeat with itemNo from 0 to (numCntArray - 1) by 1
293  set itemURL to (ocidURLArray's objectAtIndex:(itemNo))
294  set ocidSaveFileName to itemURL's lastPathComponent()
295  set ocidSaveFilePathURL to (ocidSaveDirPathURL's URLByAppendingPathComponent:(ocidSaveFileName))
296  #ダウンロード
297  set ocidOption to (refMe's NSDataReadingMappedIfSafe)
298  set listResponse to (refMe's NSData's alloc()'s initWithContentsOfURL:(itemURL) options:(ocidOption) |error| :(reference))
299  if (item 2 of listResponse) = (missing value) then
300    log "正常処理"
301    set ocidGetData to (item 1 of listResponse)
302  else if (item 2 of listResponse) ≠ (missing value) then
303    log (item 2 of listResponse)'s code() as text
304    log (item 2 of listResponse)'s localizedDescription() as text
305    return "ダウンロードエラーしました"
306  end if
307  #保存
308  set ocidOption to (refMe's NSDataWritingAtomic)
309  set listDone to (ocidGetData's writeToURL:(ocidSaveFilePathURL) options:(ocidOption) |error| :(reference))
310  if (item 1 of listDone) is true then
311    log "正常処理"
312  else if (item 2 of listDone) ≠ (missing value) then
313    log (item 2 of listDone)'s code() as text
314    log (item 2 of listDone)'s localizedDescription() as text
315    return "ファイル保存エラーしました"
316  end if
317end repeat
318
319##############################
320#####ダイアログ
321##############################
322tell current application
323  set strName to name as text
324end tell
325####スクリプトメニューから実行したら
326if strName is "osascript" then
327  tell application "Finder"
328    activate
329  end tell
330else
331  tell current application
332    activate
333  end tell
334end if
335set strMes to (numCntArray & "件収集しました") as text
336try
337  set recordResult to (display dialog strMes with title "戻り値です" default answer (ocidOutPutString as text) buttons {"クリップボードにコピー", "終了", "再実行"} default button "再実行" cancel button "終了" giving up after 20 with icon aliasIconPath without hidden answer) as record
338on error
339  return "エラーしました"
340end try
341if (gave up of recordResult) is true then
342  return "時間切れです"
343end if
344##############################
345#####自分自身を再実行
346##############################
347if button returned of recordResult is "再実行" then
348  tell application "Finder"
349    set aliasPathToMe to (path to me) as alias
350  end tell
351  run script aliasPathToMe with parameters "再実行"
352end if
353##############################
354#####値のコピー
355##############################
356if button returned of recordResult is "クリップボードにコピー" then
357  try
358    set strText to text returned of recordResult as text
359    ####ペーストボード宣言
360    set appPasteboard to refMe's NSPasteboard's generalPasteboard()
361    set ocidText to (refMe's NSString's stringWithString:(strText))
362    appPasteboard's clearContents()
363    appPasteboard's setString:(ocidText) forType:(refMe's NSPasteboardTypeString)
364  on error
365    tell application "Finder"
366      set the clipboard to strText as text
367    end tell
368  end try
369end if
370#保存先を開く
371set appSharedWorkspace to refMe's NSWorkspace's sharedWorkspace()
372set boolDone to appSharedWorkspace's openURL:(ocidSaveDirPathURL)
373
374return 0
AppleScriptで生成しました

|

« [StandardAdditions] system attribute | トップページ | [HTML解析]HTMLからURLを収集する(今一歩) »

AppleScript HTML」カテゴリの記事