指定のPDFの全ページを走査してリンクアノテーションのURLのうちURL Schemeが”http”のものを抽出し、”http://piyocast.com/as/archives/” を含むURLを取得するAppleScriptです。
macOS 10.13以降でも動作するようにしてあります。電子書籍のPDFから本Blogへのリンクを張ってある箇所を検出するために作成したものです。
AppleScript名:指定PDFの全ページからリンクアノテーションのURLを取得(http)v2 |
— Created 2017-06-08 by Takaaki Naganoya — Modified 2018-02-14 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.5" use scripting additions use framework "Foundation" use framework "Quartz" property |NSURL| : a reference to current application’s |NSURL| property PDFDocument : a reference to current application’s PDFDocument set aPOSIX to POSIX path of (choose file of type {"com.adobe.pdf"} with prompt "Choose a PDF with Annotation") set linkList to getLinkURLFromPDF(aPOSIX, "http", "http://piyocast.com/as/archives/") of me –> {{pageNum:39, linkURL:"http://piyocast.com/as/archives/69"},….} on getLinkURLFromPDF(aPOSIX, urlScheme, condURL) set v2 to system attribute "sys2" –> macOS 10.12 =12 set aURL to (|NSURL|’s fileURLWithPath:aPOSIX) set aPDFdoc to PDFDocument’s alloc()’s initWithURL:aURL set pCount to aPDFdoc’s pageCount() set outList to {} –PDFのページでループ repeat with ii from 0 to (pCount – 1) set tmpPage to (aPDFdoc’s pageAtIndex:ii) set anoList to (tmpPage’s annotations()) as list if anoList is not equal to {missing value} then –指定PDF中にAnotationが存在した –対象PDFPage内で検出されたAnnotationでループ repeat with i in anoList if v2 < 13 then –to macOS Sierra set aType to (i’s type()) as string else –macOS High Sierra or later set aType to (i’s |Type|()) as string end if — if aType = "Link" then set tmpURL to i’s |URL|() if tmpURL is not equal to missing value then set tmpScheme to (tmpURL’s |scheme|()) as string if tmpScheme = urlScheme then set urlStr to (tmpURL’s absoluteString()) as string if (urlStr contains condURL) then set the end of outList to {pageNum:(ii + 1), linkURL:urlStr} end if end if end if end if end repeat end if end repeat return outList end getLinkURLFromPDF |
More from my site
(Visited 34 times, 1 visits today)