PDA

View Full Version : Another "Could AppleScript do this?" Thread




stevietheb
May 31, 2006, 04:26 PM
I take my iBook to and from work. It functions as my main computer 99% of the time. However, at work I have a nice large LCD that I use (thanks to the screen-span hack). Because of the added real estate, and the difference in network, I have slightly different configurations: (a) at work I like to have the Dock on the bottom instead of the right where I have it at home, and (b) at work I use "Automatic" as my network Location.

Could these be changed via an AppleScript that I could put up in the scripts menu?

Thanks in advance for any and all help!



mduser63
May 31, 2006, 06:08 PM
Yes I think both of those are possible, at the very least with GUI scripting. Both of those settings are available in System Preferences. System Preferences doesn't have a good AppleScript dictionary, but it lends itself to GUI scripting fairly well. I'll see if I can get something put together fairly quickly.

mduser63
May 31, 2006, 06:15 PM
Ok here are scripts to change the Dock settings. They were tested on my machine only, and I have 10.4.6. I don't know if they'll work in Panther, although I doubt it.

To set the Dock to the left side of the screen:

--Open and activate System Preferences
tell application "System Preferences" to activate

--Attempt to change settings using System Events
tell application "System Events"
tell process "System Preferences"
try
--Open the "Dock" pane
click menu item "Dock" of menu "View" of menu bar 1
delay 2
tell window "Dock"
click radio button "Left"
end tell
on error theError
--An error occured
display dialog ("Sorry, an error occured while altering Dock settings:" & return & theError) buttons "OK" default button "OK"
end try
end tell
end tell
delay 1
tell application "System Preferences" to quit

And to set the Dock back to the bottom:

--Open and activate System Preferences
tell application "System Preferences" to activate

--Attempt to change settings using System Events
tell application "System Events"
tell process "System Preferences"
try
--Open the "Dock" pane
click menu item "Dock" of menu "View" of menu bar 1
delay 2
tell window "Dock"
click radio button "Bottom"
end tell
on error theError
--An error occurred
display dialog ("Sorry, an error occured while altering Dock settings:" & return & theError) buttons "OK" default button "OK"
end try
end tell
end tell
delay 1
tell application "System Preferences" to quit

To use these you'll need to copy them into script editor and save them as scripts. Then put them in /Library/Scripts so that they're in the Script Menu. You'll also need to enable GUI scripting by checking the box next to "Enable Assistive Devices" in Universal Access preferences.

mduser63
May 31, 2006, 06:38 PM
Here's what I've got for changing the network location to automatic. It doesn't work for some reason that I can't figure out. Anyone else got any ideas?

--Open and activate System Preferences
tell application "System Preferences" to activate

--Attempt to change settings using System Events
tell application "System Events"
tell process "System Preferences"
try
--Open the "Network" pane
click menu item "Network" of menu "View" of menu bar 1
delay 4
click menu item "Automatic" of menu 1 of pop up button 1 of window "Network"
on error theError
--An error occured
display dialog ("Sorry, an error occured while altering Network settings:" & return & theError) buttons "OK" default button "OK"
end try
end tell
end tell
delay 1
tell application "System Preferences" to quit

stevietheb
Jun 1, 2006, 05:25 AM
The scripts for the Dock work just as expected. As you indicated, the one for the Network is not working at this moment. Maybe later today I'll see if I can rig something together.

I'd like these combined into the same script so that I can click "Home" in the scripts menu or "Work" in the scripts menu and have everything change. But, first, we must get the individual chunks running.

Thanks so much, this really is a big help!

stevietheb
Jun 1, 2006, 05:44 AM
googling around I found this command:

do shell script "scselect <location>"


So, doing this: do shell script "scselect Automatic" appears to work.

stevietheb
Jun 1, 2006, 08:23 AM
Using this Prefab UI utility to help me create the script (available here (http://www.prefab.com/uibrowser/)), this is what I've come up with (this example is my "Home" script):
activate application "Finder"
tell application "System Events"
get system attribute "sysv"
if result is greater than or equal to 4144 then -- Mac OS X 10.3.0
if UI elements enabled then
tell application process "Finder"
-- GUI Scripting statements:
click menu item "Position on Right" of menu 1 of menu item "Dock" of menu 1 of menu bar item "Apple" of menu bar 1

end tell
else
beep
display dialog "GUI Scripting is not enabled" & return & return & "Open System Preferences and check Enable Access for Assistive Devices in the Universal Access preference pane, then run this script again." with icon stop
if button returned of result is "OK" then
tell application "System Preferences"
activate
set current pane to pane "com.apple.preference.universalaccess"
end tell
end if
end if
else
beep
display dialog "This computer cannot run this script" & return & return & "The script uses GUI Scripting technology, which requires an upgrade to Mac OS X 10.3 Panther or newer." with icon caution buttons {"Quit"} default button "Quit"
end if
end tell

do shell script "scselect Home"

How to improve this?
(a) I know that a lot of the stuff in the AppleScript is needless.
(b) Perhaps making it so that when I run the script a pop-up asks me which location I'd like to use [only one script needed in scripts menu].
(c) Perhaps having the applescript recognize which I'm using and just switch to the other one [only one script needed in scripts menu].
(d) Finding a way that doesn't require the switch to Finder to change the dock.

The main advantage to this version is that it doesn't require the opening of System Preferences.

Thanks again...