Improving a script

Hi Folks - Noob here that needs some help. Ive been noodling around a script that takes a phone number from Quicksilver and then constructs a URL with that number and dials it. (JJ has been helping me with a javascript version - but I have not been able to get the variables in applescript to get passed to javascript. So … Instead of entering info on a form I figured out how to construct the URL that the form sends and send it directly.

Im stripping out spaces, ( , ) , and replacing a + with 011

any thoughts on how to make this more efficient are much appreciated!!! IP addresses changed to protect the innocent

using terms from application “Quicksilver”
on process text PHONE

	set testString to PHONE
	set newString to ""
	repeat with theChar in (every character of testString)
		if (theChar as string) is "+" then set newString to newString & "011"
		if (theChar as string) is not "+" then set newString to newString & theChar
	end repeat
	set testString to newString
	set newString to ""
	repeat with theChar in (every character of testString)
		if (theChar as string) is not "-" then set newString to newString & theChar
	end repeat
	set testString to newString
	set newString to ""
	repeat with theChar in (every character of testString)
		if (theChar as string) is not " " then set newString to newString & theChar
	end repeat
	set testString to newString
	set newString to ""
	repeat with theChar in (every character of testString)
		if (theChar as string) is not "(" then set newString to newString & theChar
	end repeat
	set testString to newString
	set newString to ""
	repeat with theChar in (every character of testString)
		if (theChar as string) is not ")" then set newString to newString & theChar
	end repeat
	
	set url_dial to "http://255.355.255.255/webportal/CustomClientServlet?NAV_FUNCTION=CALL_CONTROL&CALL_CONTROL_FUNCTION=DIAL&DIAL=" & newString
	display dialog url_dial
	tell application "Safari"
		activate
		open location url_dial
		delay 2
		repeat with w in windows
			tell w to close
		end repeat
		
	end tell
end process text

end using terms from

A better way to do your text editing would be to use text item delimiters. You use these to break the text into the bits either side of the characters (or longer strings) you don’t want, then set them to another value which is inserted into the gaps when you join the pieces together again. To zap something altogether, you use an empty string when rejoining the pieces.

Your method for addressing Quicksilver looks a bit strange, but I don’t know anything about that app.

(There is a way to avoid three of the eleven delimiter settings that occur here, but it’s really not worth the bother…) :slight_smile:

An even better solution is to run through the characters once and build one string as a result.

As it stands your script runs through the phone number several times… once to filter the +, then once to filter the ‘-’, then once more to filter spaces, and so on and so on.

Compacting this into a single repeat will improve things significantly (although I wouldn’t expect the original script to be very slow since phone number strings aren’t typically very long, but I digress…)


property badChars : {space, "(", ")", "-"} -- characters to filter out

set testString to PHONE
set newString to ""
repeat with theChar in (every character of testString)
	if (theChar as string) is "+" then
		set newString to newString & "011"
	else if (theChar as string) is not in badChars then
		set newString to newString & theChar
	end if
end repeat
set testString to newString

If your original phone number string is a typical 14 character string like “(123) 456 7890”, this script will run through 14 iterations to produce the result. By comparison your original script will run 88 iterations, 6 times for each character in the string.

Or you could useJon’s Phone Tool which includes a QuickSilver script, can dial via a URL of your choice, and has robust Dialing Rules (rules for transforming a number to proper formatting for different call types: e.g., local, domestic long distance, international long distance, etc.). It has other features as well including a call log, call timer, and more.

Jon

Thanks everyone for their help and comments - I appreciate it!!!1