URL Roulette... Easter Weekend Amusement

I’ve been having some fun with this… hope you all might too.

The code may be arcane, but…

Critiques welcome… and many thanks for examples found here.

Happy Resurrection Day

Peter B.


property text_returned : ""

tell me to activate

display dialog "Enter A URL Or JavaScript Command..." buttons {"Run JavaScript", "Open Location", "Open Web Location"} default button 3 default answer text_returned with title "URL Roulette"

copy the result as list to {text_returned, button_pressed}

if button_pressed is "Run Javascript" then
	if text_returned does not start with "javascript:" then
		set the_command to "javascript:" & text_returned & ";"
	else
		set the_command to text_returned
	end if
	
	tell application "Safari"
		activate
		try
			document 1
		on error
			make new document
		end try
		do JavaScript the_command in document 1
	end tell
end if

if button_pressed is "Open Location" then
	display dialog "Add Prefix...?" & return & return & "URL:  " & text_returned & return buttons {"Cancel", "No... Continue", "Yes"} default button 3
	set prefix_button to button returned of result
	
	if prefix_button is "Yes" then
		set the_prefix to choose from list {"http://www.", "ftp://", "file:///", "file://localhost/", "file:///Volumes/"}
		set the_location to the_prefix & text_returned as text
		
		if the_prefix is false then
			return
			error -128
		end if
		
		if the_location contains " " then
			set _Priors_ to AppleScript's text item delimiters
			set AppleScript's text item delimiters to " "
			set the_location to every text item of the_location
			set AppleScript's text item delimiters to "%20"
			set the_location to the_location as string
			set AppleScript's text item delimiters to _Priors_
			--display dialog the_location
			open location the_location
		else if the_location does not contain " " then
			set the_location to the_location
			--display dialog the_location
			open location the_location
		end if
		
	end if
	
	if prefix_button is "No... Continue" then
		set the_location to text_returned as text
		if the_location contains " " then
			set _Priors_ to AppleScript's text item delimiters
			set AppleScript's text item delimiters to " "
			set the_location to every text item of the_location
			set AppleScript's text item delimiters to "%20"
			set the_location to the_location as string
			set AppleScript's text item delimiters to _Priors_
			--display dialog the_location
			open location the_location
		else if the_location does not contain " " then
			set the_location to the_location
			--display dialog the_location
			open location the_location
		end if
	end if
end if

if button_pressed is "Open Web Location" then
	if text_returned does not start with "http://" then
		set web_location to "http://" & text_returned
	else
		set web_location to text_returned
	end if
	
	tell application "Safari"
		activate
		try
			document 1
		on error
			make new document
		end try
		tell front document
			set URL to web_location
		end tell
	end tell
end if