Help Downloading Statements

Hi Bruce...

I tried to set the wait times as low as possible. Then on one occasion it took an extremely long time to load the page and i decided to raise the values since the script needs run correctly every time. I think there are some other ways to deal with page loads though. I haven't finished reading the KM documentation. (Other manuals have taken precedence lately!).

If you look at the screen shot I posted above you'll see next to the image container there is a slider. This adjusts for the amount of blur of the graphic. I found in one instance I needed to move it all the way to the right to recognize the image. You'll definitely need to experiment each time you use the "Click At Found Image" action. It's one of my favorite actions because it's so easy to implement.

That's another tricky bit. Tricky because sometimes vendors change the email address they send from. (They change quite a few other things too!) I got a lot of help from Christopher Stone as well as JMichaelTX and others on pulling my scripts together.

Setting up the email part is actually the easiest part of all of this. What you have to do is setup a Mail Rule that gets triggered when the email comes in and then runs an AppleScript in Mail.app (or other mail clients) that brings Safari forward then fires the KM script. Poke around in your mail applications preferences and look for "Rules" in Mail.app or Outlook. In the rules section one of the options after filtering the email based on the rule's conditions is to run an AppleScript.

KM can help with the bit that gets entered to call the specific KM script. The AppleScript is simple and here's what it looks like:

tell application "Safari"
  if not running then run
  activate
end tell
-- ignoring application responses
tell application "Keyboard Maestro Engine"
  do script "028B720D-4B15-4A98-B289-CD87A27CEFE3"
  -- or: do script "Move or Click Mouse"
  -- or: do script "028B720D-4B15-4A98-B289-CD87A27CEFE3" with parameter "Whatever"
end tell
-- end ignoring

You have to write the first tell block but KM will write the second tell block for you and you can paste it into AppleScript then save the script. Then have your mail rule use that AppleScript. I name my AppleScripts by financial institution to keep them straight.

Next time you are in KM take a minute to look at the action where you set the keystroke to fire off the KM marco, you'll see "New Trigger" then "Or by script", click on the "Or by script" popup menu and you'll see AppleScript as an option. Once you select it, KM will generate the code to reference the KM macro you are creating. So it will always trigger that macro and no other macro.

That should get you going on having the email trigger the script. I think I need to add alerts to all my scripts and/or macros that tells me when they are running, or perhaps have AppleScript "speak text" that says "Script has been activated, Please wait." This is because a couple of times I opened mail and the script fired off and started to perform the action and I was in the middle of something. It was a momentary shock until I realized what was happening.

I will probably do something generic that can be used in all scripts, like a module. I have realized that when scripting using modular code seems to me the best way to go. Instead of always having to write the same code over and over, I can take blocks of code and plug them in where I need them.

So you may want to also consider adding some type of alert or writing a post-it note to place on your computer that says "Wait for mail rules to run" to remind yourself to give mail time to process the attached scripts to filtering rules you have setup. You'll have to decide what's best for you.

Thank you again John. Sorry for the slow response time. My wife and I have a small business where we typically work at least 6 days a week putting in about 14 hour a day, have a special needs child and keep chickens and ducks. I will be 60 in less than 6 months so I am moving a little more slowly than when I was younger. I want to find a flexible way to start learning apple script. I signed up for what I thought was a forum but it ended up being emails that seemed to be directed toward those who already know AppleScript. For me the issue is finding the time and a good resource. I have found one site that looks promising: http://macosxautomation.com/applescript/.

I say this with sincere humility but I thought that my ability to find the embedded Java at my credit union’s website and use it in the “Execute JavaScript in Safari” showed that I can learn those things I will need to create some really nice ‘automations’ on my Mac. Then as I stated earlier I want to be able to upload these macros and scripts for others to use.

I use mail rules a lot. I also use Mail Act-On. One thing I have learned from creating Hazel rules is to use a combination of items in my rule. So for renaming the bank statement from this credit union my rules are:
if the name is document,
the extension is pdf and
the body of the message contains my account number rename it …

In the case of this credit union I am thinking I can have the AppleScript identify it this way:
if the from is: notice@blucurrent.org and
the subject is: Your new eStatement is ready! then tell application “Keyboard Maestro Engine” to execute the script ….
Okay my inner nerd is kicking in when I say that I am already getting excited about learning AppleScript first and then follow JMichaelTX’s advice to learn Java. A funny coincidence my little brother whose goes by his middle name of Michael and whose first initial is J lives in Texas.

