Goto home URL from any page - works out domain name

I do this manual all the time you in a page say “http://bbs.applescript.net/post.php?fid=11” and you want to go to the home page “http://www.applescript.net/

Ive put mine in the Applescript menu, call it home page

It works wonders

If someone would come up with all suffixes it would be prefer

property TheURL : "http://www.macscripter.net/"
property SuffixOptions : {".com", ".net", ".org", ".info", ".us", ".biz", ".tv", ".mobi", ".cc", ".ws", ".bz", ".tc", ".vg", ".ms", ".gs", ".name", ".co.uk", ".de", ".be", ".eu", ".at", ".com.mx", "org.uk", ".me.uk", ".co.nz", ".net.nz", ".org.nz", ".cn", ".tw"}

tell application "Safari"
	tell document 1
		set TheURL to URL
	end tell
end tell

set JustDomain to FindDomainName(TheURL)

if JustDomain = TheURL then
	set GotoURL to TheURL
else
	set GotoURL to "http://www." & JustDomain
end if

tell application "Safari"
	tell document 1
		set URL to GotoURL
	end tell
end tell

on FindDomainName(TheURL)
	set TheSuffix to ""
	repeat with CurrentSuffix in SuffixOptions
		if TheURL contains (CurrentSuffix & "/") then
			set TheSuffix to CurrentSuffix
			exit repeat
		end if
	end repeat
	if TheSuffix = "" then
		return TheURL -- couldn't find a Suffix and return orginal URL
	else
		set SuffixOffset to offset of (CurrentSuffix & "/") in TheURL
		set JustDomain to (characters 1 thru (SuffixOffset - 1) of TheURL) as string
		set PointOffSet to 0
		repeat with NegOffSet from (length of JustDomain) to 1 by -1
			if character NegOffSet of JustDomain is "." or character NegOffSet of JustDomain is "/" then
				set PointOffSet to NegOffSet
				exit repeat
			end if
		end repeat
		set JustDomain to (characters (PointOffSet + 1) thru (length of JustDomain) of JustDomain as string) & CurrentSuffix
		return JustDomain
	end if
end FindDomainName

How this work for you.

tell application "Safari"
	tell document 1
		set TheURL to URL
		set slash to "/"
		set counter to ""
		set GotoURL to do shell script "dirname " & quoted form of TheURL
		set ch to items of (characters of GotoURL as list)
		repeat with i from 1 to number of items in ch
			set this_item to item i of ch
			if this_item is slash then
				set counter to counter + 1
			end if
		end repeat
		if counter is greater than 3 then
			set counter to counter - 2
			repeat counter times
				set GotoURL to do shell script "dirname " & quoted form of GotoURL
			end repeat
		end if
	end tell
end tell
tell application "Safari"
	tell document 1
		set URL to GotoURL
	end tell
end tel

EDIT* forgot about if url has multiple “/” *fixed.

I can see your logic, find between the second slash ‘/’ and the thrid. Thats the host, I want the domain name. If you want to go home of the host then use this

tell application "Safari"
	activate
	set the TheHost to (do JavaScript "location.host" in document 1)
	tell document 1
		set URL to ("http://" & TheHost)
	end tell
end tell

great for http://en.wikipedia.org/ when you what to go to english sub site en.wikipedia.org but the domain name is wikipedia.org and the defualt homepage is wikipedia.org or www.wikipedia.org

The problem is the domain name is 1 or 2 ‘.’ away eg. or .ru (1) russia or .co.nz (2) New Zealand

Does someone know a site where there a list of all suffix?

Hey, Thanks for the tip on Location.host.

I did not read the post properly :rolleyes:

So using your last script (please tell me if I am barking up the wrong tree …again)
does this get rid of the need for the suffix

tell application "Safari"
	activate
	set the TheHost to (do JavaScript "location.host" in document 1)
	tell document 1
		set x to "."
		set cc to offset of x in TheHost
		set Domain to characters cc thru -1 of TheHost
		set URL to ("http://www" & Domain )
	end tell
end tell

