• 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
Playing with ASObjC - Rename PayPal Statement
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Playing with ASObjC - Rename PayPal Statement


  • Subject: Playing with ASObjC - Rename PayPal Statement
  • From: Christopher Stone <email@hidden>
  • Date: Thu, 13 Aug 2015 16:04:59 -0500

Hey Folks,

I finally updated to Yosemite — which is a bit ironical since El Capitan is so close.  Hopefully 10.11.x will solve some of the things I don't like about 10.10.x, and I will probably take the time and effort to clean-install it.

In any case I can play with ASObjC a lot more comfortably now.

I learn best when I can work on projects that interest me or are of use to me, so I'm getting my feet wet by adapting stuff Shane Stanley wrote to my own purposes.

Normally I would have used `pdftotext` and the Satimage.osax for this job, but this was a good opportunity to try out some ASObjC.

I'm very pleased with how smoothly this works on Yosemite (as opposed to Mavericks), and I'm really looking forward to getting comfortable using all the goodies ASObjC provides (despite the rather painful learning curve).

As usual I will post things I think might be educational and/or useful, and since I'm new to ASObjC I hope this will help others who are struggling with it in their quest for competency and beyond.

--
Best Regards,
Chris

-------------------------------------------------------------------------------------------
# Auth: Christopher Stone <email@hidden>
# Auth: ASObjC handlers provided by Shane Stanley
# dCre: 2015/08/12 15:00
# dMod: 2015/08/13 15:15
# Appl: Finder & ASObjC
# Task: Read PayPal Statement and return Statement Date.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Finder, @PayPal, @ASObjC
# Test: OSX 10.10.5
-------------------------------------------------------------------------------------------
use framework "Foundation"
use framework "Quartz"
use scripting additions
-------------------------------------------------------------------------------------------
property displayNameInBBEdit : false
property displayNameInTextEdit : true
property renameFile : false
-------------------------------------------------------------------------------------------

set AppleScript's text item delimiters to {""}
tell application "Finder" to set finderSelection to selection as alias list

if finderSelection ≠ {} then
  set fileAlias to first item of finderSelection
  set posixPath to POSIX path of fileAlias
  set pdfText to my pdf2Text:posixPath

  

  set paypalStatementDate to my regxRepl:"(?s)\\x{FFFC}Account Statement \\| (\\w+) (\\d{4}).*" inString:pdfText usingThis:"$1 01, $2"

  

  tell (date paypalStatementDate)
    set newFileName to ((its year & "-" & text -2 thru -1 of ("0" & (its month as number))) as text) & " » PayPal Statement.pdf"
  end tell

  

  if displayNameInBBEdit then bbeditNewDoc(newFileName, true)
  if displayNameInTextEdit then displayTextInTextEdit(newFileName)
  if renameFile then renameFileInFinder(fileAlias, newFileName)

  

end if

-------------------------------------------------------------------------------------------
--» HANDLERS
-------------------------------------------------------------------------------------------
on bbeditNewDoc(_text, _activate)
  tell application "BBEdit"
    set newDoc to make new document with properties {text:_text, bounds:{0, 44, 1920, 1200}}
    tell newDoc
      select insertion point before its text
    end tell
    if _activate = true or _activate = 1 or _activate = "activate" then activate
  end tell
end bbeditNewDoc
-------------------------------------------------------------------------------------------
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 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:
-------------------------------------------------------------------------------------------
on renameFileInFinder(fileAlias, newFileName)
  if length of newFileName < 50 then
    tell application "Finder" to set name of fileAlias to newFileName
  else
    error "Something is wrong with length of newFileName!"
  end if
end renameFileInFinder
-------------------------------------------------------------------------------------------
on displayTextInTextEdit(_text)
  do shell script "echo " & (quoted form of _text) & " | open -f"
end displayTextInTextEdit
-------------------------------------------------------------------------------------------

 _______________________________________________
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

  • Prev by Date: Re: Yosemite Mail — New Message from Template
  • Next by Date: Saving PDF as RTF in Adobe Acrobat
  • Previous by thread: Re: Yosemite Mail — New Message from Template
  • Next by thread: Saving PDF as RTF in Adobe Acrobat
  • Index(es):
    • Date
    • Thread