I have no AppleScript experience! “ Just thought I’d make that clear
Still, into the abyss.
I would like to create a script that would make a new network location in system preferences & then set that location to make use of a remote proxy configuration file, finally making the new location the currently active one.
So far I can get System Preferences to activate with the Network pane showing but cannot get further. After viewing other scripts it appears I have to invoke System Events to manipulate the menu items & dialogue boxes, I think the problem is I am not describing the elements in the window acurately? How should I know which item is which button of which tab group etc?
If this is a) All listed on a webpage somewhere, please point me in the right direction!
or b) Way beyond a AS newbie like me, then please, put me out of my misery!
Any help gratefully appreciated,
Stephen
Model: White 2GHz MacBook
AppleScript: 1.10.7
Browser: Safari 419.3
Operating System: Mac OS X (10.4)
I don’t know how to make a new location, but if you have several fully set up then you can find out which one is active with this:
to getLocNames()
set locNames to {}
try
do shell script "scselect"
on error Loc
end try
set myLocs to paragraphs 2 thru -2 of Loc
repeat with aLoc in myLocs
set tid to AppleScript's text item delimiters
set AppleScript's text item delimiters to "("
set temp to last text item of aLoc
set AppleScript's text item delimiters to tid
if "*" is in contents of aLoc then
set end of locNames to "*" & text 1 thru -2 of temp
else
set end of locNames to text 1 thru -2 of temp
end if
end repeat
return locNames
end getLocNames
set Locs to getLocNames()
repeat with anItem in Locs
if "*" is in anItem then set CurrLoc to text 2 thru -1 of anItem
end repeat
and, if you know the names of the Locations (and be sure to use names without spaces, i.e. “23_Baxter_St”) then you can set the current location to it with this command:
Scripting the GUI is a real pain at times especially when you want to go has deep has you do.
tell application "System Preferences"
activate
set current pane to pane "Network"
end tell
tell application "System Events"
tell process "System Preferences"
set frontmost to true
tell window 1
click pop up button 1
tell pop up button 1
tell menu 1
click menu item "New Location."
return every UI element
end tell
end tell
end tell
end tell
end tell
The above script should take you a fraction deeper than you are already(but not all the way)
A couple of things here you may encounter errors if you already have a few locations, this is set up on my mac and i have only 1
if you notice the line “return every UI element” you can use this from time to time in your script to return certain info
regarding the buttons, drop down menus etc…
good Luck stick with it…
I have downloaded the UI Elements inspector, I can see it’s giving me the info I should need, just need to work out which bits?
Adam, erm, did I say I know nothing? - I think I may of over stated my experience! I’m sorry, I have no idea what you said Even so, I intend to find out one day. It’s a great site here & lots of helpful people - I just need pigeon sized steps.
Pidge1 thanks also, it does indeed get me a little further, I can also ‘see’ what’s occuring - so gives me stuff to build on. Can see the sense in returning the UI stuff here & there so will bear that in mind.
Cheers all. I do intend to stick with it, although I can feel myself falling over backwards looking up at the learning curve from here!!
Cheers, Steve
Model: White 2GHz MacBook
AppleScript: 1.10.7
Browser: Safari 419.3
Operating System: Mac OS X (10.4)
tells you the name of your current location and 2) will change your location. Then just use if/then statements to make it work for your situation. I don’t know how to make a new location other than GUI scripting.
-- change these 2 variables to the names of the 2 network loacations you want to switch between
set loc_home to "home" -- home is a name of one of my network locations
set loc_work to "work" -- work is a name of one of my network locations
set current_location to do shell script "scselect 2>&1 | grep '^ ' 2>&1 | grep '*' | cut -f 2 -d '(' | cut -f 1 -d ')'"
if current_location is loc_home then
do shell script "scselect '" & loc_work & "'"
delay 0.5
else
do shell script "scselect '" & loc_home & "'"
delay 0.5
end if
thanks for all the advice so far. I have made some progress, it’s not pretty, and I’m sure some of you guys & gals could make something much more efficient in minutes. I have two issues at the moment, along the lines of where I first got stuck… I can’t seem to find the right way to click the OK button after defining the name of the new location (hence the delay to allow me to click it manually) & I can not find the right way of describing the proxy pop-up menu item.
Any advice, as ever, very welcome
tell application "System Events"
if (name of processes) contains "System Preferences" then tell application "System Preferences" to quit
end tell
delay 1
tell application "System Preferences"
activate
set current pane to pane "com.apple.preference.network"
end tell
tell application "System Events"
tell process "System Preferences"
set frontmost to true
try
tell window 1
click pop up button 1
tell pop up button 1
tell menu 1
click menu item "New Location."
keystroke "Test"
delay 3
-- Trigger default "OK" button here
end tell
end tell
end tell
end try
tell application "System Events" to tell process "System Preferences"
set frontmost to true
tell window 1
click pop up button 2
tell pop up button 2
tell menu 1
click menu item "Airport"
end tell
end tell
end tell
end tell
delay 1
tell application "System Events" to tell process "System Preferences"
click radio button "Proxies" of tab group 1 of group 1 of window "Network"
end tell
end tell
end tell
delay 1
tell application "System Events" to tell process "System Preferences"
tell window 1
click pop up button 1 of tab group 1
tell pop up button 1
tell menu 1
click menu item "Using a PAC file"
keystroke "http://10.0.0.1/wpad/wpad.dat"
end tell
end tell
end tell
end tell
Cheers, Stephen.
Model: White 2GHz MacBook
AppleScript: 1.10.7
Browser: Safari 419.3
Operating System: Mac OS X (10.4)
I’m dead happy, I managed to guess how to click my “OK” button!
tell process "System Preferences"
click button "OK" of sheet 1 of window "network"
end tell
Still struggling to get to the pop up menu that selects “Configure Proxies:” in the proxy tab…?
Any clues much appreciated.
As an aside, my script seems riddled with End tell’s “ Is this normal? can I/should I thin them out, I think maybe I’m still instructing the same application (System Events) so do I need to keep ending it, or should I just plough on? Really they’re only there 'cause when I compile the editor keeps prompting for them - so I just keep adding them till it’s happy
I don’t know which “OK” button you mean, but take a look at Apple’s UIElementInspector
to figure out the UI elements: http://developer.apple.com/samplecode/UIElementInspector/
You can access the Configure Proxies menu with this:
tell application "System Preferences"
reveal anchor "Proxies" of pane id ¬
"com.apple.preference.network"
end tell
tell application "System Events"
tell process "System Preferences"
click pop up button 1 of group 1 of tab group 1 of group 1 of window "Network"
end tell
end tell
If your script compiles, all tell blocks are correct, but
you can “clean up” the tell blocks e.g. from
tell application "System Events" to tell process "System Preferences"
set frontmost to true
tell window 1
click pop up button 2
tell pop up button 2
tell menu 1
click menu item "Airport"
end tell
end tell
end tell
end tell
to
tell application "System Events" to tell process "System Preferences"
set frontmost to true
click pop up button 2 of window 1
click menu item "Airport" of menu 1 of pop up button 2 of window 1
end tell
Firstly, MUCH thanks to those that have contributed with advice & suggestions to my initial post. I have written my script, it does essentially(!) what I require, and has been an educational pursuit. Thanks to StefanK, it now even looks tidy!
There is a negative aspect, I have tried to run the script when copied to two friend’s Macs (saved as an application) & both times it has fallen over (at this stage I cannot give more details as to where) Can anybody shed any light as to why? Is there something inherently wrong with the script that would prevent it working on another machine?
Once again, many thanks & any tips or useful links appreciated.
Stephen.
display dialog "This script Creates a new Location named 'Work WLAN' in System Preferences, configures it and sets it as the active Location.
To return to 'Automatic' settings, please use your Apple Menu.
V1.1B" buttons {"Continue", "Cancel"} default button 2
tell application "System Events"
if (name of processes) contains "System Preferences" then tell application "System Preferences" to quit
end tell
delay 1
tell application "System Preferences"
activate
set current pane to pane "com.apple.preference.network"
end tell
tell application "System Events" to tell process "System Preferences"
set frontmost to true
click pop up button 1 of window 1
click menu item "New Location." of menu 1 of pop up button 1 of window 1
keystroke "Work WLAN"
delay 1
end tell
tell application "System Events" to tell process "System Preferences"
click button "OK" of sheet 1 of window 1
end tell
tell application "System Events" to tell process "System Preferences"
set frontmost to true
click pop up button 2 of window 1
click menu item "Airport" of menu 1 of pop up button 2 of window 1
end tell
delay 1
tell application "System Events" to tell process "System Preferences"
click radio button "Proxies" of tab group 1 of group 1 of window 1
end tell
tell application "System Preferences"
reveal anchor "Proxies" of pane id "com.apple.preference.network"
end tell
tell application "System Events" to tell process "System Preferences"
click pop up button 1 of group 1 of tab group 1 of group 1 of window 1
click menu item "Using a PAC file" of menu 1 of pop up button 1 of group 1 of tab group 1 of group 1 of window 1
keystroke "http://10.0.0.1/wpad/wpad.dat"
end tell
delay 1
tell application "System Events" to tell process "System Preferences"
click button "Apply now" of window "Network"
end tell
tell application "System Preferences" to quit
Doesn’t work on mine either, and it may be a matter of timing. Assuming they’re running the same version of OS X as you are but not the same processor speed, then try tinkering with or adding more delays where things don’t get done.
Hm, it works perfect on my machine (although my native language is german, I run an english system on my computer)
could it be a localization problem?
You can optimize your script deleting a couple of tell blocks.
The quit System Preferences at the beginning isn’t necessary, too
display dialog "This script Creates a new Location named 'Work WLAN' in System Preferences, configures it and sets it as the active Location.
To return to 'Automatic' settings, please use your Apple Menu.
V1.1B" buttons {"Continue", "Cancel"} default button 2
tell application "System Preferences"
activate
set current pane to pane "com.apple.preference.network"
end tell
tell application "System Events" to tell process "System Preferences"
set frontmost to true
click pop up button 1 of window 1
click menu item "New Location." of menu 1 of pop up button 1 of window 1
keystroke "Work WLAN"
delay 1
click button "OK" of sheet 1 of window 1
click pop up button 2 of window 1
click menu item "Airport" of menu 1 of pop up button 2 of window 1
delay 1
tell application "System Preferences" to reveal anchor "Proxies" of pane id "com.apple.preference.network"
click pop up button 1 of group 1 of tab group 1 of group 1 of window 1
click menu item "Using a PAC file" of menu 1 of pop up button 1 of group 1 of tab group 1 of group 1 of window 1
keystroke "http://10.0.0.1/wpad/wpad.dat"
delay 1
click button "Apply now" of window "Network"
end tell
tell application "System Preferences" to quit
Advice I often give from my own GUI scripting experiences…the “delay” command is your friend. I find the minimum delays are 0.2 for quick delays and 0.4 for “medium” delays. These interspersed in key places on a script running on a G5 or a G4 mirror will do wonders for UI scripts.
My new “technique” now on an UI script that seems to “go too fast” or "not always work, but sometimes does, is to stick a “delay delay_variable” command after every single UI command and tweak the delay variable until it works over at least 5 tries. Then go back and try removing delays until it breaks again, and slowly fine-tuning.
Key places for delays are any time a new window or sheet needs to reveal itself before some button or menu needs to be selected. UI scripts will fail because the sheet wasn’t full displayed and the item you wished to interact with technically wasn’t there the split second you tried to use it. Long popup menus or lists that take a second or so to load can cause issues too, as can lengthy strings “typed” with the keystroke command, and even copy-n-paste to the clipboard.
Blimey, does nobody sleep on this list! That has to some of the speediest feedback ever :lol:
Anyways, thanks. The delay thing does seem the most obvious, at least the thing has run on another machine so I guess I’l tinker a little more.
Thanks for the further house keeping Stefan, I did wonder if it would reduce a little more as I stripped it out, but copped out! (was too nervous to risk it!).
As a further point of interest, the aim is to make this script available at work on the default page of the proxy server for freelance users with ‘little silver or white - even black - boxes of joy’ What is the considered view, would it run as an app from the server as is? would this pose any obvious security risk - to client or server? Would it need to be downloaded & run locally?
I appreciate this is a scripting forum, but I would love to hear your insight,
Stephen.
Model: White 2GHz MacBook
AppleScript: 1.10.7
Browser: Safari 419.3
Operating System: Mac OS X (10.4)
I have an applescript that I want to monitor network requests, if there is a network request, the location is switched from a location I have called ‘no connection’ to ‘automatic’. If no traffic has been generated for a specific amount of time, I want the location to switch back to ‘no connection’.
I’m envisioning this happening with another shell script.
Do you know where I can look for shell scripting help? Trying to go through all the commands based on the command name is not always informative. I’ve been searching around a bit, and haven’t found the information yet.
If you want to make this process totally automatic then that’s much more complicated than my simple script. The only program that I know of that can monitor network traffic is a shell program called “tcpflow”. You could probably write a “stay-open” applescript that monitors the output from tcpflow and then does your tasks based on that output… i.e. if there’s no output from tcpflow for a period of time then your applescript would switch to the “no connection” location and vice versa.
You can find more about tcpflow by doing a search on this website and also a google search.