Is there a script that can print all opened tabs in safari

Can anyone help me figure out a way to print all opened tabs in safari

Many thanks in advance
ffierrojr@aol.com

Model: G5
Browser: Safari 312.6
Operating System: Mac OS X (10.4)

Borrowing from http://bbs.applescript.net/viewtopic.php?id=16155 and http://bbs.applescript.net/viewtopic.php?id=16334 I came up with a script.

This is ugly because it is cobbled together, and it might need some tweaking - it wasn’t heavily tested - but try this (the pages need to be loaded before running the script)


to countSafariTabs()
	tell application "System Events" to tell (group 1 of window 1 of process "Safari" whose class of UI elements ¬
		is class of buttons and "" is not in title of buttons) to if exists then return count buttons
end countSafariTabs

set tabCount to countSafariTabs() --get tab count

repeat tabCount times
	
	tell application "System Events" to tell process "Safari"
		set frontmost to true
		
		keystroke "]" using {command down, shift down} --open next tab, will end at first tab
		
		keystroke "p" using command down --start print dialog
		tell sheet 1 of window 1
			repeat until exists --wait for print dialog
				delay 0.5
			end repeat
			keystroke return --print
		end tell
		delay 3 --leave time for pages to be processed before continuing
		
	end tell
	
end repeat



Hi, CapJ;

It’s simpler than that (presuming you want the titles of each document):

set N to {}
tell application "Safari"
	repeat while title of window 1 is not in N
		set end of N to title of window 1
		activate application "Safari"
		tell application "System Events" to tell process "Safari" to keystroke "}" using {command down}
	end repeat
end tell
N

Hi Adam.

Safari got an error: Can’t make title of window 1 into type reference.

So I changed “title” (which was green - a variable) to “name” and it worked

Thanks for the help - I was pretty sure I wasn’t doing it the easy way.

j

Good catch. I typed it in the reply, ran it and fixed it from a forum preview and forgot to update it in the window itself :confused:

I’ve found another bit - sometimes Safari is a bit slow on the uptake and you’ll miss a tab if many are open so a small delay is added:

set N to {}
tell application "Safari"
	repeat while name of window 1 is not in N
		set end of N to name of window 1
		activate application "Safari"
		tell application "System Events" to tell process "Safari" to keystroke "}" using {command down}
		delay 0.2
	end repeat
end tell
N

The lesson for both of us, BTW, is to read TFD - last word is Dictionary.

Thanks again - I was preoccupied by the problem of the time needed for pages to be processed for printing and forgot to go back - I was thinking of a test for page load, which I saw someplace, but that might be overkill.

j

Hi again, CapJ;

This script will get a list of names of the documents being printed. If the name of the last document you sent to print is there, then you can tell Safari to print the next one in the list.

tell application "Printer Setup Utility"
	set P to current printer
	set j to name of jobs of P
end tell

Thanks again - that’s much simpler than what I was going to try.

j

The printer bit kept returning an empty list (haven’t figured that out), so for now this is what I have put together:

set N to {}
tell application "Safari"
	repeat while name of window 1 is not in N
		set end of N to name of window 1
		activate application "Safari"
		
		tell application "System Events" to tell process "Safari"
			
			keystroke "}" using {command down}
			delay 0.2
			
			keystroke "p" using command down --start print dialog
			tell sheet 1 of window 1
				repeat until exists --wait for print dialog
					delay 0.5
				end repeat
				keystroke return --print
			end tell
			delay 3 --leave time for pages to be processed before continuing
			
		end tell
	end repeat
end tell


I noticed a potential problem even if I sort out the empty job list. In my printer jobs list, both

“MacScripter BBS | AppleScript Forums / AppleScript / OSX”
and
“MacScripter BBS | AppleScript Forums / AppleScript / Is there a script that can print all opened tabs in safari”

get shortened to "“MacScripter BBS | AppleScript Forums” so the script could stop prematurely

j