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.
-------------------------------------------------------------------------------------------
# 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
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
-------------------------------------------------------------------------------------------