Save as webarchive troubles - tab name

Hi, I have a useful little script which changes the tab name of an open safari webpage to a specific item from the webpage. It then saves that tab as a webarchive using the tab name as the filename.

Since updating to Big Sur 11.6 and Safari 15.0 this morning, the second part of the script has stopped working. Specifically, the filename in the ‘Save As’ pop up window is now populated with the last bit of the web address (‘News’, for example from the BBC Website), where it used to be populated with my newly changed tab name.

Something has obviously changed! Can anyone please help to get this working again please?

Thanks,
Niscors


-- For testing only (this is normally obtained from the webpage)
set myShortAddress to "New Tab Name"


-- Change the tab name (Current Tab). 
tell application "Safari"
	activate
	do JavaScript "document.title ='" & myShortAddress & " - FRA" & "';" in current tab of window 1
end tell


tell application "Safari"
	activate
	tell application "System Events"
		tell application process "Safari"
			set frontmost to true
			keystroke "S" using {command down} -- Save As…
			tell last pop up button of its sheet
				select menu item "Web Archive"
			end tell
			key code 36 -- to accept Save button
		end tell
	end tell
	
	display dialog "Report saved as " & myShortAddress & " - FRA.Webarchive"
end tell
return


Niscors. I tested your script on Catalina and encountered the issue you note. I don’t know what has changed–my guess is that the default file name was the tab name on older versions of macOS but is now a portion of the URL.

In my testing, the following script works as you want, with one exception. I could not get the script to set Web Archive, although this can be set manually and appears to stick from that point on.

set myShortAddress to "New Tab Name"

tell application "Safari"
	activate
	do JavaScript "document.title ='" & myShortAddress & " - FRA" & "';" in current tab of window 1
end tell

tell application "System Events" to tell process "Safari"
	keystroke "S" using {command down} -- Save As…
	delay 0.5 -- test different values
	keystroke myShortAddress & " - FRA"
	delay 0.5 -- test different values
	key code 36 -- to accept Save button
end tell

display dialog "Report saved as " & myShortAddress & " - FRA.Webarchive" buttons {"OK"} cancel button 1 default button 1

Hello, @Niscors,

It’s time to abandon GUI scripting and move on to normal saving of the web page as a web archive. To make your script work in future OS and Safari updates.

This site has examples of how to do this using AsObjC. Look up posts from @Shane Stanley, who posted an example like this for the first time.

Thanks Peavine, that has got it back up and running again. I use the code below to automatically select the Webarchive format (I don’t know why I left some of it out in my first post). :rolleyes:

tell application "Safari"
	activate
	tell application "System Events"
		tell application process "Safari"
			set frontmost to true
			keystroke "S" using {command down} -- Save As…
			tell last pop up button of its sheet
				select menu item "Web Archive"
			end tell
			delay 0.5 -- test different values
			keystroke myShortAddress & " - FRA"
			delay 0.5 -- test different values
			key code 36 -- to accept Save button
			
		end tell
	end tell
	
	--display dialog "Report saved as " & myShortAddress & " - FRA.Webarchive"
end tell
return

You intimidated the OP so much that he immediately rushed back to his GUI scripting. Without even checking what he was offered in return. As I understand, what you said is: “If the computer is old, the web page is strange, etc. etc … then maybe AsObjC won’t work … …” I admit it’s true, but I would like to have a concrete example of such a web page. Because I don’t think the OP uses some webpage which saves itself some content as webarchive using Java. He uses usual “Save as…” of Safari.app.