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.

No comments:

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