Connect to Internet with phone, scripted

Hi,

I’m trying to automate the following process:
-turn bluetooth on (done using blueutil http://www.frederikseiffert.de/blueutil/)
-switch to specific Network location
-connect to internet using the phone as modem
-wait silently for input
-on input, disconnect from internet (can take a long time)
-switch to original Network location
-turn bluetooth off

I’ve found lots of scripts using Internet Connect with GUI scripting, which doesn’t help much given I’m using Leopard.

I’ve come this far for now:

property blueutilPath : "/usr/local/bin/blueutil"

if execBlueutil("status") ends with "off" then
	execBlueutil("on")
end if

tell application "System Events"
	tell current location of network preferences
		set current location to "On the road"
		set isConnected to connected of current configuration of Bluetooth
	end tell
end tell

on execBlueutil(command)
	set res to do shell script blueutilPath & " " & command
	if res contains "Error" then
		display dialog res
		quit
	end if
	return res
end execBlueutil

Some parts I should be able to manage, but i’m completely lost on how to communicate with System Events for connection and location change. Can anyone help?

Hi

You can use “scselect” from the command line to change your location.

do shell script "scselect Automatic" --will put your location back to the osx default if you still have it

do a google search for what you can do with this command it should do what you need

Hi,

@ pidge: scselect is not needed in Leopard, Sytem Events has a new network preferences element, which is directly scriptable

@ meegothethird

I don’t know how to do the “-wait silently for input” request,
but try this for the rest


property blueutilPath : "/usr/local/bin/blueutil"


if execBlueutil("status") ends with "off" then
	execBlueutil("on")
end if

tell application "System Events"
	tell network preferences
		set current location to location "On the Road"
		tell current location
			set BlueService to a reference to (first service whose kind is 3)
			if exists BlueService then connect BlueService
			my CheckInput()
			disconnect BlueService
		end tell
		set current location to location "Home"
	end tell
end tell
execBlueutil("off")

on execBlueutil(command)
	set res to do shell script blueutilPath & " " & command
	if res contains "Error" then
		display dialog res
		quit
	end if
	return res
end execBlueutil

on CheckInput()
	-- do something
end CheckInput

hi stefan

thanks for that i didn’t realise!! :confused:

Stefan, pidge, Thanks.

I’ll probably skip the “wait silently for input” and CheckInput part. I can two scripts. One for connection, one for disconnection. With Quicksilver, it’s not much of a hassle.

I’ll post my stuff when it’s done.

As a side note, in:

set BlueService to a reference to (first service whose kind is 3)

kind 3 means BT modem, right? So kind 1,2 should be airport/ethernet/etc… Do you know where can i find documentation on this? I checked the System Events library in Script Editor, which tell me what a “kind” is but not which connection they describe so it’s of limited use.

Edit: :cool: this thread is already first result in Google with words “applescript connect bluetooth” or “applescript connect phone”

I took the reference by kind from AppleScript Release Notes.
You can probably refer to the service also with


tell application "System Events"
	set BlueService to service "Bluetooth" of current location of network preferences
end tell

AFAIK there is no documentation about the kind numbering, but you can use AppleScript to retrieve the name and kind of the services


tell application "System Events"
	set serviceList to {}
	repeat with i in services of network preferences
		set end of serviceList to {name, kind} of i
	end repeat
end tell
serviceList