Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Hughmac

macrumors 603
Original poster
Feb 4, 2012
6,047
33,087
Kent, UK
I got fed up with having too many clicks for my bluetooth headphones, so I made a quick couple of apps to help, and they reside in the dock for instant use.

The first 2 are actually the same script, but just with different names and icons, and are to set the sound output to bluetooth or other external source.

The last sets the sound back to internal speakers.

As they were made on a PPC machine I'm not sure if they will work on an Intel Mac.

Enjoy !!

Cheers :)

Hugh
 

Attachments

  • Set Bluetooth.zip
    192.1 KB · Views: 81
  • Set Sound Output.zip
    46.8 KB · Views: 78
  • Set Speakers.zip
    228.5 KB · Views: 75
Oh, and here's the scripts if you want to make your own or improve on them - I actually got the idea from somewhere on the net but can't remember the site.

Code:
[B]Choose sound output:-[/B]

tell application "System Preferences" to activate
tell application "System Events"
	get properties
	tell process "System Preferences"
		click menu item "Sound" of menu "View" of menu bar 1
		delay 2
		set theRows to every row of table 1 of scroll area 1 of ¬
			tab group 1 of window "sound"
		set theOutputs to {} as list
		repeat with aRow in theRows
			copy (value of text field 1 of aRow as text) to the end of theOutputs
		end repeat
		tell application "Finder"
			activate
			set desiredOutput to display dialog ¬
				"Choose Sound Output: " buttons theOutputs
		end tell
		repeat with aRow in theRows
			if (value of text field 1 of aRow as text) is equal to ¬
				(button returned of desiredOutput as text) then
				set selected of aRow to true
				exit repeat
			end if
		end repeat
	end tell
end tell
tell application "System Preferences" to quit

[B]Choose specific sound output (e.g bluetooth music):-[/B]

tell application "System Preferences" to activate
tell application "System Events"
	get properties
	tell process "System Preferences"
		click menu item "Sound" of menu "View" of menu bar 1
		delay 2
		set theRows to every row of table 1 of scroll area 1 of ¬
			tab group 1 of window "sound"
		set theOutputs to {} as list
		repeat with aRow in theRows
			copy (value of text field 1 of aRow as text) to the end of theOutputs
		end repeat
		tell application "Finder"
			activate
			set desiredOutput to display dialog ¬
				"Choose Sound Output: " buttons theOutputs default button "bluetooth music"
		end tell
		repeat with aRow in theRows
			if (value of text field 1 of aRow as text) is equal to ¬
				(button returned of desiredOutput as text) then
				set selected of aRow to true
				exit repeat
			end if
		end repeat
	end tell
end tell
tell application "System Preferences" to quit


[B]Set sound to internal speakers:-[/B]

tell application "System Preferences" to activate
tell application "System Events"
	get properties
	tell process "System Preferences"
		click menu item "Sound" of menu "View" of menu bar 1
		delay 2
		set theRows to every row of table 1 of scroll area 1 of ¬
			tab group 1 of window "sound"
		set theOutputs to {} as list
		repeat with aRow in theRows
			if (value of text field 1 of aRow as text) ¬
				is equal to "Internal speakers" then
				set selected of aRow to true
				exit repeat
			end if
		end repeat
	end tell
end tell
tell application "System Preferences" to quit

Cheers :)

Hugh
 
Way back when my son's Mac was an iBook and because the antenna was trashed when we went to Starbucks I had to share my connection with him.

Got old real fast opening up System Preferences and turning on Internet Sharing. It got frustrating having to remember to turn it off when I got home.

So, I used Lingon to write a launch daemon that continuously checked to see if the Airport preferences file changed. The daemon would run an Applescript any time it detected a change. Since that preference change happens any time you switch a network my Applescript triggered any time I walked into Starbucks and anytime I came home.

The Applescript itself queried the SSID of the network and if it matched either my home or Starbucks netword SSIDs GUI scripting of System Preferences kicked in to turn Internet sharing on or off. I also managed to find a setting that did this without showing System Preferences itself.

