Scripting Safari

So far I have had no success adapting my scripts from other browsers to get Safari to open a selected group of sites.

Does anybody know the correct terms. More to the point, how does one read the Dictionary for Safari to figure this out by yourself?

Thomas S. England
Decatur GA 30030
Portfolio:
http://englandphoto.com/portfolio//

Some samples: http://www.apple.com/applescript/safari/
Make new window and go to url x:

tell app "Safari"
	make new document at beginning of documents
	set URL of result to "http://www.google.com/"
end tell

Cheers!

I had come very close, trying some of the syntax from those examples.
Thanks.
Any idea how to open multiple sites?

Mmm… :shock: :?:

set list_of_sites to {"http://site.com","http://site.es","http://site.ru","http://site.net","http://site.ar","http://site.biz","http://site.org","http://site.nu","http://site.vi","http://site.ra","http://site.fr","http://site.uk","http://site.de","http://bbs.applescript.net","http://www.macscripter.net","http://www.osaxen.com","http://www.macscripter.net/script-builder.t"}

repeat with i in list_of_sites
	tell application "Safari" to set URL of (make new document at beginning of documents) to i
end repeat

Do you mean “Why do you know you must make new document at beginning of documents”?
You don’t know! But this is a common syntax for many apps… “open” will open local files. So, you must use “make” [what] “new document” [where] “at beginning of documents” (or “at end of documents”)…

I guess I thought the approach would at least mimic the scripts I use for iCab, or Navigator, or IE. Something like:

OpenURL “http://www.salon.com/” toWindow 1
OpenURL “http://www.ResExcellence.com/” toWindow 2

or

«event WWW!OURL» “http://www.usatoday.com/
«event WWW!OURL» “http://www.accessatlanta.com/ajc/

silly me

I’ve been checking it and seems that (as must be) Safari accepts a standard «event GURLGURL» (will compile as standard additions’ “open location”), and also it will open every url in a new window:

repeat with i in list_of_sites
	tell application "Safari" to open location i
end repeat

Anyway, I’m pretty sure Safari will add this event to its dictionary in next releases as “getUrl”, “openUrl” or whatever… :smiley:

would it be possible to edit apple’s open linked images script for safari to open all the linked pages on a page; i’m thinking: do a google search, then hit this script to open all the displayed links in tabs.

apple’s code, which includes a lot of javascripts (which is what make me hesitate to tinker):


	tell application "Safari"
	activate
	
	try
		
		(*	
		
		to have the script automatically open linked images in tabs, i changed: the "display dialog...." and "set the display...." lines, that is these lines....
		
	display dialog "Open Linked JPEG Images" & return & return & "This script will open all the thumbnail-linked JPEG images in the front document in new windows or new tabs." buttons {"Cancel", "Windows", "Tabs"} default button 3
		set the display_method to the button returned of the result
		
		to this next line:
		
		*)
		
		set the display_method to "Tabs"
		
		-- the number of images in the front document
		set the image_count to (do JavaScript "document.images.length" in document 1)
		
		-- the number of links in the front document
		set the link_count to (do JavaScript "document.links.length" in document 1)
		if the link_count is "0" then error "No links in this document."
		
		-- get the url of the directory containing the current page
		set the document_URL to (do JavaScript "document.URL" in document 1)
		set AppleScript's text item delimiters to "/"
		set the parent_URL to ((text items 1 thru -2 of the document_URL) as string) & "/"
		set AppleScript's text item delimiters to ""
		
		set the image_links to {}
		repeat with i from 0 to (the link_count - 1)
			-- get the text of the anchor link
			set this_text to (do JavaScript "document.links[" & (i as string) & "].text" in document 1)
			-- if the text of the anchor link is an image, add the link to the list
			if this_text contains "<IMG " then
				set this_URL to (do JavaScript "document.links[" & (i as string) & "].href" in document 1)
				if the this_URL ends with ".jpg" then
					set this_host to (do JavaScript "document.links[" & (i as string) & "].host" in document 1)
					if this_host is "" then
						set this_URL to the parent_URL & this_URL
					end if
					set the end of the image_links to this_URL
				end if
			end if
		end repeat
		
		if the image_links is {} then
			error "This page contains no thumbnailed images that linked to other JPEG images."
		end if
		
		if the display_method is "Tabs" then
			-- size the document to full screen    
			do JavaScript "moveTo(0,0);self.resizeTo(screen.availWidth,screen.availHeight)" in document 1
		end if
		
		repeat with i from 1 to number of items in the image_links
			set the image_URL to item i of the image_links
			if the display_method is "Windows" then
				set this_doc to make new document at beginning of documents
			else
				my new_tab()
			end if
			set the URL of document 1 to image_URL
		end repeat
		
	on error the error_message number the error_number
		if the error_number is not -128 then
			display dialog the error_message buttons {"OK"} default button 1
		end if
	end try
