PDFを一括ダウンロードしてEvernoteに取り込む
行政系のWebサイトに行くとPDFへのリンクてんこ盛り。いちいちダウンロードしてEvernoteに取り込むのが面倒。自動化するスクリプトを書いてみた。Safariで動作する。
-- You may modify following variables
set dlFolName to "ever" -- temporary folder inside Safari's download folder
set titleName to "nothing" -- default name of new note will be replaced by Title name of web document
set defaultText to "" -- default text inserted when creating new note
set dlFol to getSafariDownloadFolder() of me
set targets to {}
tell application "Finder"
try
set dlFol to make folder at dlFol with properties {name:dlFolName}
on error
set dlFol to dlFol & dlFolName & ":"
end try
end tell
tell application "Safari"
activate
set counter to 0
-- the number of links in the front document
set the_link_number to (do JavaScript "document.links.length" in document 1)
set the_URL to URL of document 1
set titleName to the name of document 1
repeat the_link_number times
-- getting the target URL of the first link of the front document
set the_link to do JavaScript "document.links[" & counter & "].href" in document 1
-- display dialog the_link
if the_link ends with ".pdf" then
set aFileName to getFNfromURL(the_link) of me
set dlPath to (dlFol as string) & aFileName
try
with timeout of 3600 seconds
tell application "URL Access Scripting"
activate
download the_link to file dlPath with progress
-- display dialog dlPath
set the end of targets to dlPath
end tell
end timeout
on error
-- display dialog "Download Error"
end try
end if
set counter to counter + 1
end repeat
end tell
tell application "Evernote"
set new_note to create note title titleName with text defaultText attachments targets
-- set new_tag to make tag with properties {name:myTag}
-- assign new_tag to new_note
set source URL of new_note to the_URL
end tell
-- clean up temp files
set tmpDir to (POSIX path of (dlFol as string)) & "*.pdf"
try
do shell script "rm " & tmpDir
end try
-- Reference http://piyocast.com/as/archives/722
-- Safariのダウンロードフォルダを取得
-- By Phillip Aker, Mark J Reed
on getSafariDownloadFolder()
set preferencesPath to POSIX path of (path to preferences)
set bundleId to bundle identifier of (info for (path to application "Safari"))
set safariPrefsFile to preferencesPath & bundleId & ".plist"
tell application "System Events" to set folderName to get value of property list item "DownloadsPath" of property list file safariPrefsFile
set downloadFolder to (POSIX file (do shell script "echo " & folderName)) as alias
return downloadFolder
end getSafariDownloadFolder
-- URLからファイル名のみを抽出
on getFNfromURL(aURL)
if aURL ends with "/" then
display dialog "与えられたURLの末尾がスラッシュで終了しているため、ファイルではありません。" buttons {"OK"} default button 1
return ""
end if
set aLen to length of aURL
set aR to reverse of (characters of aURL)
set aNstr to aR as string
set aLoc to offset of "/" in aNstr
set aRes to text (aLen - aLoc + 2) thru -1 of aURL
return aRes
end getFNfromURL