Since it was written as an app that also made it easy to hide it from the dock and the app switcher by using RocketDock. Essentially with the pieces put together I had a Mac that silently switched sharing on or off depending on network. I even included code for Growl so the Mac would tell me if sharing was on or off.

I used a combination of Applescript and do shell script scripting in the app.

Since your scripts are manipulating System Preferences, my scripts could probably be modified so that yours could work automatically based on your requirements.

LocationX which is the ONLY location aware app that will work for my Macs can also trigger Applescripts as part of it's features. So you could also couple your scripts with that so that they trigger based on location.
 
Last edited:
So, you think I could use Lingon to run one of my scripts if it detects bluetooth being turned on?

I've found Lingon 2.1.1 (freeware) on MajorGeeks and downloaded it, but I fear it will be heavy going for me to work it out straight away.
I shall give it a go though :)

Cheers :)

Hugh
 
So, you think I could use Lingon to run one of my scripts if it detects bluetooth being turned on?

I've found Lingon 2.1.1 (freeware) on MajorGeeks and downloaded it, but I fear it will be heavy going for me to work it out straight away.
I shall give it a go though :)

Cheers :)

Hugh
Yes. If BT works the same way as Airport does then when it checks for connections then if the pref file is modified Lingon will trigger your script. Lingon writes launch daemons. Just create a new one in the User section. Don't mess with anything else. You just need to name the daemon, provide the paths to the file to watch and the script to trigger and then save the daemon.

Note that the way Lingon works if you need to make a correction to it, you have to delete the old daemon and create a new one.
 
Thanks both ↑↑, I will have a go tomorrow sometime, around the fixing of light fittings, painting fences if it ever stops raining, new outside light as well, repairing sewing machine, changing the toilet valve, door painting x 5, laying paving slabs to make a path across our waterlogged lawn, trying to make XP run better in a PBG4, etc etc :D

All things saved up for my week off work; I work harder at home than I ever do in my job.

Cheers :)

Hugh
 
Thanks both ↑↑, I will have a go tomorrow sometime, around the fixing of light fittings, painting fences if it ever stops raining, new outside light as well, repairing sewing machine, changing the toilet valve, door painting x 5, laying paving slabs to make a path across our waterlogged lawn, trying to make XP run better in a PBG4, etc etc :D

All things saved up for my week off work; I work harder at home than I ever do in my job.

Cheers :)

Hugh
Can you weedwhack my front lawn? The weeds are pretty high! Oh wait…I did that already on Saturday, never mind. I hate yardwork! :D
 
Code:
tell application "System Preferences" to activate
tell application "System Events"
	get properties
	tell process "System Preferences"
		click menu item "Sound" of menu "View" of menu bar 1
		delay 2
		set theRows to every row of table 1 of scroll area 1 of ¬
			tab group 1 of window "sound"
		set theOutputs to {} as list
		repeat with aRow in theRows
			copy (value of text field 1 of aRow as text) to the end of theOutputs
		end repeat
		tell application "Finder"
			activate
			set desiredOutput to display dialog ¬
				"Choose Sound Output: " buttons theOutputs
		end tell
		repeat with aRow in theRows
			if (value of text field 1 of aRow as text) is equal to ¬
				(button returned of desiredOutput as text) then
				set selected of aRow to true
				exit repeat
			end if
		end repeat
	end tell
end tell
tell application "System Preferences" to quit
<snip>

Just out of curiosity, why the delay 2? I've been having issues with not being able to get to the sound control panel, and this delay now works. Was that your solution for that problem?

Cheers
 
I wasn't aware of any problem in the first place, as I copied the script from a Mac site (can't remember which) and then made it into an app.

Cheers :)

Hugh
 
I would suspect any delay is to give the Mac time to perform the requested action before moving on to the next action. I'm just guessing here.

I have one script that I force to wait 3 minutes before it starts a copy operation because things go screwy when someone is writing a file to the network and the script starts copying the file. 3 minutes is enough time for the write operation to complete before my script kicks in.

Since this is GUI scripting I would suspect this to be even more the case as the Mac tends to ignore any new actions sent when it's already performing a previous action.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.