Save as PDF 10 new'st MacScripter/AppleScript_Mac_OSX topics to disk

Hi everyone. I like to work with the topics of our site offline, save and leave the most interesting articles in the documents of my computer, as PDF.

Therefore, I decided to automate the process for myself and for other MacScripter users.


-- Saves 10 new'st MacScripter/AppleScript_Mac_OSX topics on local disk
-- Is developed and tested on macOS Catalina Vesion 10.15.6

property indexOfNewestTopic : 47979
property topicsToExport : 10 -- you can set more topics to export

set indexofLastTopics to indexOfNewestTopic - (topicsToExport - 1)

set documentsFolderHFS to (path to documents folder) as text
tell application "Finder" to if not (exists (documentsFolderHFS & "MacScripter Topics:")) then make new folder at documentsFolderHFS with properties {name:"MacScripter Topics"}
set macscripterFolder to alias (documentsFolderHFS & "MacScripter Topics:")

set destinationFolderHFS to (macscripterFolder as text) & indexofLastTopics & "-" & indexOfNewestTopic
tell application "Finder" to if not (exists destinationFolderHFS) then make new folder at (macscripterFolder as text) with properties {name:("" & indexofLastTopics & "-" & indexOfNewestTopic)}
set destinationFolderPath to POSIX path of (alias destinationFolderHFS)

tell application "Safari" to activate

repeat with i from indexOfNewestTopic to indexofLastTopics by -1
	
	tell application "Safari" to open location ("https://www.macscripter.net/viewtopic.php?id=" & i)
	
	tell application "System Events" to tell application process "Safari"
		repeat until (UI element "Reload this page" of group 3 of toolbar 1 of window 1 exists)
			delay 0.1
		end repeat
	end tell
	
	if document "Info / MacScripter" of application "Safari" exists then
		-- badTopic URL : "The link you followed is incorrect or outdated."
		close document "Info / MacScripter" of application "Safari" saving no
	else
		tell application "System Events" to tell application process "Safari"
			click menu item "Print…" of menu 1 of menu bar item "File" of menu bar 1
			repeat until exists sheet 1 of window 1
				delay 0.02
			end repeat
			tell window 1 to tell sheet 1
				click menu button "PDF"
				repeat until exists menu 1 of menu button "PDF"
					delay 0.02
				end repeat
				click menu item "Save as PDF" of menu 1 of menu button "PDF"
				repeat until exists sheet 1
					delay 0.02
				end repeat
				tell sheet 1
					keystroke "g" using {shift down, command down}
					repeat until exists combo box 1 of sheet 1
						delay 0.02
					end repeat
					tell sheet 1
						set value of combo box 1 to destinationFolderPath
						-- Now click the Go button
						click button "Go"
						repeat while it exists
							delay 0.02
						end repeat
					end tell
					click button "Save"
					delay 1
					if sheet 1 exists then click button "Replace" of sheet 1
				end tell
			end tell
		end tell
		close front document of application "Safari" saving no
	end if
	
end repeat

The script above is written for previous version of Safari. The following script I developed just now for new version of “Safari”. It has some differences from previous.

First, I have split the code into modules so that it can be easily modified in the future.

Secondly, I removed the replacement of the existing PDF (clicking the “Replace” button), as this makes the code unstable with the new version of “Safari”.

Third, of course, I made the changes due to the different object model of the new version of “Safari”.


-- Saves MacScripter topics on local disk
-- Is developed and tested for Safari Version 14.0 (15610.1.28.1.9, 15610) 

property indexofLastTopics : 1
property indexOfNewestTopic : 47983

set documentsFolderHFS to (path to documents folder) as text
tell application "Finder" to if not (exists (documentsFolderHFS & "MacScripter Topics:")) then make new folder at documentsFolderHFS with properties {name:"MacScripter Topics"}
set macscripterFolder to alias (documentsFolderHFS & "MacScripter Topics:")

set destinationFolderHFS to (macscripterFolder as text) & indexofLastTopics & "-" & indexOfNewestTopic
tell application "Finder" to if not (exists destinationFolderHFS) then make new folder at (macscripterFolder as text) with properties {name:("" & indexofLastTopics & "-" & indexOfNewestTopic)}
set destinationFolderPath to POSIX path of (alias destinationFolderHFS)

tell application "Safari" to activate

repeat with i from indexOfNewestTopic to indexofLastTopics by -1
	
	-- open location
	tell application "Safari" to open location ("https://www.macscripter.net/viewtopic.php?id=" & i)
	
	tell application "System Events" to tell application process "Safari"
		set frontmost to true
		repeat until (UI element "Reload this page" of group 3 of toolbar 1 of window 1 exists)
			delay 0.1
		end repeat
	end tell
	
	if document "Info / MacScripter" of application "Safari" exists then
		-- badTopic URL : "The link you followed is incorrect or outdated."
		close document "Info / MacScripter" of application "Safari" saving no
	else
		my clickMenuPrint()
		my clickMenuButtonPDF()
		my clickMenuItemSaveAsPDF()
		my setFolderPath()
		my goToFolder(destinationFolderPath)
		my clickButtonSave()
		my clickButtonReplace()
		close front document of application "Safari" saving no
	end if
	
end repeat

on clickMenuPrint()
	tell application "System Events" to tell application process "Safari"
		click menu item "Print…" of menu 1 of menu bar item "File" of menu bar 1
		repeat until exists sheet 1 of window 1
			delay 0.02
		end repeat
	end tell
end clickMenuPrint

on clickMenuButtonPDF()
	tell application "System Events" to tell application process "Safari"
		tell window 1 to tell sheet 1
			click menu button "PDF"
			repeat until exists menu 1 of menu button "PDF"
				delay 0.02
			end repeat
		end tell
	end tell
end clickMenuButtonPDF

on clickMenuItemSaveAsPDF()
	tell application "System Events" to tell application process "Safari"
		tell window 1 to tell sheet 1
			click menu item "Save as PDF" of menu 1 of menu button "PDF"
			repeat until exists sheet 1
				delay 0.02
			end repeat
		end tell
	end tell
end clickMenuItemSaveAsPDF

on setFolderPath()
	tell application "System Events"
		keystroke "g" using {shift down, command down}
		tell application process "Safari" to tell window 1
			repeat until exists combo box 1 of sheet 1 of sheet 1 of sheet 1
				delay 0.02
			end repeat
		end tell
	end tell
end setFolderPath

on goToFolder(destinationFolderPath)
	tell application "System Events" to tell application process "Safari"
		tell window 1 to tell sheet 1
			tell sheet 1 to tell sheet 1
				-- set folder Posix path
				set value of combo box 1 to destinationFolderPath
				-- Now click the Go button
				click button "Go"
			end tell
			repeat while sheet 1 of sheet 1 exists
				delay 0.02
			end repeat
		end tell
	end tell
end goToFolder

on clickButtonSave()
	tell application "System Events" to tell application process "Safari"
		tell window 1 to tell sheet 1 to tell sheet 1
			click button "Save"
		end tell
	end tell
	delay 2
end clickButtonSave

on clickButtonReplace()
	tell application "System Events" to tell application process "Safari"
		tell window 1 to tell sheet 1
			if sheet 1 of sheet 1 exists then click button "Cancel" of sheet 1 of sheet 1
		end tell
	end tell
end clickButtonReplace