Friday, January 18, 2013

AppleScript to open any web interface/account like GMail, etc

Few years back I switched to Mac and since then I'd say it's been great. Yes, Macs are expensive but they do offer great things in comparison. Anyway, since I'm not doing a sales pitch here I'll go ahead and say how you can automate logging onto any of your web accounts, for example your gmail account. The reason I do this is that I open Google web interface for both email and calendar everyday early in the morning at my work. Eventually, you'd think of doing it with just one click and so I came up with this.

First, open AppleScript editor on your mac and type what I show here below:
tell application "Safari" to open location "https://mail.google.com"
delay 5
tell application "Safari" to activate
tell application "System Events"
 keystroke "username"
 keystroke tab
 keystroke "password"
 keystroke return
end tell
Most of the times usernames and passwords are contain numerals. Entering keystroke "Sreedhar123" for username would just enter "Sreedhar" in the username field without 123 at the end of it. Which I'd say is a problemo. To avoid this from happening we need to enter applescript key codes for the numbers. So, for "Sreedhar123" you need to put this code:
tell application "Safari" to open location "https://mail.google.com"
delay 5
tell application "Safari" to activate
tell application "System Events"
 keystroke "Sreedhar"
        key code 18
        key code 19
        key code 20
 keystroke tab
 keystroke "password"
 keystroke return
end tell
Remember that same goes for password as well. Google "applescript keycodes" if you are looking a keycode for a specific character.


Of course, you can test this script immediately without having to save it by clicking on "Run" in the AppleScript menu. Once your'e satisfied save it as an application by choosing "Application" from the dropdown menu for File Format. You can drag the saved application onto your dock. Clicking it would take you to gmail web interface with just one and only one click. Nice! Right?

Now, remember that once you save it as an app you wouldn't be able to change the code anymore. If you'd like to see or change the code you'll have to save it in Script format by choosing "Script" for File Format which is default. Again, it's a no brainer doing something like this because you've your password exposed. Whereas saving it as an app doesn't expose anything.

Doing this way you can automate logging onto any web interface with just one click there by avoiding typing the web page address, entering the username, password and then entering return.

Finally, as you see I put "delay 5" in the code so that it waits for 5 seconds before trying to enter the username. The reason is that sometimes a page takes 2 to 3 seconds to load. If it takes more than 5 seconds then change this number. Otherwise, nothing happens as applescript goes through the code executing each line. Which means it'll have entered the username and password into something as the page hasn't loaded yet. In the worst case this might end up typing full or partial username and password into the address bar. So, be careful.

Wednesday, January 16, 2013

Making an app with an applescript on your mac that gets you public transit directions


At the end of the day in my office, just before leaving the office I generally check google maps for getting public transit directions and time tables. Google Maps lets you save driving directions with a link "Save to My Maps" under the directions to your "My places". But this is not the case with public transit directions. Which in my opinion sucks. This might sound trivial to some one who uses their car to goto office. Trust me it's a heck of nuisance to someone like me as I use public transit all the time.

Generally, what I do is I open a new tab in my browser and then open the page "maps.google.com" and then click on public transit, then type the addresses, let's say for example home or office if you've saved them or type the entire addresses and then select the train or bus from options based on your public transit mode. Again, it doesn't sound that big of a deal if you have to do this once in a month. But I do this million times in a month. So, I've started looking for some sort of a way to save it to my places.

Whatever I found googling didn't help me at all. In the end I've decided to make an app for my self with an applescript on my mac. Then I thought of putting it here if someone is interested in knowing how I did it. Here we go:

First goto maps.google.com and get the public transit directions for your destination. Let's say we need public transit directions from Hoboken HBLR Terminal to Penn Station, New York, NY. I know that there is a PATH train service that runs from Hoboken to 34th street which is where Penn station. Of course, you don't need to know anything of this at all. As I know I always select the option "train" from show options. Then I get the link to directions from share symbol next to My places. This is what I've got here:

https://maps.google.com/maps?saddr=Hoboken+HBLR+Terminal&daddr=Penn+Station,+New+York,+NY&hl=en&ll=40.740982,-74.011202&spn=0.027411,0.05579&sll=40.740982,-74.012575&sspn=0.027411,0.05579&geocode=Fc-MbQId92SW-yk7lmyP4lnCiTHoGLLYwLjysQ%3BFfTNbQIdlPKW-yELxignjABkPin5tzQUrlnCiTELxignjABkPg&oq=Penn&dirflg=rT&ttype=now&noexp=0&noal=0&sort=def&mra=ls&t=m&z=15&start=0



Once you have this link, go and open applescript editor (Press command + space bar and type applescript) and add this code below and then save it to where ever you want to have it in your system. I put it on my desktop.

Applescript Code:

do shell script "open -a Safari \"https://maps.google.com/maps?saddr=Hoboken+HBLR+Terminal&daddr=Penn+Station,+New+York,+NY&hl=en&ll=40.740982,-74.011202&spn=0.027411,0.05579&sll=40.740982,-74.012575&sspn=0.027411,0.05579&geocode=Fc-MbQId92SW-yk7lmyP4lnCiTHoGLLYwLjysQ%3BFfTNbQIdlPKW-yELxignjABkPin5tzQUrlnCiTELxignjABkPg&oq=Penn&dirflg=rT&ttype=now&noexp=0&noal=0&sort=def&mra=ls&t=m&z=15&start=0\""


The above code opens the tab with public transit directions in Safari.

To open the page in a default browser, just remove -a Safari:

do shell script "open \"https://maps.google.com/maps?saddr=Hoboken+HBLR+Terminal&daddr=Penn+Station,+New+York,+NY&hl=en&ll=40.740982,-74.011202&spn=0.027411,0.05579&sll=40.740982,-74.012575&sspn=0.027411,0.05579&geocode=Fc-MbQId92SW-yk7lmyP4lnCiTHoGLLYwLjysQ%3BFfTNbQIdlPKW-yELxignjABkPin5tzQUrlnCiTELxignjABkPg&oq=Penn&dirflg=rT&ttype=now&noexp=0&noal=0&sort=def&mra=ls&t=m&z=15&start=0\""


Before making it an app you can simply try running it with "Run" on the editor which shows you whether it is working or not. Once you know that it is working the way you wanted it to be go for save options and there select "Application" in the "File Format" drop down menu. Once you save it as application, let's say on your Desktop you can simply drag it onto your Dock.

Once you have it there on the dock you can simply click it just like you open any other application. This opens public transit directions with your selected public transportation mode with the latest time table.

By the way, if you'd like to save your script so that you can modify or refer to in the future then I say save another copy some where in SCPT format by choosing SCPT from File Format drop down menu options.

PBS Script Generator: Interdependent dropdown/select menus in Javascript

PBS SCRIPT GENERATOR
SH/BASH TCSH/CSH
Begin End Abort

About Me

LA, CA, United States
Here I write about the battles that have been going on in my mind. It's pretty much a scribble.

Sreedhar Manchu

Sreedhar Manchu
Higher Education: Not a simple life anymore