Wednesday, August 3, 2011

applescript to allow emulate separate external and internal server settings in outlook

To show my appreciation for all those folks on the web who shared details on applescripting outlook, let me share my first AppleScript. I use it to keep the Outlook server value up to date when I move from outside to inside a vpn. Mac Mail has separate internal and external server settings, but I prefer Mac Outlook (for task integration with my work) and it only has one server setting. In order to use outlook inside the vpn, I found myself constantly updating the server when I entered my vpn. Outside the vpn, the autodiscover mechanism takes effect for me so I didn't need to do anything when I left the vpn.

The script below checks up on outlook when the system is idle and tells it to update the server value if it is inside the vpn.



on check_server()
 
 tell application "Microsoft Outlook"
  
  if (do shell script "/sbin/ifconfig") contains "your internal vpn address" and server of default account does not contain "your internal server name" then
   set server of default account to "your internal server name"
   set ldap server of default account to "your ldap server"
   set ldap port of default account to port_number
   set ldap search base of default account to "..."
   sync inbox
  end if
  
 end tell
 
 
end check_server

on run
 check_server()
end run

on isRunning(appName)
 tell application "System Events" to (name of processes) contains appName
end isRunning

on idle
 if isRunning("Microsoft Outlook") then
  check_server()
 else
  quit
 end if
end idle



You could add code to generate the external server too. In my case, the autodiscover mechanism is able to find the externally visible server.

I recommend saving your package in the AppleScript editor as an always-on applet. To avoid having open applescript app sitting on your dock, edit the top-level dict entry of the Info.plist file to include.


<key>LSUIElement</key>
<string>1</string>


The above key keeps your application form showing up on the dock.

One more thing: this script will actually start Outlook if it isn't open already, so you don't just want to run it on startup. What I've done instead is to run this script _instead_ of Outlook. And just to make it look pretty, I opened up the Outlook package and copied the .icns file from Outlook into the saved applet package and stuck a link on my dock. So now I have something that looks like Outlook on my dock, that starts Outlook, but also keeps my server value up to date.

I hope this is useful so some of you out there! Thanks to everyone who shared their AppleScript expertise on the web.