Safariで表示中のYouTubeムービーのサムネール画像を取得、保存、表示するAppleScriptです。
YouTubeのムービーのサムネール画像の取得方法を確認し、動作確認用にダイアログ表示+画像保存の機能を追加したものです。Script Debugger上で動かしている分には、NSImageの内容を結果表示ビューワで自動表示されますが、ない人向けに付けた機能です。
画像自体は、「ピクチャ」フォルダにUUIDつきでPNG形式で保存します。
–> Download Script bundle with Library
掲載リストには、画像表示ライブラリが含まれていないため、そのままでは実行できません。上記のScript Bundleをダウンロードして実行する必要があります。
AppleScript名:Safariで表示中のYouTubeムービーのサムネイル画像を取得.scptd |
— – Created by: Takaaki Naganoya – Created on: 2022/05/09 — – Copyright © 2022 Piyomaru Software, All Rights Reserved — use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "AppKit" use imgLib : script "imageDisplayLib" property NSUUID : a reference to current application’s NSUUID property |NSURL| : a reference to current application’s |NSURL| property NSString : a reference to current application’s NSString property NSImage : a reference to current application’s NSImage property NSPNGFileType : a reference to current application’s NSPNGFileType property NSURLComponents : a reference to current application’s NSURLComponents property NSBitmapImageRep : a reference to current application’s NSBitmapImageRep property NSMutableDictionary : a reference to current application’s NSMutableDictionary tell application "Safari" tell front document try set aURL to URL on error set aURL to "https://www.youtube.com/watch?v=_fmDtIV9vvI" end try end tell end tell if aURL does not start with "https://www.youtube.com/watch?" then return set urlDict to parseURLParamsAsDict(aURL) of me set aParam to urlDict’s valueForKey:"v" if aParam = missing value then return set imgURL to "https://i1.ytimg.com/vi/" & (aParam as string) & "/mqdefault.jpg" set newURL to |NSURL|’s URLWithString:imgURL set aImg to NSImage’s alloc()’s initWithContentsOfURL:newURL set imgPath to (POSIX path of (path to pictures folder) & ((aParam as string) & "_") & (NSUUID’s UUID()’s UUIDString()) as string) & ".png" –Save saveNSImageAtPathAsPNG(aImg, imgPath) of me –Display dispImage(aImg, "YouTube thumbnail") of imgLib on parseURLParamsAsDict(aURL) set components to NSURLComponents’s alloc()’s initWithString:aURL set qList to (components’s query())’s componentsSeparatedByString:"&" set paramRec to NSMutableDictionary’s dictionary() repeat with i in qList set keyAndValues to (i’s componentsSeparatedByString:"=") (paramRec’s setObject:(keyAndValues’s objectAtIndex:1) forKey:(keyAndValues’s objectAtIndex:0)) end repeat return paramRec end parseURLParamsAsDict –NSImageを指定パスにPNG形式で保存 on saveNSImageAtPathAsPNG(anImage, outPath) set imageRep to anImage’s TIFFRepresentation() set aRawimg to NSBitmapImageRep’s imageRepWithData:imageRep set pathString to NSString’s stringWithString:outPath set newPath to pathString’s stringByExpandingTildeInPath() set myNewImageData to (aRawimg’s representationUsingType:(NSPNGFileType) |properties|:(missing value)) set aRes to (myNewImageData’s writeToFile:newPath atomically:true) as boolean return aRes –成功ならtrue、失敗ならfalseが返る end saveNSImageAtPathAsPNG |
More from my site
(Visited 135 times, 1 visits today)