Can I get opinions on there being an easier way to do this?
I’ve set up a key-combo via Butler that pulls up this script from anywhere, no matter what I’m doing.
set getURL to display dialog "URL?" default answer "" buttons {"net", "org", "com"} default button 3
set theURL to text returned of getURL
set theDomain to button returned of getURL
if theURL begins with "http://www." then
set theURL2 to theURL
else if theURL begins with "www." then
set theUR2 to "http://" & theURL
else
set theURL2 to "http://www." & theURL
end if
if theURL2 ends with ".com" or ".net" or ".org" then
tell application "Safari"
activate
open location theURL2
end tell
else
tell application "Safari"
activate
open location theURL2 & "." & theDomain
end tell
end if
The point is I can type in a URL and it pipes it to Safari, and pulls it open as the frontmost window.
If I just pipe the input straight into Safari, I get an error, so I
wrote the two “If” routines to handle some possible combinations.
display dialog "URL?" default answer "" buttons {".net", ".org", ".com"} default button 3
set {text returned:theURL, button returned:theDomain} to result
-- Optional
if theURL is "" then set theURL to "http://www.google.com"
if theURL starts with "www." then
set theURL to "http://" & theURL
else if theURL does not start with "http://www." then
-- This isn't appropriate when you want to enter a sub-domain
set theURL to "http://www." & theURL
end if
if (text -5 thru -1 of theURL) is not in {".com", ".net", ".org"} then
-- This isn't appropriate for other domains
set theURL to theURL & theDomain
end if
tell application "Safari"
activate
open location theURL
end tell
Here’s another take, in case it’s of interest. The syntax correction noted near the end is essential!
set getURL to (display dialog "URL?" default answer "" buttons {"net", "org", "com"} default button 3)
set {text returned:theURL, button returned:theDomain} to getURL
if ((count theURL) is 0) then error number -128 -- Exit if theURL is "". (In lieu of a "Cancel" button!)
-- This doesn't force a "www.", since not all domains begin with that. (This site, for instance.)
if (theURL does not start with "http://") then set theURL to "http://" & theURL
-- 'if.or.or.' syntax corrected.
if not ((theURL ends with ".com") or (theURL ends with ".net") or (theURL ends with ".org")) then
set theURL to theURL & "." & theDomain
end if
tell application "Safari"
activate
open location theURL
end tell
I edited my post to use this instead (just to be different):
if (text -4 thru -1 of theURL) is not in {".com", ".net", ".org"} then
Edit: Actually, I would change that part to something else:
display dialog "URL?" default answer "" buttons {".net", ".org", ".com"} default button 3
set {text returned:theURL, button returned:theDomain} to result
if theURL is "" then error number -128 -- cancel
if (offset of "." in theURL) is 0 then
-- This works for other domains, as long as no subdomains are entered
set theURL to theURL & theDomain
end if
if theURL starts with "www." then
set theURL to "http://" & theURL
else if theURL does not start with "http://www." then
set theURL to "http://www." & theURL
end if
tell application "Safari"
activate
open location theURL
end tell
I think I’m gunna have to pick up an AppleScript Book (besides the ancient one I have on my shelf) or troll this site; that was all over my head, but I get parts of it.