Saving as PDF in Firefox...problems with setting destination folder

I have this GUI script that works with FF 3.5.7, not sure of other versions but I would think it’s ok back to any 3 version. I can get it to save the pdf doc but I can’t figure out how to save it to a specific location…

the problem is when you gui script the print command (Command-P) a window opens with the usual variety of print choices and the menu button “PDF” where you can select what flavor of PDF print/save you want to do. Once you click that another window/sheet/panel (I’m not sure which) opens for the Save options. I can’t target this window – when working with a window you can tell it “every gui element” and it’ll return all the elements/objects in the window, which allows you to work your way through them if they are not named intelligently (like button 1, button 2, button 3 and so forth as opposed to menu button PDF - thank you Firefox team.

But I can’t target that second window, tell window “Save” doesn’t work, nor does tell front window or tell window 1. I’m not even sure if the second window is part of the first window or just part of FF…i.e.

tell process Firefox
tell window “Save”

or

tell process Firefox
tell window “Save” of front window -->print window

neither of those worked either.

I can get it to save by doing a Keystroke return but I would like to pick a location to save it to first? Can anyone help?

Thanks


set mydelay to 0.5
tell application "Firefox" to activate
tell application "System Events"
	tell process "Firefox"
		
		-->this gets us to the save as pdf window
		keystroke "p" using command down
		tell front window
			delay mydelay
			tell menu button "PDF"
				click
				delay mydelay
				tell menu 1
					click menu item 1
				end tell
			end tell
		end tell
		
		keystroke return
	end tell
end tell

You might be doing yourself a favor if you choose the Desktop or your Home folder as the save-to location. The Save window can be scripted to accept a keystroke “d” using command down command, which will bring focus to the desktop; or use keystroke “h” using {command down, shift down} to save to your Home folder.

I’ve had some success with the following scripts in Firefox 3.5.7:

tell application "Firefox" to activate
tell application "System Events"
	tell process "Firefox"
		tell window 1
			keystroke "p" using command down
			tell window "Print"
				repeat 5 times
					keystroke tab
				end repeat
				keystroke space
			end tell
			tell menu 1
				key code 125
				keystroke return
			end tell
			delay 2 --> minimum
		end tell
		tell window "Save"
			keystroke "d" using command down
			delay 0.5 --> increase if necessary
			keystroke return
		end tell
	end tell
end tell

Incidentally, I found it wasn’t necessary to tell any of Firefox’s windows or menus to do anything – I let System Events do the work. The script below has been stripped down:

tell application "Firefox" to activate
tell application "System Events"
	tell process "Firefox"
		keystroke "p" using command down
		repeat 5 times
			keystroke tab
		end repeat
		keystroke space
		key code 125
		keystroke return
		delay 2 --> minimum
		keystroke "d" using command down
		delay 0.5 --> increase if necessary
		keystroke return
	end tell
end tell

Also, the Save window’s Search field can be scripted to accept a predefined folder name, although doing this would be slow, and might prove unreliable. Note the keystroke “Folder Name” command in the script:

tell application "Firefox" to activate
tell application "System Events"
	tell process "Firefox"
		tell window 1
			keystroke "p" using command down
			tell window "Print"
				repeat 5 times
					keystroke tab
				end repeat
				keystroke space
			end tell
			tell menu 1
				key code 125
				keystroke return
			end tell
			delay 2 --> minimum
		end tell
		tell window "Save"
			repeat 4 times
				keystroke tab
			end repeat
			keystroke "Folder Name" --> replace with actual folder name
			delay 15 --> decrease if possible; increase if necessary
			keystroke tab
			keystroke tab
			key code 125
			keystroke return
		end tell
	end tell
end tell

And the condensed version here:

tell application "Firefox" to activate
tell application "System Events"
	tell process "Firefox"
		keystroke "p" using command down
		repeat 5 times
			keystroke tab
		end repeat
		keystroke space
		key code 125
		keystroke return
		delay 2 --> minimum
		repeat 4 times
			keystroke tab
		end repeat
		keystroke "Folder Name" --> replace with actual folder name
		delay 15 --> decrease if possible; increase if necessary
		keystroke tab
		keystroke tab
		key code 125
		keystroke return
	end tell
end tell

Just a couple ideas… hope this helps.



set destinationFolder to choose folder with prompt "Select a folder to save the PDF files in."
set startTime to current date

-->determine how to get the urls
set urllist to {" ", "http://www.wired.com/", "http://www.esquire.com/", "http://www.cbssports.com/collegebasketball/polls/"}
tell application "Firefox"
	close front window
end tell

repeat with thisurl in urllist
	set mydelay to 0.5
	
	tell application "Firefox"
		activate
	end tell
	
	tell application "System Events"
		tell process "Firefox"
			delay 5
			keystroke "l" using {command down} & (keystroke thisurl) & (keystroke return)
			
			delay 10
			
			-->this gets us to the save as pdf window
			keystroke "p" using command down
			tell front window
				delay mydelay
				tell menu button "PDF"
					click
					delay mydelay
					tell menu 1
						click menu item 1
					end tell
				end tell
			end tell
			
			-->save
			keystroke return
			
			delay 10
		end tell
	end tell
end repeat

set desktopitems to list folder (path to desktop) without invisibles

repeat with thisitem in desktopitems
	set itempath to (path to desktop) & thisitem as string
	set iteminfo to info for alias itempath
	set itemkind to kind of iteminfo
	
	if itemkind contains "PDF" then
		set createdate to creation date of iteminfo
		if createdate > startTime then
			if name of iteminfo = "about_blank.pdf" then
				delete file alias itempath
			else
				do shell script "mv " & quoted form of POSIX path of itempath & " " & quoted form of POSIX path of destinationFolder
			end if
		end if
	end if
end repeat