Download Bank Statement Almost Working

No I didn't. But I do now. :smile:
So I ran it and got an interesting reply:

tell current application
	path to desktop as text
		--> "Mac HD:Users:jon:Desktop:"
	beep
end tell
tell application "Finder"
	display dialog "File alias Mac HD:Users:jon:Desktop:Financial_Statement_Destination_Folder: wasn’t found.

Num: -43" with title "ERROR!" buttons {"Copy Error Message", "Cancel", "OK"} default button "OK" giving up after 30
		--> error number -1708
	«event ascrgdut»
		--> error number -1708
	display dialog "File alias Mac HD:Users:jon:Desktop:Financial_Statement_Destination_Folder: wasn’t found.

Num: -43" with title "ERROR!" buttons {"Copy Error Message", "Cancel", "OK"} default button "OK" giving up after 30
		--> {button returned:"Copy Error Message", gave up:false}
end tell

So I assume from this I need to insert my own folder name which is really a folder that exists on my drive somewhere, would that be correct?

I should also add that I am saving everything to another mac in the house so...

I am wondering if it would be better to save everything to my local machine then either setup a watch folder script or add a mount volume statement which checks to see if the volume is mounted and if it is, saves it to the desired folder? I mention this because I end up waiting many times when I navigate to the folder on the other machine while it polls to list the files in each folder I have drilled down into. If the folder is several levels deep, sometimes I wait for each one quite a while.

I read the thread and downloaded your bash script along with the KM macro mentioned in the thread. So from what I gather instead of my Keyboard Shortcut (^P) for printing to PDF, I can execute your Keyboard Shortcut instead and everything will print to a desired name folder in my Downloads folder instead. I am slightly confused here, it appears you have two different Print workflows; one that prints to a folder and the other that prints to the Web Receipts folder. Did I miss something?

Hey John,

In the Bash script you need to define your own location.

Keep in mind that those are examples — not specific solutions for you.

It's been a while since I wrote that stuff. I'll eyeball it again and see if I can make things more clear.

-Chris

Chris,
I don’t want to be the newbie hanging on your apron strings. :wink:
If you have time that’s great and there’s certainly no hurry but if I can sort it out I will report back in case someone has that issue. We all have to earn our own stripes one way or another. No pain, no gain. Heh!

Hey John,

Your log shows the script erroring-out because there is no Financial_Statement_Destination_Folder folder on your Desktop.

You have to provide the proper location for the script to search.

You also have to provide the unique initial name of the files whatever that is. Like:

Bank Statement 01.pdf

The unique part would be "Bank Statement".

When working with scripts in the Script Editor you can comment-out lines using -- or # to keep them from running.

You can use block comments as well:

(*
The text you want not to run.
*)

OR you can just remove text from the script and run the partial script.

Beware of commenting-out essential parts of blocks though like tell...end tell or if... end if. If you comment out one part of the block and not the other then you'll create an error.

That's debugging 101.

Of course you need to learn a bit about what you're doing, so you can read scripts with an educated eye. They tend to look like so much Greek to begin with, but once you get a little bit of understanding you can puzzle-out what a script is doing — even when you don't completely understand the specifics.

Make sure you test scripts on TEST folders — NOT important data folders.

Once you get a script working satisfactorily you can work with real data.

Use copies of the real thing, so you don't make mistakes making mockups.

-Chris

Hi Chris,
Thanks for the feedback on the script. The "file alias mac" thing was probably a paste error when I copied it or maybe something from TextWrangler.

How does this work if I change the file path and folder location? What if it's on another computer?

Hey John,

Humph. That part was supposed to be deleted from my post, because I figured out where it came from. It was just the way the Script Editor was logging. I edited it out of the post just now.

You have to do something like this:

tell application "Finder"
  make new folder at desktop with properties {name:"JunkMe!.ccs"}
end tell

set myFolder to alias ((path to desktop as text) & "JunkMe!.ccs:")

I'm using the Desktop as a known anchor, and that will work on any system.

There are many path-to specifications. Many of them are listed in the Applescript Language Guide, but all should be listed in the afdr enumeration in the AppleScript dictionary of the StandardAdditons osax.

-Chris

Hey John,

I forgot to ask — what version of OSX are you using?

-ccs

10.9… Still haven’t gone to 10.10.

So my download script for credit card statements was working on one site but this month it didn’t fire off. At some point I must have disabled some steps and got distracted and never re-enabled them.

I set it up to work again and tested it. It seems to be back online.

I am going to see if it fires off next time but that’s a month away. In the meantime I thought I would add some more actions to the workflow. After the statement downloads the file is named "Financial_Institution_Statement. I want to scrape the date inside the .PDF statement and insert it in the name of the downloaded statement.

