指定のPDFの全ページを走査してリンクアノテーションのURLのうちURL Schemeが”applescript”のものを抽出して、Link Scriptの内容を取得するAppleScriptです。
macOS 10.13以降でも動作するようにしてあります。
AppleScript名:指定PDFの全ページからリンクアノテーションのURLを取得(applescript) 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 NSString : a reference to current application’s NSString property PDFDocument : a reference to current application’s PDFDocument property NSMutableDictionary : a reference to current application’s NSMutableDictionary property NSUTF8StringEncoding : a reference to current application’s NSUTF8StringEncoding 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, "applescript") of me –> {{pageNum:95, linkScript:"set aStr to \"ぴよまるソフトウェア\" set aPath to choose file name……}} on getLinkURLFromPDF(aPOSIX, urlScheme) 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 set urlRec to parseQueryDictFromURLString(urlStr) of me set tmpScript to (urlRec’s |script|) as string set the end of outList to {pageNum:(ii + 1), linkScript:tmpScript} end if end if end if end repeat end if end repeat return outList end getLinkURLFromPDF on parseQueryDictFromURLString(aURLStr as string) if aURLStr = "" then error "No URL String" set aURL to |NSURL|’s URLWithString:aURLStr set aQuery to aURL’s query() –Get Query string part from URL if aQuery’s |length|() = 0 then return false set aDict to NSMutableDictionary’s alloc()’s init() set aParamList to (aQuery’s componentsSeparatedByString:"&") as list repeat with i in aParamList set j to contents of i if length of j > 0 then set tmpStr to (NSString’s stringWithString:j) set eList to (tmpStr’s componentsSeparatedByString:"=") set anElement to (eList’s firstObject()’s stringByReplacingPercentEscapesUsingEncoding:(NSUTF8StringEncoding)) set aValStr to (eList’s lastObject()’s stringByReplacingPercentEscapesUsingEncoding:(NSUTF8StringEncoding)) (aDict’s setObject:aValStr forKey:anElement) end if end repeat return aDict end parseQueryDictFromURLString |
More from my site
(Visited 21 times, 1 visits today)