So I think I am getting the idea. First I have an AppleScript that will trigger KM when I get an email from my credit union that my statement is ready. The last thing is that I need another AppleScript that would do something like: if Desktop contains a file whose named ends with BluCurrent.pdf then logout of website in active tab of Safari. This should be some nice clean code without reference to mouse clicks. I will confess that I really prefer doing as much as possible from the keyboard. Mice have their place but I think in some instances they can slow one down. This may come from my DOS and Linux days.

Okay I did a little research and study. Here is what I have thus far and it works:

tell application "Mail"
  set theSelection to selection
  set theMessage to item 1 of theSelection
  subject of theMessage
end tell
--> "Your new eStatement is ready!"
tell application "Safari"
  if not running then run
  activate
end tell

I am bogged down on the correct way to add a 2nd condition. Here is what I had following the above but it was wrong:

tell application "Mail"
  set theSelection to selection
  set theMessage to item 2 of theSelection
  sender of the message
end tell
--> "notice@blucurrent.org"

Any guidance would be appreciated. Woo hoo AppleScript is cool.

Having fun?

Yep. This stuff is cool. Nothing like being able to (hopefully) get things to work the way you want them to! Are we actually saving time? Who cares??? :slight_smile:

Well Dan I don’t know if I am actually saving much time but it is good mental exercise which I want to keep up as I approach my 60th year of life. I am young at heart which also helps along with regular exercise. In my case this would be working outside, doing yoga, practicing marital arts, and swinging Indian Clubs. My belief is that we will have longer richer lives if we eat right while keeping physically and mentally fit.

Absolutely! I’m 59, and I started working out with a personal trainer last year, and it’s made a big difference. I’m actually fit enough to get back on the golf course with my wife, walking and carrying our bags (so I swing different kinds of clubs than you). And I’ve lost 30 pounds.

Mental exercise is indeed equally important, as you said. Though sometimes, when I’m fighting with a programming issue, I question whether it’s good for my mental health. But I tell the voices in my head to shut up, then everything’s fine! :open_mouth:

Just a comment or to use the AppleScript term an “Explanation”. Since I like audio-visual feedback I am going to start putting “Beep” into my AppleScripts.

1 Like

How about this?

1 Like

I like that idea. With a mischievous bent one could have a lot of fun pranking family members.

Oh man, that brought back a chilling memory. Can't remember the details - it was a long time ago. But somehow I had set up a recorded voice to say something late at night, to prank someone who was going to be on my computer.

They ended up coming at a different time, and I forgot all about it. Until I was home alone late one night, and I had the volume turned up... Hoo boy, did that scare the daylights out of me. Serves me right!

Hey Bruce,

I'm not clear on what you're doing, but here's a few tidbits:

tell application "Mail"
  set selectedMessageList to selection
  
  repeat with theMessage in selectedMessageList
    set msgSubject to subject of theMessage
    set msgSender to sender of theMessage
    
    if msgSubject contains "something" or msgSender contains "something else" then
      # Do Something...
    end if
    
  end repeat
  
end tell

-Chris

First of all thank you Chris. I think I have figured 95% of my macro out. It seems the best place to begin is with an Apple Mail Inbox rule instead of an AppleScript. This way no more computer resources are used than necessary. In my case I created a mail rule that will launch when the email is from: "notice@blucurrent.org" and
the subject is: “Your new eStatement is ready!”. This rule ends by executing an AppleScript: KMBCS.

This is the KMBCS Apple Script I created:

tell application “System Events” to keystroke "kmbcs"
delay (random number from 0.5 to 5)
end
beep 2

this macro just performs the key presses to launch my KM macro to download my statement from a specific credit union.

For any other members of this forum who are newbies like me you can use this script. Lust copy and paste into the Script Editor changing the kmbcs in quotes to whatever keypresses you want. This will allow you to do what I did by creating an Apple Mail Inbox rule to launch an Apple Script to in turn launch a KM Macro. One more thing I had to move the script to ~Library/Application Scripts/com.apple.mail/. Once this is done it will become a ‘linkable’ script in Apple Mail.

The problem now seems to be with either this particular credit union’s website, my ISP and/or my use of the “Wait for Safari to Finish Loading”. The best solution would seem to be for KM to halt until a page has loaded completely instead of waiting for a specified period of time. I looked at “How to tell if Safari has finished loading a web page” in this forum but I do not yet have enough knowledge to figure how if it contained a solution for my situation and if so how exactly to implement it.

As I think about the big picture for those of us who want to download statements and the time we put into trying to construct complete download macros, I have considered that maybe a less complete download macro is okay.

