Friday, October 22, 2021

#1 2021-10-04 11:42:21 am

Niscors
Member
Registered: 2020-04-11
Posts: 9

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

Applescript:


-- 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


Filed under: applescript, Tab, save as

Offline

 

#2 2021-10-05 06:33:40 pm

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 1089

Re: Save as webarchive troubles - tab name

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.

Applescript:

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


2018 Mac mini - macOS Catalina - Script Debugger 8

Offline

 

#3 2021-10-06 06:33:41 am

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 2091

Re: Save as webarchive troubles - tab name

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.

Last edited by KniazidisR (2021-10-06 06:35:14 am)


Model: MacBook Pro
OS X: Catalina 10.15.7
Web Browser: Safari 14.1
Ram: 4 GB

Offline

 

#4 2021-10-06 09:59:14 am

Fredrik71
Member
Registered: 2019-10-23
Posts: 901

Re: Save as webarchive troubles - tab name

Only like to remind that...

Shane webarchive code used to work when Apple used WebView and older Safari.
Apple have recommend to use WKWebView from Mojave to current OS.
Apple still do security update of Safari for Mojave that also update the framework.

https://developer.apple.com/documentati … guage=objc

It means that Shane code only work on website that do not use JavaScript to export webarchive.
In other words the code ASObjC that Shane did will not work on all websites.

Regards.


if you are the expert, who will you call if its not your imagination.

Offline

 

#5 2021-10-06 01:30:30 pm

Niscors
Member
Registered: 2020-04-11
Posts: 9

Re: Save as webarchive troubles - tab name

peavine wrote:

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.



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). roll

Applescript:

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

Offline

 

#6 2021-10-07 01:39:18 pm

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 2091

Re: Save as webarchive troubles - tab name

Fredrik71 wrote:

Only like to remind that...

Shane webarchive code used to work when Apple used WebView and older Safari.
Apple have recommend to use WKWebView from Mojave to current OS.
Apple still do security update of Safari for Mojave that also update the framework.

https://developer.apple.com/documentati … guage=objc

It means that Shane code only work on website that do not use JavaScript to export webarchive.
In other words the code ASObjC that Shane did will not work on all websites.

Regards.



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.

Last edited by KniazidisR (2021-10-07 01:47:44 pm)


Model: MacBook Pro
OS X: Catalina 10.15.7
Web Browser: Safari 14.1
Ram: 4 GB

Offline

 

#7 2021-10-07 03:04:13 pm

Fredrik71
Member
Registered: 2019-10-23
Posts: 901

Re: Save as webarchive troubles - tab name

@KniazidisR
The ASObjC code from Shane that you speak about was something I used alot before.
The OS I used the code was Mojave. The issue I had later was when I update Safari that also
update the framework WebKit. In return I couldn't make a webarchive on sites that used JavaScript.
The reason is Shanes code use WebView and not WKWebView class.

You could test on: Apple's developers website.

I also had a old code that use NSWindow and WebView that didn't longer could read a webarchive
file stored on local HD. This issue I have already solved by update to WKWebView. When methods become deprecated it could mean it no longer work as before or old script breaks.


if you are the expert, who will you call if its not your imagination.

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)