On login: Launch certain apps depending on where I am?

Hello folks,

First let me start by saying I’m more or less brand new to AppleScript, but certainly not to Macs in general. I remember dabbling in AppleScript a bit back in System 7, but clearly that was long enough ago that I really have no memory of it.

So here’s my question. I currently have several applications listed as Login Items for my user account, but I’d really like to be able to segregate the list. When I log in to my machine while at work, I’d like it to launch one set of apps. When I log in while at home, I’d like a totally different set of apps.

Of course, I’d like this to be transparent, i.e. I don’t want to have to run something or make a choice to let the system know which set of apps to launch.

My thought for an approach to this is to write an AppleScript that looks up my machine’s IP address (on two different NICs), then launches a given list of apps depending on what the addresses are. More specifically, since in both locations I’m on DHCP for my wireless NIC and a static IP for my ethernet NIC, the script would need to make a choice based on a substring of the IPs, since in some cases the last digits will change.

I’m sure this sort of script sounds trivial, but that’s where the “me being totally new” part comes in. I understand basic programming concepts well, but not AppleScript, at least beyond a conceptual point.

Anyone have any pointers? I don’t think I want or need a fully developed script, just some thoughts on how to do certain parts (like look up the IP assigned to a specific network interface, etc.)

Model: MacBook Air
AppleScript: 2.0.1
Browser: Camino 1.6.6
Operating System: Mac OS X (10.5)

Hi,

welcome to MacScripter.

On my PowerBook I use a script for a similar purpose.
This handler detects the internet connection and its kind

set {connected, wired, IPAddress, ssid} to getLocation()

on getLocation()
	set wiredIP to do shell script "/sbin/ifconfig en0 | /usr/bin/awk '/inet / {print $2}'"
	if wiredIP is not "" then
		return {true, true, wiredIP, ""}
	else
		set wirelessIP to do shell script "/sbin/ifconfig en1 | /usr/bin/awk '/inet / {print $2}'"
		if wirelessIP is not "" then
			set ssid to do shell script "/System/Library/PrivateFrameworks/Apple80211.framework/Resources/airport -I | /usr/bin/awk '/ SSID: / {print $2}'"
			return {true, false, wirelessIP, ssid}
		else
			return {false, false, "", ""}
		end if
	end if
end getLocation

the result will be saved into 4 variables:
connected is true if there is a valid internet connection, otherwise false
wired is true if the computer is connected via Ethernet , otherwise false
IPAddress contains the network IP address or “” if there is no connection
ssid contains the name of the connected wireless network otherwise “”

Awesome. That’s about what I was thinking it would be (running shell scripts to get the NIC information) but I was unsure if AppleScript was able to get that info itself.

I’ll work something out based on your code and post the result here once I get it working. It looks like your script assumes wired OR wireless and can’t provide an IP for both if both exist - is that correct? If so, I’ll modify my version to return both IPs, since in some cases I have both connections active and in other cases, only one.

Though, I could probably do 90% of what I need just by getting the ssid of the wireless connection, since it’s pretty much always on.

Thanks!

hmm, this is awkward… I registered today and posted a new topic that is almost exactly the same as this one without looking for the answer first. I have a similar problem, I just want to force-quit a certain application (in this case, Transmission) when I connect to a certain SSID (ACCStudent).

So I stole one line from your code and wrote my script based on ssid of the wireless connection:

set ssid to do shell script "/System/Library/PrivateFrameworks/Apple80211.framework/Resources/airport -I | awk '/ SSID: / {print $2}'"
if ssid is "ssid_at_home" then
	tell application "Quicksilver" to activate
	tell application "Camino" to activate
end if
if ssid is "ssid_at_work" then
	tell application "Quicksilver" to activate
	tell application "Microsoft Entourage" to activate
	tell application "My Day" to activate
	tell application "Camino" to activate
	tell application "Finder" to mount volume "file_share"
end if

The only thing I’m struggling with now is that if I put Entourage in my login items for my account, it “remembers” it’s window position correctly (maximized on an external display.) When I launch it using this script, it doesn’t. Instead, it shows up maximized on my built-in display.

I’m betting there’s a way to control window position in a script? I’m off to go searching on this, but if anyone can provide guidance I’d appreciate it.

Added this to control window position:

	tell application "Microsoft Entourage"
		activate
		set bounds of window "Inbox ” Exchange" to {-10, -1000, 1400, -100}
		set zoomed of window "Inbox ” Exchange" to true
	end tell

The tricky part was realizing I needed to use negative numbers to get it up to my external display, which is “above” my built in display. So the first line gets it up to that display, then the second one zooms it. I’m guessing this could all be done in a single “set bounds” line, but this seems to work OK.

Really neat, Stefan, with just one word of warning: using a private framework always runs the risk that a system or security update will break the ssid function used here.

I know, Adam, but unlike /usr/sbin/networksetup this solution works on both Tiger and Leopard.
An alternative way is


set ssid to do shell script "/usr/sbin/system_profiler SPAirPortDataType | /usr/bin/awk '/Current/ {print $NF}'"