A while back I posted a script that renames a selected PDF file by reading its content and finding the relevant name bits.
I have a general purpose filing script for the Finder that looks at the single selected file in a variety of ways and moves it to the appropriate place if it matches a set of rules.
At the moment I'm playing with the WhereFroms MetaData Safari adds to downloaded items.
This script is specific to my PayPal Statements.
Enjoy.
------------------------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2017/04/13 10:00
# dMod: 2017/04/13 10:33
# Appl: Finder
# Task: File selected item according to WhereFroms MetaData – File PayPal Statement.
# Libs: None
# Osax: Satimage.osax
# Tags: @Applescript, @Script, @ASObjC, @Satimage.osax, @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 whereFromsMetaDataList to join whereFromsMetaDataList using linefeed
set shCmd to "
export PATH=/opt/local/bin:/opt/local/sbin:/usr/local/bin:$PATH
pdftotext -layout " & qf(posixPath) & " - | egrep -i 'Account Statement.+[0-9]{4}'
"
set statementDate to do shell script shCmd
set statementYear to fnd("\\d{4}", statementDate, false, true) of me
set currentStatementFolder to (paypalStatementBaseFolderPath & statementYear) as alias
tell application "Finder"
set newStatementLocation to move fileAlias to currentStatementFolder
reveal newStatementLocation
end tell
end if
------------------------------------------------------------------------------
--» HANDLERS
------------------------------------------------------------------------------
on fnd(_find, _data, _all, strRslt)
try
find text _find in _data all occurrences _all string result strRslt with regexp without case sensitive
on error
return false
end try
end fnd
------------------------------------------------------------------------------
on fndBool(_find, _data, _all, strRslt)
try
find text _find in _data all occurrences _all string result strRslt with regexp without case sensitive
return true
on error
return false
end try
end fndBool
------------------------------------------------------------------------------
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 qf(_text)
return (quoted form of _text)
end qf
------------------------------------------------------------------------------