From 2ced7bdbd7e6510f0b39f5861608f388c8fb049b Mon Sep 17 00:00:00 2001 From: Martin Koistinen Date: Fri, 15 Feb 2013 15:28:34 +0200 Subject: [PATCH] We need the service name, not the LocalizedDisplayName /usr/sbin/networksetup requires the unique, user-defined service name, E.g., "Built-in Ethernet" or, in my case "Ethernet (Intel)" which is what I named mine to distinguish it from any other Ethernet interfaces (I have multiple). Unfortunately, SCNetworkInterfaceGetLocalizedDisplayName is rather poorly documented, but I think it was intended to provide a localized version of "Ethernet" or "Bluetooth". In practice, I don't know if other languages define a different word for these. This would return the same string "Ethernet" (on EN-localized systems) on systems with multiple Ethernet interfaces, so isn't helpful. Signed-off-by: Martin Koistinen --- ToggleProxy.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/ToggleProxy.py b/ToggleProxy.py index 9f0236f..51594b5 100644 --- a/ToggleProxy.py +++ b/ToggleProxy.py @@ -63,12 +63,19 @@ class ToggleProxy(NSObject): return SCDynamicStoreCopyValue(self.store, 'State:/Network/Global/IPv4')['PrimaryInterface'] def loadNetworkServices(self): - """ load list of network services """ + """ Load and store a list of network services indexed by their BSDName """ self.services = {} - for interface in SCNetworkInterfaceCopyAll(): - device = SCNetworkInterfaceGetBSDName(interface) - servicename = SCNetworkInterfaceGetLocalizedDisplayName(interface) - self.services[device] = servicename + # Create a dummy preference reference + prefs = SCPreferencesCreate(kCFAllocatorDefault, 'PRG', None) + # Fetch the list of services + for service in SCNetworkServiceCopyAll(prefs): + # This is what we're after, the user-defined service name e.g., "Built-in Ethernet" + name = SCNetworkServiceGetName(service) + # Interface reference + interface = SCNetworkServiceGetInterface(service) + # The BSDName of the interface, E.g., "en1", this will be the index of our list + bsdname = SCNetworkInterfaceGetBSDName(interface) + self.services[bsdname] = name def watchForProxyChanges(self): """ install a watcher for proxy changes """