Here it is.
NOTE – neither the first script nor this one ensures the existence of the destination folder. I leave that and other niceties as exercises for the reader.
Enjoy.
------------------------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2017/04/13 10:00
# dMod: 2017/04/13 13:51
# Appl: Finder
# Task: File selected item according to WhereFroms MetaData – File PayPal Statement – ASObjC Edition.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @ASObjC, @File, @Selected, @Finder, @Item, @WhereFroms, @MetaData, @PayPal, @Statement
------------------------------------------------------------------------------
use AppleScript version "2.4"
use framework "Foundation"
use scripting additions
------------------------------------------------------------------------------
set paypalStatementBaseFolderPath to ((path to documents folder as text) & "Web Receipts:PayPal Statements:")
tell application "Finder" to set finderSelectionList to selection as alias list
if length of finderSelectionList = 0 then
error "No files were selected in the Finder!"
else if length of finderSelectionList > 1 then
error "Too many files were selected in the Finder!"
end if
set fileAlias to item 1 of finderSelectionList
set posixPath to POSIX path of (item 1 of finderSelectionList)
set whereFromsMetaDataList to getWhereFromsMetaData(posixPath)
if whereFromsMetaDataList = {missing value} then error "No WhereFroms MetaData was found!"
set AppleScript's text item delimiters to linefeed
set whereFromsMetaDataList to whereFromsMetaDataList as text
if matchFound ≠ {} then
set pdfText to its pdf2Text:posixPath
end if
set matchFound to its reMatch:"(?m)Account Statement[^\\n\\r]*[0-9]{4}" inText:pdfText
if matchFound ≠ {} then
set matchFound to matchFound as text
set statementYear to its reMatch:"[0-9]{4}" inText:matchFound
if length of statementYear = 1 then
set statementYear to item 1 of statementYear
set currentStatementFolder to (paypalStatementBaseFolderPath & statementYear) as alias
tell application "Finder"
set newStatementLocation to move fileAlias to currentStatementFolder
reveal newStatementLocation
end tell
end if
end if
------------------------------------------------------------------------------
--» HANDLERS
------------------------------------------------------------------------------
on getWhereFromsMetaData(thePosixPath)
set anNSURL to current application's class "NSURL"'s fileURLWithPath:thePosixPath
set theNSMetadataItem to current application's NSMetadataItem's alloc()'s initWithURL:anNSURL
set whereFromsList to theNSMetadataItem's valueForAttribute:"kMDItemWhereFroms"
set whereFromsList to whereFromsList as list
return whereFromsList
end getWhereFromsMetaData
------------------------------------------------------------------------------
on pdf2Text:thePath
set theText to current application's NSMutableString's |string|()
set anNSURL to current application's |NSURL|'s fileURLWithPath:thePath
set theDoc to current application's PDFDocument's alloc()'s initWithURL:anNSURL
set theCount to theDoc's pageCount() as integer
repeat with i from 1 to theCount
set thePage to (theDoc's pageAtIndex:(i - 1))
(theText's appendString:(thePage's |string|()))
end repeat
return theText as text
end pdf2Text:
------------------------------------------------------------------------------
on reMatch:findPattern inText:theText
set theNSString to current application's NSString's stringWithString:theText
set theOptions to ((current application's NSRegularExpressionDotMatchesLineSeparators) as integer) + ((current application's NSRegularExpressionAnchorsMatchLines) as integer)
set theRegEx to current application's NSRegularExpression's regularExpressionWithPattern:findPattern options:theOptions |error|:(missing value)
set theFinds to theRegEx's matchesInString:theNSString options:0 range:{location:0, |length|:theNSString's |length|()}
set theFinds to theFinds as list -- so we can loop through
set theResult to {} -- we will add to this
repeat with i from 1 to count of items of theFinds
set theRange to (item i of theFinds)'s range()
set end of theResult to (theNSString's substringWithRange:theRange) as string
end repeat
return theResult
end reMatch:inText:
------------------------------------------------------------------------------
on regxRepl:_find inString:pdfText usingThis:_replace
set theRegEx to current application's NSRegularExpression's regularExpressionWithPattern:_find options:0 |error|:(missing value)
set theResult to theRegEx's stringByReplacingMatchesInString:pdfText options:0 range:{location:0, |length|:length of pdfText} withTemplate:_replace
return theResult as text
end regxRepl:inString:usingThis:
------------------------------------------------------------------------------