Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > I need a little AppleScript help

I need a little AppleScript help
Thread Tools
Uncle Skeleton
Addicted to MacNN
Join Date: Nov 2002
Location: Rockville, MD
Status: Offline
Reply With Quote
Dec 10, 2005, 10:56 PM
 
I want to change the Sound Output device in System Prefs by applescript. I've gotten as far as this:

tell application "System Preferences"
activate
set current pane to pane "com.apple.preference.sound"
reveal (first anchor of current pane whose name is "Output")
end tell

But nothing I've found online seems to work, as far as changing the options in that pane. What do I do?

TIA
     
Diggory Laycock
Professional Poster
Join Date: Oct 2001
Location: London
Status: Offline
Reply With Quote
Dec 11, 2005, 08:22 AM
 
I happened to need the same thing a few months ago:

[php]
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 "Internal Speakers"
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
[/php]

However - I'd recommend the free SoundSource menuItem instead:

http://www.rogueamoeba.com/freebies/
     
Uncle Skeleton  (op)
Addicted to MacNN
Join Date: Nov 2002
Location: Rockville, MD
Status: Offline
Reply With Quote
Dec 13, 2005, 09:28 PM
 
It seems that whatever my first call to System Events is, I get the error:
AppleScript Error
System Events got an error:
NSReceiverEvaluationScriptError: 4

Know what that means? I know I've used System Events in the past, but it was a long time ago (like 10.2 days) and I don't remember how I did it or what for.

The reason I want applescript is I also want to make it change monitor configuration at the same time. It's for when I switch screens to the projector, which is in another room with other speakers.

Thanks for your help
     
Uncle Skeleton  (op)
Addicted to MacNN
Join Date: Nov 2002
Location: Rockville, MD
Status: Offline
Reply With Quote
Dec 13, 2005, 09:36 PM
 
drrr. I needed to "enable access for assistive devices." why are easy things always so hard?
     
suthercd
Senior User
Join Date: Oct 2000
Location: Midwest
Status: Offline
Reply With Quote
Dec 14, 2005, 09:24 AM
 
On my machine I needed to add
Code:
click radio button "Output" of tab group 1 of window "Sound"
after "delay 2" to access the list of available output devices. OS X 10.4.x

??

Craig
     
Uncle Skeleton  (op)
Addicted to MacNN
Join Date: Nov 2002
Location: Rockville, MD
Status: Offline
Reply With Quote
Dec 17, 2005, 04:14 AM
 
yeah, I added that too for completeness, but it was working without it as long as no one ever used the Sound pane manually and left it on another thing. Here's my finished (functional) script. I might add other things for flash, like I'm going to try to have it change its own icon between two things. Also I wish the delays were adaptive somehow.

[php]tell application "System Preferences" to activate

set goingOut to 1

tell application "System Events"
get properties
tell process "System Preferences"
click menu item "Sound" of menu "View" of menu bar 1
delay 1
tell radio button "Output" of tab group 1 of window "sound"
click
end tell
set deviceTable to table 1 of scroll area 1 of tab group 1 of window "sound"
set theRows to every row of deviceTable
if (selected of the first item of theRows) then
set selected of the third item of theRows to true
else
set goingOut to 0
set selected of the first item of theRows to true
end if
click menu item "Displays" of menu "View" of menu bar 1
delay 3
tell radio button "Arrangement" of tab group 1 of window "SyncMaster"
click
end tell
tell group 1 of tab group 1 of window "SyncMaster"
tell checkbox "Mirror Displays"
click
end tell
end tell
end tell
end tell
tell application "System Preferences" to quit
[/php]
     
   
 
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 05:42 AM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,