I’m new to the world of Applescript and am having fun making my mac speak to me etc
I’d like to use Applescript to cycle through some proxy servers on my mac. So I’ve figured out how to use;
do shell script "sudo networksetup -setwebproxy airport 200.25.201.100 80"
and change proxy servers and even bring up prompts to pick between Airport and Ethernet for example but what I need to be able to do is take an input of eliminated proxies like so;
set proxies to "200.25.201.100:80
200.75.92.71:8080
67.165.106.164:8085
68.196.172.210:8085
200.217.189.10:3128
211.115.185.50:8080"
OR (whichever is easier)
set proxies to "200.25.201.100:80,200.75.92.71:8080,67.165.106.164:8085,68.196.172.210:8085,200.217.189.10:3128,211.115.185.50:8080"
For each of these proxies I need to run the change function and be able to split the proxy from the port.
What’s the equivalent of explode and the For loop in PHP?
on run
set proxies to "200.25.201.100:80
200.75.92.71:8080
67.165.106.164:8085
68.196.172.210:8085
200.217.189.10:3128
211.115.185.50:8080"
set proxies to my gettxtitems(ASCII character 10, proxies)
repeat with proxie in proxies
set {domain, pport} to my gettxtitems(":", proxie)
display dialog "Domain: " & domain & return & "Port: " & pport
end repeat
end run
-- I am returning the text items of a text separated by the given delimiter
on gettxtitems(delim, txt)
set olddelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to {delim}
set txtitems to text items of txt
set AppleScript's text item delimiters to olddelims
return txtitems
end gettxtitems
It seems that Martin and I were typing at the same time. Our code uses the same mechanism. I also included a note about sudo and do shell script at the end of this post.
There is no direct equivalent to explode, but you can get very close with a simple use of text item delimiters and text items. The loop is repeat with var in listValue . end. Once you have an equivalent to explode, you can go with either input string, though AppleScript does have a built-in paragraphs reference that can break up the string that uses values on separate lines.
Check out the AppleScript Language Guide to read about all the basic syntax and built-in features of AppleScript.
-- like PHP's explode? http://php.net/manual/en/function.explode.php
to explode(delimiter, str)
set text item delimiters to {delimiter}
text items of str
end explode
to explodeWithLimit(delimiter, str, limit)
if limit is 0 then set limit to 1
set theItems to explode(delimiter, str)
if limit < 0 then
set limit to (length of theItems) + limit
items 1 through (limit - 1) of theItems
else
if limit > length of theItems then set limit to length of theItems
items 1 through limit of l
end if
end explodeWithLimit
set l to {}
set proxies_comma to "200.25.201.100:80,200.75.92.71:8080,67.165.106.164:8085,68.196.172.210:8085,200.217.189.10:3128,211.115.185.50:8080"
repeat with proxy in explode(",", proxies_comma)
set {theIP, thePort} to explode(":", proxy)
set end of l to "networksetup . " & quoted form of theIP & space & quoted form of thePort
end repeat
set proxies_lines to "200.25.201.100:80
200.75.92.71:8080
67.165.106.164:8085
68.196.172.210:8085
200.217.189.10:3128
211.115.185.50:8080"
repeat with proxy in paragraphs of proxies_lines
set {theIP, thePort} to explode(":", proxy)
set end of l to "network setup . " & quoted form of theIP & space & quoted form of thePort
end repeat
l