end tell

on new_tab()
	tell application "Safari" to activate
	tell application "System Events"
		tell process "Safari"
			click menu item "New Tab" of menu "File" of menu bar 1
		end tell
	end tell
end new_tab


tell application "Safari" to set allLinks to ¬
	do JavaScript "for (i = 0; i < document.links.length; i++){
	l += document.links[i].href + '\r';
}
l" in document 1
paragraphs of result

This should return a list of all links. However, you should filter the links you don’t wish, such as own Google’s links (relative urls), cache pages, etc.

i’ve got this ‘open google links’ script working after a fashion; it seems kinda out of control tho. any comments would be helpful.

(*

this script is an altered version of apple's open linked images script

it is meant to open the pages from a google search in tabs

*)

tell application "Safari"
	activate
	
	try
		
		set the display_method to "Tabs"
		
		-- the number of links in the front document
		set the link_count to (do JavaScript "document.links.length" in document 1)
		(*	if the link_count is "0" then error "No links in this document."*)
		
		-- get the url of the directory containing the current page
		set the document_URL to (do JavaScript "document.URL" in document 1)
		set AppleScript's text item delimiters to "/"
		set the parent_URL to ((text items 1 thru -2 of the document_URL) as string) & "/"
		set AppleScript's text item delimiters to ""
		
		-- I got this next part (tell application...) here. as the poster said: This should return a list of all links. However, you should filter the links you don't wish, such as own Google's links (relative urls), cache pages, etc.  it works tho it seems slow; of course that might be just the way it has to be.
		tell application "Safari" to set allLinks to ¬
			do JavaScript "for (i = 0; i < document.links.length; i++){ 
   l += document.links[i].href + '\r'; 
} 
l" in document 1
		paragraphs of result
		
		
		set the google_links to {}
		repeat with i from 0 to (the link_count - 1)
			-- get the text of the anchor link
			set this_text to (do JavaScript "document.links[" & (i as string) & "].text" in document 1)
			
			-- if the text of the anchor link is a 'major link,' add that link to the list. i am not sure this next part (if this_text...) is working as it should
			if this_text does not contain "Cached" or "Similar Pages" or "More results" or "View as" or "[PDF]" or "html" then
				set this_URL to (do JavaScript "document.links[" & (i as string) & "].href" in document 1)
				
				if the this_URL begins with "http" then
					set this_host to (do JavaScript "document.links[" & (i as string) & "].host" in document 1)
					if this_host is "" then
						set this_URL to the parent_URL & this_URL
					end if
					set the end of the google_links to this_URL
				end if
			end if
		end repeat
		
		if the google_links is {} then
			error "This page contains no applicable links."
		end if
		
		if the display_method is "Tabs" then
			-- size the document to full screen    
			do JavaScript "moveTo(0,0);self.resizeTo(screen.availWidth,screen.availHeight)" in document 1
		end if
		
		repeat with i from 1 to number of items in the google_links
			set the image_URL to item i of the google_links
			if the display_method is "Windows" then
				set this_doc to make new document at beginning of documents
			else
				my new_tab()
			end if
			set the URL of document 1 to image_URL
		end repeat
		
	on error the error_message number the error_number
		if the error_number is not -128 then
			display dialog the error_message buttons {"OK"} default button 1
		end if
	end try
end tell

on new_tab()
	tell application "Safari" to activate
	tell application "System Events"
		tell process "Safari"
			click menu item "New Tab" of menu "File" of menu bar 1
		end tell
	end tell
end new_tab