Consider that the companies we download statements from and especially the large ones like major credit card issuers, insurance companies and banks have an obligation to protect our data from the ‘bad guys’. I would expect that this situation is similar to the back and forth between the manufacturers of police radars and radar detectors. I can see putting in a lot of time to create a complete download macro only to have a company change some part of the ‘equation’ in an effort to increase security with the result that my macro would fail. For some of my statements I am thinking of modifying my macros to get me 80% of the way there with a popup message that will tell me to download my xyz statement. Where I think my time might be better spent is on an AppleScript that could be launched when I receive an email notifying me that my statement is ready for download. This AppleScript would have a feature that could ask me if I would like to download my statement now or would you like to be reminded at a later time. A really nice feature if it is possible would enable me to select the later time, either in minutes, hours or days.

Hey Bruce,

Don't send keystrokes to Keyboard Maestro with System Events.

Call the macro directly:

tell application "Keyboard Maestro Engine"
  do script "Generic-Test 01"
end tell

You can use the macro name or its UUID.

-Chris

Chris,

Thank you again for all of your help and the really nice AppleScript code. I like your concision in coding compared to some examples I have seen online. You also have a great way of explaining things which in turn helps me to learn AppleScript more quickly and easily. I am going to organize my coding notes with sections for AppleScript, Java, Keyboard Maestro, etc. probably in a Circus Ponies Notebook unless I find a better alternative.

I am reading a nice beginner’s book on AppleScript when I need to take a break from work as well as the Keyboard Maestro Manual. The book on AppleScript which is free in the iBooks store is titled, The Basics of Apple Script by Nayan Seth who was pursuing a MS in Computer Science at Arizona State University.

@DanThomas - To tell you the truth I hadn’t even looked into it yet but it’s so easy I think I need to go in and add it right this minute. :slight_smile:

Absolutely. It's also a good idea to do things to give you a heads up that the rules are running because think of all the times you launch mail and start working on something. I have discovered it's a good idea to know the script has fired off because I actually caused a script to cancel once. There was also getting over the shock of realizing that your computer just did something and you don't know why. You can add a tell block for announcing that the script has fired or you can add speaking steps in Keyboard Maestro. It's your choice.

I put in a speak text action as the first step in Keyboard Maestro which announces which bank statement script is initiating. It might actually be better to put it in a tell block in AppleScript since that's the first script to run. I suppose I should test it but I am less competent with AppleScript right now and find focusing on one thing at a time is better for my learning style.

Also I don't do rule running sessions I just let the statements download as the emails come in. I suppose I could consolidate some mail rules into Automation sessions and just let a bunch of Automated tasks run but I think it's better to let macros do what they do while I am watching so I can monitor things in the cases of email. There are other situations where letting something run while you grab a coffee is a better idea. Such as making thumbnails of 300 photos or situations like that. The macros that deal with statements are really in place (in my case) because the can do it so much more quickly.

I wonder if anyone else has some additional thoughts on this, one way or another?

Thank you for the feedback Tunes. I have pretty much given up on a complete statement download macro using KM. I have an Apple Mail inbox rule to notify me that a statement has arrived. I the login into the appropriate website and download the my bank, credit unions, AT&T wireless, State Farm, etc. statement. I have a Hazel function that renames these statements in the format of year-month followed by that statement name. For example, “2016-06 State Farm Statement”. That statement stays on my desktop until I reconcile the related account at which point I drag it into a folder named Hazel on my Desktop which in turn has a rule to move it into the appropriate statements folder. The issue I was having with KM was getting it recognize the latest statement on the company’s web-site and then to download it.

I like your suggestion of a speak text action. I want to do more with Apple Script but my life is way too busy currently. I have a love hate relationship with Apple Mail and am anxious to see MailMate 2 when it is released. In El Capitan Apple removed the ability to have what I will call a complex rule, i.e., a rule that has both “All” and “Any” conditions. Right now I have ~2,800 emails in my top level Inbox in part because of merchants who seem to think that I have nothing to do but read emails from them. A few merchants send me multiple email a day. My numerous Apple Mail Inbox rules do not appear to be working the way they are supposed to. In previous Apple Mail releases I could have a rule that for example would say if I receive an email from “Any” of these emails addresses “And” it has been more than x days since the email was received “Flag” and “Move” the email.

I did not think that I would save as much time getting some of these KM rules to work as I would spend using my current method. I am also thinking that banks, insurance companies, utilities, credit card companies, etc. may move towards more standardized electronic statements and web storage for this very purpose and away from JavaScript. BernSH sent me a linkin this thread about a company that offers to download your statements and email them to you as a service. I think this kind of service will grow and put pressure on companies for more statement standardization online probably with .PDF being the standard format.

BernSh I just wanted to say thank you for recommending filethis.com. Thanks again.

Very gratifying to get to contribute. Thanks for the opening :wink: