• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
File selected Finder item according to WhereFroms MetaData – File PayPal Statement
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

File selected Finder item according to WhereFroms MetaData – File PayPal Statement


  • Subject: File selected Finder item according to WhereFroms MetaData – File PayPal Statement
  • From: Christopher Stone <email@hidden>
  • Date: Thu, 13 Apr 2017 13:49:37 -0500

Hey Folks,

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.

The script looks for a destination like this:

~/Documents/Web Receipts/PayPal Statements/
  ├── ...
  └── 2017

REQUIREMENTS:

Satimage.osax ⇢ http://tinyurl.com/smile-beta-page
pdftotext ⇢ http://www.foolabs.com/xpdf/download.html

I'll post a script here in a but that uses AppleScriptObjC instead of the Satimage.osax and pdftotext.

Enjoy.

--
Best Regards,
Chris

------------------------------------------------------------------------------
# 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
# Reqs: pdftotext Unix executable — http://www.foolabs.com/xpdf/download.html
# 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

if fndBool("https://www.paypal.com.+get-statement", whereFromsMetaDataList, false, false) of me = true then
    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
------------------------------------------------------------------------------

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden

  • Follow-Ups:
    • Re: File selected Finder item according to WhereFroms MetaData – File PayPal Statement
      • From: Christopher Stone <email@hidden>
  • Prev by Date: Re: Automator actions etc.
  • Next by Date: Re: File selected Finder item according to WhereFroms MetaData – File PayPal Statement
  • Previous by thread: Re: Automator actions etc.
  • Next by thread: Re: File selected Finder item according to WhereFroms MetaData – File PayPal Statement
  • Index(es):
    • Date
    • Thread