Pass a string to a URL and open in a new tab

This has to be a pretty obvious fix. Thanks for the ideas. I’m trying to pass this_text to a URL to open in a new tab, but I’m doing something wrong, as the new tab is empty.

What I do is this: I copy mydomain.com to the clipboard, run the script, and then the URL whois.com/whois/mydomain.com opens in the front tab:

tell application "System Events"
	set this_text to (get the clipboard) as string
	open location "http://www.whois.com/whois/" & this_text
end tell

But this one just opens a new and empty tab:

set this_text to (get the clipboard) as string
set theURL to "http://www.whois.com/whois/" & this_text

tell application "Safari"
	activate
	try
		tell window 1 to set current tab to make new tab with properties {URL:theURL}
	on error
		open location theURL
	end try
	
end tell

Hi,

try this, it considers also the cases no document and blank document without tabs


set this_text to the clipboard
set theURL to "http://www.whois.com/whois/" & this_text

tell application "Safari"
	activate
	if (count documents) is 0 then make new document
	tell window 1
		set tabTest to URL of tab 1
		try
			tabTest -- throws an error if the window has no tabs
			set current tab to (make new tab with properties {URL:theURL})
		on error
			set URL of tab 1 to theURL
		end try
	end tell
end tell


Thanks, but that still gives me an empty tab; I’m trying to get the tab to open http://www.whois.com/whois/mydomain.com

Hey There,

Your script as is works perfectly well for me with a basic domain like ‘macscripter.net’. Changing it just a bit for clarity also works:


tell window 1 to set current tab to (make new tab with properties {URL:theURL})

Perhaps you should provide an example of your look-up text that doesn’t work.

I would grab that text from the Clipboard and make sure there’s nothing weird in it:


set _clip to "'" & (get the clipboard) & "'"
set the clipboard to _clip