How will either of you handle a site that doesn’t include a host (e.g. http://macscripter.net/books/)?

???

So is http://macscripter.net/ a sub domain??? of applescript.net or am I really confused.

My first script will do http://macscripter.net/books/ even throw it doesn’t exist

comes up with http://www.macscripter.net/

work on http://subdomain.book.macscripter.net/books/

also comes up with http://www.macscripter.net/

also work with also http://www.comment.com/

where there two .com thats why search for .com/ com with the slash

doesn’t work on

http://www.apple.ru/

simple because .ru is not in SuffixOptions list

mark I learn something off you, a -1 with thru go to the end, not logical but works, my way using length eg

set Thehost to "[url=http://www.macscripter.net]www.macscripter.net[/url]" as string
set FirstDomain to characters 5 thru -1 of Thehost as string
set SecondDomain to characters 5 thru (length of Thehost) of Thehost as string

Here an example of use

This script save the webpage text to text file named host and timestamp
Achrive into host folder in a a chest in extacts, nice UNIX method making folders
Puts the URL in the comments for later getting

tell application "Safari"
	activate
	set TheText to text of document 1
	set TheURL to URL of document 1
	set TheHost to (do JavaScript "location.host" in document 1)
end tell

set TimeStamp to do shell script "date -n +%Y-%m-%d-%H-%M-%S"
set thepath to ((path to desktop) as string) & "chest:extracts:" & TheHost & ":"
set TheName to TheHost & "-" & TimeStamp & ".txt"
do shell script "mkdir -p " & (POSIX path of thepath)

set theFile to thepath & TheName

try
	set fileref to (open for access theFile with write permission)
	set eof fileref to 0
	write TheText to fileref as string
	close access fileref
on error number x
	display dialog "Error " & x & ": did not write"
	close access fileref
end try

tell application "Finder"
	set comment of file theFile to TheURL
end tell

better yet use my script like this

property TheURL : "http://www.macscripter.net/"
property SuffixOptions : {".com", ".net", ".org", ".info", ".us", ".biz", ".tv", ".mobi", ".cc", ".ws", ".bz", ".tc", ".vg", ".ms", ".gs", ".name", ".co.uk", ".de", ".be", ".eu", ".at", ".com.mx", "org.uk", ".me.uk", ".co.nz", ".net.nz", ".org.nz", ".cn", ".tw"}

tell application "Safari"
	activate
	set TheText to text of document 1
	set TheURL to URL of document 1
end tell
set TheHost to FindDomainName(TheURL)
set TimeStamp to do shell script "date -n +%Y-%m-%d-%H-%M-%S"
set thepath to ((path to desktop) as string) & "chest:extracts:" & TheHost & ":"
set TheName to TheHost & "-" & TimeStamp & ".txt"
do shell script "mkdir -p " & (POSIX path of thepath)

set theFile to thepath & TheName

try
	set fileref to (open for access theFile with write permission)
	set eof fileref to 0
	write TheText to fileref as string
	close access fileref
on error number x
	display dialog "Error " & x & ": did not write"
	close access fileref
end try

tell application "Finder"
	set comment of file theFile to TheURL
end tell


on FindDomainName(TheURL)
	set TheSuffix to ""
	repeat with CurrentSuffix in SuffixOptions
		if TheURL contains (CurrentSuffix & "/") then
			set TheSuffix to CurrentSuffix
			exit repeat
		end if
	end repeat
	if TheSuffix = "" then
		return "Misc"
	else
		set SuffixOffset to offset of (CurrentSuffix & "/") in TheURL
		set JustDomain to (characters 1 thru (SuffixOffset - 1) of TheURL) as string
		set PointOffSet to 0
		repeat with NegOffSet from (length of JustDomain) to 1 by -1
			if character NegOffSet of JustDomain is "." or character NegOffSet of JustDomain is "/" then
				set PointOffSet to NegOffSet
				exit repeat
			end if
		end repeat
		set JustDomain to (characters (PointOffSet + 1) thru (length of JustDomain) of JustDomain as string) & CurrentSuffix
		return JustDomain
	end if
end FindDomainName

needs a bit of work, blanks results should not try and write, no documents

simple change

set TheText to source of document 1
set thepath to ((path to desktop) as string) & “chest:source:” & TheHost & “:”

and it save the source as txt