Applescript notes

My pay stubs come in via ADP's portal, which is frightfully annoying to use on a Mac.  Basically, it doesn't work for crap in Firefox, and you can't use 1Password (or keychain, come to that) to enter your credentials.

But it's been bugging me, so I've been working to automate it.  I've actually got it mostly working (I can go to the newest pay stub, and open that in its own window (it normally opens via an embed), but haven't yet gotten it to save), and just wanted to mention a few things I did to it there.

I should mention, too, that this is my first Applescript.  In fact, I was going to try to do the entire process via Automator, but that proved impossible (well, I couldn't manage to come close, anyway), so I fell back to using Applescript.

The script opens Safari, goes to the portal URL, and enters credentials via
tell application "System Events"
  keystroke "userid"
  keystroke tab
  keystroke "pw" -- yes, this is not terribly secure; using the keychain would be better
  keystroke return
  delay 4 -- needed before next step
end tell

Yes, this is horrible syntax (this is what happens when you have a programming language written for non-programmers), but it is pretty powerful.

Then, it gets really interesting, as I start embedding javascript:

tell application "Safari"
  tell current tab of window 1
    do JavaScript "document.getElementById (\"menuHit.node2\").onmouseover();"
    do JavaScript "for (var i = 0; i < document.links.length; i++) {
        if (document.links[i].textContent == 'Pay Statements') {
    delay 4

    -- you'd think I'd be used to it from java, but I hate these
    -- doubled backslashes
    set linkScript to do JavaScript "function getLink() {
      var pat = /\\d{2}\\/\\d{2}\\/\\d{4}/;
      for (var i = 0; i < document.links.length; i++) {
        if (document.links[i].textContent.search (pat) >= 0) {
          return i;

    set docName to do JavaScript "document.links[" & linkScript & "].textContent.replace (/(\\d{2})\\/(\\d{2})\\/(\\d{4})/, '$3$1$2');"

The interesting parts to, there, are that
  1. You can do javascript
  2. You can get values back from javascript (note that the whole purpose of that function, getLink(), is to enable returning a value via return.  Supposedly (I didn't try it), you can't just return from the javascript.  I thought for loops had a return value, implicitly, but that didn't seem to be the case from the slight experimentation I did.
  3. You can embed applescript variables into your javascript calls
  4. You can use javascript to get regular expression processing in your applescript
Anyway, just thought I'd post about this.  It's kind of interesting to me.  Dunno if I'll keep doing things in AppleScript; hopefully I can find better ways to do things.

Update: I was finally able to save the file by forcing safari to just save it to disk by doing
defaults write com.apple.Safari WebKitOmitPDFSupport -bool YES
defaults write com.apple.Safari AutoOpenSafeDownloads -bool NO
(The second of those was optional, but kept Preview from opening.)

Then I just used a shell script (after saving the file name from the embed src) to move the file from Downloads to my target directory.

Oh, getting the filename separated out was a little interesting:
set fileName to do shell script "basename `dirname " & docUrl & "`"

Update 2: Just noticed, with some amusement, that I had about ten tabs open for looking up various applescript oddities.

NHL Recap on the night

I watched roughly the second half of the first period of last night's Flyers/Rangers game, so I saw both first-period goals (and it was a beautiful forecheck on that Flyers goal, much as I hate to say it).

It was looking, for a while, like the Flyers would cruise.  But, boy howdy, did that change in the third.  Thanks to a pair of power-play goals, the Rangers just walked home with it.

And I really wish I'd watched the Hawks/Blues game.  Triple overtime?!?

And wow, did San Jose ever handle LA.  Definitely didn't expect that.  Despite Jones being perfect in his period of work for LA, if Quick has to come out again, LA is in serious trouble.  Although, now that I saw the highlights, you can't really blame Quick for any of the three goals I saw.  Any one of those would have taken a near-miraculous save.


Maestro musings

Mac Power Users has sold me on Keyboard Maestro, and I've been playing with that a bit.

Today, I realized that some of my apps don't update automagically, so I set up scripts to automatically download and install the latest version once a week.

Basically, it's just a shell script that runs, but there are a couple interesting things in the shell script.

One of the apps is dropbox, which has a download link of https://www.dropbox.com/download?src=index&plat=mac

To save that string as a shell variable (so it can be referenced multiple times), I finally figured out that I need to do:
export URL='https://www.dropbox.com/download?src=index\\\&plat=mac'

The parts of note are the single quotes and the double-quoting of the ampersand (those three backslashes).  That enables doing
curl -s -e "$URL" ...
a bit later on.

The other interesting bit is very apple-specific.  That download gets a dmg, of course, which needs to be mounted.  What makes that tough is that there's a license agreement (GPL) to which agreement must be indicated.  Hence,
echo 'y' |hdiutil attach $ONAME -readonly >/dev/null

The utility 'yes' should work in place of 'echo y', but for some reason it wasn't (or at least it didn't appear to be.  It might be that another change in the command, or the line before it, was what got things working, rather than the change to echo).

Anyway, Keyboard Maestro is pretty cool.  I'm sure that I've only scratched the surface of what it can do, so far.

I actually got dropbox to facilitate syncing 1Password on my mac and my iOS devices, but haven't used it for that at all; I've been doing WiFi syncing (MPU did not sell me on 1Password; I've been using that for years.  But they might have gotten me to update to v4, and definitely did get me to get the iOS version).

Sky blue goes red

I was fortunate to be able to watch the Liverpool v Man City game from the weekend, last night.  And it was quite a game, with Liverpool attacking to build-up very quickly.  Kompany broke up the first several build-ups by being in the right place, but City wasn't able to do anything in return.

Five minutes in, things looked like they might be improving, as Suarez was carded for a bad tackle.

But things turned wrong very quickly thereafter, as Liverpool managed a very nice counter-attacking goal, with Sterling providing the finish after a couple of very nice fakes.  Then things got even worse, as Yaya Toure (sp?) had to go off for an injury (a pull or strain appeared to be the likely culprit).  It was less encouraging when his replacement got a yellow card on his first challenge.

Things evened out a little bit after that, insofar as City was finally able to mount a little bit of an attack.  But Liverpool won a corner off a counterattack around the twentieth minute (I almost forgot, but it was a corner instead of a goal due to an amazing save by Hart), and Skrtle converted on a beautiful header from a very sharp angle to make City's task appear nearly hopeless.

Liverpool kept up pressure for the rest of the half, but was unable to score another.

City came out firing in the second half (helped by a smart substitution, putting in... Manlin, maybe?), bringing immediate pressure.  Nasri and Silva looked far more dangerous, all of a sudden, and City scored one about fifteen minutes into the half.  As if that wasn't tense enough, they scored another (it was officially scored as an own goal, for reasons that are beyond me.  Yes, the shot deflected off a defender and the goalie, but I still don't see why that's enough to make it an own goal) only a few minutes later.

That made for a very tense rest of the game.

Liverpool fans were getting very concerned, but they managed to find the back of the net around the 80th minute after a badly failed clearing attempt by Kompany went right to Coutinho.

That forced City to push forward even more, and they were pushing hard when Henderson got a straight red for a hard tackle to put Liverpool down a man in stoppage time.

And that's just about when things went south for me, as the DVR had apparently stopped recording with a couple minutes left in stoppage time.  Thank goodness that was the final result; I'd've been about ready to kill someone if City had leveled after that and I'd missed it.

But it was one hell of a good game, regardless.  I'd been sure for quite a while that City was going to end up winning the league, but now I'm a doubter.  Liverpool will have to falter pretty badly, at this point.


"City to get ultra-fast broadband"

I saw that headline from the BBC (that was the RSS headline), and that a new fiber optic network would be created in New York.  My first thought was, "I thought I heard that the incumbent telecoms wouldn't make that possible in New York for the foreseeable future".  Then, "Maybe I was wrong?"

Then I realized that I had mis-read the one-sentence summary, and that it was York, England, not New York, New York.  And suddenly it all made sense again.  *sigh*


I read, yesterday, about the full lunar eclipse that happened last night.  Here, it started around one AM, reached peak around four AM, and ended by about seven AM.

I debated setting an alarm to help me wake up to observe it last night, but ended up not bothering.  However, I did wake up around four, to go to the bathroom, and decided to walk outside to see how things looked.

Well, I needn't have bothered; the cloud cover was complete, so there was no chance of getting a picture at all.

I wandered back to bed, glad that I hadn't even bothered to search for the camera, proper lens, and heavy tripod.  Still disappointed, though; I've never seen a lunar eclipse.

Supposedly, there'll be another on October eighth; will hope for clearer skies, then.  And will remember to check the cloud map before going to bed.

What is the military?

It was recently reported that the Sgt Maj of the Marine Corps (the highest enlisted soldier in the Marines) said to Congress (in sworn testimony) that "lower pay raises discipline".

And while I'm sure that there's a kernel of truth in that statement (for sure, it forces decisions over where limited resources should be spent), it's also true that it's awfully cruel to soldiers who already sacrifice a lot to serve their country.  Forcing them to choose between keeping the power on or paying the co-pay to see a doctor to see whether a persistent cough is important, for instance.

His response to those charges:
"If we don't get a hold of slowing the growth, we will become an entitlement-based, a health care provider-based Corps, and not a warfighting organization," Barrett said.

I wonder how far he thinks the warfighting will get, without the warfighter.

Marines without weapons are still a force to be reckoned with.  Weapons without Marines are not the least bit scary.

Ab Phablet

I listened to the latest Vector podcast, and I think they missed one big thing (the biggest, actually) driving Asian demand for phablets.

Our family went to Hong Kong for vacation a year and change ago, to visit family over there.  And the biggest thing that struck me was how many people were watching TV shows on their phones on the MTR (Hong Kong's metro; they were going to and from work, presumably).  Seriously, it was a large majority of the young people.

And that's a big application that will look best in 1080p (I'd hate to think how big the tablet would need to get for 4K to make any sense, even ignoring the streaming bandwidth demands of that).  If you're going to spend half an hour or an hour every day watching TV on your phone, you'll damned-sure want to do it with maximum resolution.  So I can certainly see why people would want a bigger phone.

(And while I'm talking about this, let's also point out what this says about our crummy public transportation, that we can't even offer free WiFi on trains and buses.  And what it says about our telecomm infrastructure, even more than what it says about our transportation infrastructure.)

The downside, of course, is that the phone looks ridiculous when you're using it as a phone.  I remember one young lady using one of those phones with a case on it.  It looked like she was holding her purse up to her ear to talk.  Looking cool, she was not.

All that said, if Apple were to make one, would I want it?  I'm leaning towards no, but I'd definitely have to hold it and play with it a bit to be sure.


Going out with a whimper

I actually did watch the last several games of the Caps; all except the Hurricanes game.  And despite me saying how much better off they'd be if they'd lost those last several, they decided to take nine points in those last five games.

The result of that was that they moved up nine spots from where they would have been had they lost out.  In terms of the draft lottery, their chance of getting the top pick dropped from 4.7% (roughly; that's the nominal chance, but New Jersey's guaranteed last place would have made it slightly better) to 0.5% (ditto).

So, did they get all those points by playing well?  Well, fenwick 5v5 close says no; it says that they got very, very lucky.  Part of that was Holtby and Halak playing very well (Holtby, in particular, looked fabulous), and part of it was very good luck at shooting.  The third line, especially, looked very good.

In fact, in that Carolina game that I missed, each member of the third line scored a very nice goal.  Quite a performance.  The downside was that OV got another -2 in that game (tough to do in a 5-2 win, although the shortie allowed had a lot to do with it).

And it should be pointed out that all this happened with Green on the shelf again, as he was injured again.  So the bottom two pairings were Orlov-Strachan and Carrick-Brouillette.  And those two pairs were absolutely brutal at times, giving lots of tests to that goaltending.  In the last game, Stamkos was allowed behind Orlov-Strachan twice in the first period (Holtby saved one shot, while the other went wide; that is not the way to bet, on Stamkos alone).

And it should also be pointed out that two of those games were shootouts, taking the Caps' total on the season to twenty-one (with a 10-11 record.  Thank goodness they didn't match the Devils' winless record (0-13) in the shootout).  Hard to believe that a team can go to the shootout in 1/4 of their games.  I certainly hope that was a record.

Anyway, it was hard to decide on whether to be happy or upset at what happened.  There was some really nice play over those games, but the results really didn't help the team at all.

And finishing with dueling shutouts, and losing when also shut out in the shootout?  Yeah, that was the whimper.  In fact, it was a terrible game.  The Caps had all of seven shots in the first two periods (and eight shots that missed the net); they were just brutalized through that span.  And the third period was only eleven shots; that's not bad, but not enough to make the entire game respectable, either.

A frustrating end to a frustrating season; it'll be interesting to see what happens in the off-season.  I'm betting that both McPhee and Oates are gone (and certainly good riddance to the latter, despite my initial happiness at his hiring).  The big question, in my mind, is if Dick Patrick accompanies them into that sunset.

And then there's the big question of who replaces them.  I really have no idea who that might be, but I hope it's someone good.

Fingers crossed; go Caps!