I want to append the scraped date to the beginning of the name as in:
"[MM]-[DD]-[YYYY} Financial_Institution_Name"
Is there a way to do that with KM?

Since the file is pre-named partially correct, I might also utilize a FIND to locate it. Then in the next step rename it using the date in front.


Moderator's Note 2022/11/19 14:09 CST:

Carsten Blüm's version of pdftotext is no longer available, but you can find a working version here.

See this section:

  • Download the Xpdf command line tools:
    • Mac 64-bit: download (GPG signature)

Hey John,

If you were on 10.10.x it would be fairly easy with ASObjC.

Since you're on 10.9.x I'd install this little Unix executable:

http://www.bluem.net/files/pdftotext.dmg?rev=0

The package installs it here:

/usr/local/bin/pdftotext

Invoke it in an Execute Shell Script action like this:

/usr/local/bin/pdftotext -layout -

Note the dash at the end — this is vital, because it caused the output to to go StdOUT instead of a file.

A working example on my system:

export PATH=/opt/local/bin:/opt/local/sbin:/usr/local/bin:$PATH;
pdftotext -layout "/Users/chris/test_directory/pdf_test_files/test.pdf" -

Turning it into an AppleScript that works on the selected file:

tell application "Finder" to set finderSelectionList to selection as alias list

if finderSelectionList ≠ {} then
	set posixFilePath to POSIX path of (first item of finderSelectionList)

	set shCMD to text 2 thru -2 of ("
		export PATH=/opt/local/bin:/opt/local/sbin:/usr/local/bin:$PATH;
		pdftotext -layout " & (quoted form of posixFilePath) & " - 
	")
	
	set pdfText to do shell script shCMD
	
end if

You can see what the output looks like by running this in the Terminal:

pdftotext -layout -

Make sure the trailing dash is on the end of the command, and run it.

-Chris

2 Likes

Love this Chris!

Since you mentioned ASObjC, are you using ASObjC Explorer 4?
It looks pretty nifty for debugging.

Hey John,

I do own a copy.

I also own Script Debugger, and I use that for regular AppleScript. (SD-5 doesn't support ASObjC though.)

-Chris

1 Like

Something tells me I should look into those. The script Debugger in Filemaker Advanced is so helpful it’s a no brainer to be looking at these as well. So many toys, so little time. :wink:

Edit 01/20/2016 4:27 PM… I was just looking over this entire thread and realized this is a gold mine of information. I am going to have to print it or clip it to Evernote or something.

Some time there is an issue with downloading the files.

@SaraParker, please post your question/issue in a new Topic, and provide a much detail as you can to describe exactly what is not working as you expect.

Chris,

Revisiting this after much tutorialing. This works beautifully in ScriptEditor. Where is the contents stored? I assume shCMD is either a variable or a standard command. I note it's not on the clipboard. pdftotext -layout - does not work. Is there a way I can dump the result either into a file or straight to the, or a clipboard? (Assuming it's not already in a file) The intent being to have KM parse it for a line in the file that has the account number and the date immediately following. Here's the output:

> " P.O. Box 56035 Member Statement Period: > Chicago, IL 60666 No: > (800) 328-1935 > 63753 04/01/20 thru 04/30/20 > > Account Summary

Also is the double quote " Coming from the output as well? Curious if I can use it as a possible starting point for parsing. Unless KM would not need it to snag the date of the file. I am snagging the date because the statements are named by the date of the statement so I can see them in order in a folder. (Note the > is the start of a new line, it didn't paste correctly)

In the AppleScript variable pdfText.

It would be best to add a command at the end:
return pdfText

to make it explicitly clear.

If you put this script in a KM Execute AppleScript, you can set that Action to receive the results in a variety of locations, including the System Clipboard, or a KM Variable.

shCMD is just an AppleScript text variable that stores the command to be run by the do shell script command, as shown in Chris' script:

set shCMD to text 2 thru -2 of ("
    export PATH=/opt/local/bin:/opt/local/sbin:/usr/local/bin:$PATH;
    pdftotext -layout " & (quoted form of posixFilePath) & " - 
  ")
  
  set pdfText to do shell script shCMD

I would save the results of the AppleScript to a KM Variable, and then use a KM Search using Regular Expression action using that variable as its source, to extract the date and any other data I wanted.

So does adding return store it somewhere such as a buffer?

No, it just returns the value of the variable from the script to whatever tool called the script.
As I said above, in the case of KM, you can capture the return value in the KM Action by setting the results option to any of these:

image

Got it. Thanks JMichael!

1 Like