AppleScript: Safari - Save As Web Archive
The title says it all: Can someone tell me please how - most likely via UI scripting, since I can't see anything in the dictionary - I can get Safari to save the front most document as a Web Archive using AppleScript?
This is one I have recently written that saves all tabs in a window, it remembers the parent folder until next time, and also uses the webarchive ending when Safari is in doubt. You can set properties for whether you like it to overwrite or not. Please look at Macscripter for any updates.
property tlvl : me
# Release 1.0.1
# © 2012 McUsr and put in Public Domain under GPL 1.0
# Please refer to this post: http://macscripter.net/post.php?tid=30892
property shallClose : false # set this to false if you don't want to close the windows, just saving them
property dontOverWriteSavedTabs : false # set this to true if you don't want to overwrite already saved tabs in the folder
script saveTabsInSafariWindowsToFolder
property parent : AppleScript
property scripttitle : "SafariSaveTabs"
on run
if downloadWindowInFront() then return 0 # activates Safari
local script_cache
set script_cache to my storage's scriptCache()
set saveFolder to POSIX path of (getHFSFolder({theMessage:"Choose or create folder to save Safari-tabs in.", hfsPath:DefaultLocation of script_cache as alias}))
if saveFolder = false then return 0 -- we were obviously mistaken, about what we wanted to do.
my storage's saveParenFolderInScriptCache(saveFolder, script_cache)
tell application "Safari"
tell its window 1
local tabc, oldidx
set tabc to count tabs of it
if not tlvl's shallClose then
set oldidx to index of current tab
tell tab tabc to do JavaScript "self.focus()"
end if
local saveCounter
set saveCounter to 1 -- regulates setting of save folder to only first time in Safari.
repeat while tabc > 0
local theUrl, theIdx, theProtocol, alreadyClosed
set {theUrl, theIdx, alreadyClosed} to {URL of its current tab, index of its current tab, false}
if my isntAduplicateTab(theIdx, it) then
set theProtocol to my urlprotocol(theUrl)
if theProtocol is in {"http", "https"} then
# save it
set saveCounter to my saveCurrentTab(saveFolder, saveCounter)
else if theProtocol is "file" then
# make an alias of it
my makeAliasForAFurl(saveFolder, theUrl)
end if
else
if tlvl's shallClose then
close current tab
set alreadyClosed to true
end if
end if
if not alreadyClosed and tlvl's shallClose then
close current tab of it
set tabc to tabc - 1
else if not tlvl's shallClose then
set tabc to tabc - 1
if tabc > 0 then tell tab tabc to do JavaScript "self.focus()"
end if
end repeat
# move forwards
if not tlvl's shallClose then
tell tab oldidx to do JavaScript "self.focus()"
end if
end tell
end tell
end run
to makeAliasForAFurl(destinationFolder, furl)
local ti, tids, thefilePath
set ti to "file://"
set {tids, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ti}
set thefilePath to text item 2 of furl
set AppleScript's text item delimiters to tids
set theFile to POSIX file thefilePath as alias
set theFolder to POSIX file destinationFolder
tell application "Finder"
make alias at theFolder to theFile
# I don't care if there was one there from before, as it could equally
# be a file with the same name.
end tell
end makeAliasForAFurl
to saveCurrentTab(destinationFolder, timeNumber)
tell application id "sfri" to activate
tell application "System Events"
set UI elements enabled to true
tell process "Safari"
keystroke "s" using {command down}
tell window 1
repeat until exists sheet 1
delay 0.2
end repeat
tell sheet 1
if timeNumber = 1 then -- We'll set the savepath upon first call
keystroke "g" using {command down, shift down}
repeat until exists sheet 1
delay 0.2
end repeat
tell sheet 1
set value of text field 1 to destinationFolder
click button 1
delay 0.1
end tell
end if
keystroke return
delay 0.2
if exists sheet 1 then -- We are being asked if we want to overwrite already saved tab
if dontOverWriteSavedTabs then
keystroke return # if it was already saved. We don't overwrite it
click button 3
else
keystroke tab
keystroke space # we are to overwrite
end if
else
try
set dummy to focused of sheet 1
on error
# click button 1 of panel of application "Safari"
keystroke return
delay 0.2
if exists sheet 1 then -- We are being asked if we want to overwrite already saved tab
if dontOverWriteSavedTabs then
keystroke return # if it was already saved. We don't overwrite it
click button 3
else
keystroke tab
keystroke space # we are to overwrite
end if
end if
end try
end if
end tell
end tell
end tell
end tell
set timeNumber to timeNumber + 1
return timeNumber
end saveCurrentTab
on downloadWindowInFront()
tell application "Safari"
activate
set tabCount to count tabs of its window 1
if tabCount < 1 then
tell application "SystemUIServer" to activate
activate
return true # Downloads window or somethingelse
end if
end tell
return false
end downloadWindowInFront
on isntAduplicateTab(idxOfCurrentTab, theWin)
using terms from application "Safari"
tell theWin
set curTabname to name of tab idxOfCurrentTab
set curTabUrl to URL of tab idxOfCurrentTab
repeat with i from (idxOfCurrentTab - 1) to 1 by -1
if name of tab i = curTabname and URL of tab i = curTabUrl then return false
end repeat
return true
end tell
end using terms from
end isntAduplicateTab
on getHFSFolder(R) -- Returns hfsPathAsText
-- R : {Amessage:theMessage,hfsPath:aStartPath}
local new_path, failed
set failed to false
tell application "SystemUIServer"
activate
repeat while true
try
set new_path to (choose folder with prompt (theMessage of R) default location (hfsPath of R) without invisibles) as text
on error e number n
if n is -128 then
set failed to true
exit repeat
end if
end try
exit repeat
end repeat
end tell
if failed is true then
return false
else
return new_path
end if
end getHFSFolder
on urlprotocol(anUrl)
# returns the protocol of an Url, i.e. http, https, file, localhost etc.
local tids, theProtocol
set {tids, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "://"}
set theProtocol to text item 1 of anUrl
set AppleScript's text item delimiters to tids
return theProtocol
end urlprotocol
to parentfolder for aPath
local colons, slashes, origDelims
set {colons, slashes} to {false, false}
if (offset of ":" in aPath) > 0 then set colons to true
if (offset of "/" in aPath) > 0 then set slashes to true
if colons and slashes then
return null
else if colons then
set origDelims to ":"
else if slashes then
set origDelims to "/"
else
return null
end if
local tids
set {tids, AppleScript's text item delimiters} to {AppleScript's text item delimiters, origDelims}
if aPath = "/" then
-- we return root when we get root
set AppleScript's text item delimiters to tids
return "/"
end if
local theParentFolder
if text -1 of aPath is in {":", "/"} then
set theParentFolder to text items 1 thru -2 of text 1 thru -2 of aPath
else
set theParentFolder to text items 1 thru -2 of aPath
end if
set theParentFolder to theParentFolder as text
if slashes and theParentFolder = "" then set theParentFolder to "/"
-- sets the root path if we got a folder one level below it
if colons and (":" is not in theParentFolder) then set theParentFolder to theParentFolder & ":"
-- we return volumename, if we are given volumename
set AppleScript's text item delimiters to tids
return theParentFolder
end parentfolder
script storage
property cachespath : ((path to library folder from user domain as text) & "caches:" & "net.mcusr." & scripttitle)
on scriptCache()
local script_cache
try
set script_cache to load script alias (my cachespath)
on error
script newScriptCache
property DefaultLocation : (path to desktop folder as text)
# edit any of those with default values
end script
set script_cache to newScriptCache
end try
return script_cache
end scriptCache
to saveScriptCache(theCache)
store script theCache in my cachespath replacing yes
end saveScriptCache
to saveParenFolderInScriptCache(theFolderToSaveIn, script_cache)
local containingFolder
set containingFolder to (parentfolder of saveTabsInSafariWindowsToFolder for theFolderToSaveIn) & "/"
local theLoc
set theLoc to POSIX file containingFolder as alias
set DefaultLocation of script_cache to theLoc
my saveScriptCache(script_cache)
end saveParenFolderInScriptCache
end script
end script
tell saveTabsInSafariWindowsToFolder to run
Similar Messages
-
Opening Safari for Mac Web Archives in Safari for Windows???
I need to open Safari for Mac Web Archives (saved html web pages on a Mac) on my Windows PC at work. Does anyone know how I can accomplish this? The original Web Pages are no longer available and the data in the Archives is the only set left.
I've tried opening the files with Safari for Windows but that doesn't work. I've also tried renaming the web archives from file.webarchive to file.html and that doesn't work either.
Does anyone out there have any suggestions for accomplishing this task??? Thanks in advance for your help!
YVIf you can move your archives to a Mac I think you can convert them to folders with Webarchive Folderizer and then move back to your pc:
http://www.versiontracker.com/dyn/moreinfo/macosx/29095 -
Applescript Safari Save Document error
The following applescript command started giving me an error message 3 days ago. I suspect there was a bug in Apple's last OS software update.
tell application "Safari" to save document 1 in file ((path to desktop as string) & "test")
The error I get is "The document “Start New Discussion : Apple Support Communities” could not be saved as “test”. " It doesn't seem to matter what document is currently open in Safari nor the type of document. I first encountered it with a JPEG image. At that time the code was part of a larger script and the file name included ".jpg" but the error occurs with or without an extension.
I get the same error on my Mac Pro, my MacBook Pro and my wife's Mac Mini. I believe all 3 Mac's have the must current OS 10.6 updates.
The larger script in question functioned correctly on July 25, 2011, so this must be recent.
Any ideas?
TIA
Rich CohenI suspect there was a bug in Apple's last OS software update.
I think you're right.
Any ideas?
Maybe you might want to try the following workaround (which doesn't require that you enable access for assistive devices in the Universal Access System Preference pane):
tell application "Finder"
set fileExists to file ((path to desktop as text) & "test") exists
end tell
tell application "Safari"
if not (document 1 exists) then return
activate
end tell
tell application "System Events"
keystroke "s" using {command down}
delay 1 -- might be less
keystroke "test"
keystroke "d" using {command down} -- desktop
keystroke return -- save
if fileExists then
delay 1 -- might be less
keystroke "r" using {command down} -- replace
end if
end tell -
How to tell Safari to save as a Web Archive
The default "Save" when using a script is to save as "Page Source." How would one write a script to Save as "Web Archive."
01. In 'Safari' - manually, select the 'File, Save As...' menu item. A drop down sheet will appear.
02. Select the 'Format:' Web Archive' menu item.
03. Click on the 'Save' button.
04. Locate the just saved web page, trash it, and empty the trash.
You have now set 'Safari' to save all future 'Save As...'es in the 'Web Archive' format.
05. Use the code (which is supplied with no error trapping) below.
-- Code starts here --
property tURL : "http://www.badcopnews.com/"
tell application "Safari"
activate
make new document at end of documents
set URL of document 1 to tURL
get document 1
end tell
delay 3
tell application "System Events" to tell process "Safari"
keystroke "s" using command down -- 'Command S' - 'Save As...'
delay 1 -- Pause for 1 second.
keystroke "d" using command down -- 'Command D' - Go to 'Desktop'.
delay 1 -- Pause for 1 second.
keystroke "c" using command down -- 'Command C' - Go copy highlighted text.
set the clipboard to ((do shell script ("date +%Y%m%d%H%M%S")) & (the clipboard))
keystroke "v" using command down -- 'Command V' - 'Paste'.
delay 1 -- Pause for 1 second.
click button "Save" of sheet 1 of window 1 -- Click on 'Save' button.
end tell
-- Code ends here -- -
From time to time I save a web archive of a site for off-line browsing and for future reference in case the site vanishes.
This page - www.gabrielleray.150m.com - can produce a Webloc file, but the Save button is greyed out. It seems peculiar to Safari and this page as I had earlier saved a page from the same site without problem. I have also saved a Web archive from IE (ugh), but it will not open in safari.
Any ideas?
Cheers, Colinyou have longer filenames than this?? - that's what Safari tries to save as....
John Culme's Footlight Notes - Press Clippings of the Week - Opening of the Avenue Theatre, London, 1882 - Alabama<-I>, a play by Augustus Thomas, Garrick Theatre, London, 2 September 1895 - Héro, billed in London as a ‘Parisienne,’ circa 1910 - ‘Nudity’ on the London music hall stage, 1911 - Marie Dressler - ‘a bogus manager’ - in London, 1909 - Week ending 28 September 2002 -
What the blood clot is up wit Safari and SAVIN web pages..
i'm pretty fed up...it shouldn't be this difficult..
i don't have interent at home so its mad vital on this..
i'll go to my friends or family house surf the web...and whatnot...then i try to save for later viewing...
i'm going to a web page and hitting save as.. selecting save as web archive complete...
but when i get home... and with airport turned off.. i go reopen thsese saved archives and it doesn't save correctly...the page comes up but no images come up...
ex. i'm on a video game forum and i save as complete web archive
when i get home and try to show my wife my post with images.. ONLY the text comes up and little square boxes wit a ? on it instead of images..
waht am i doing wrong?
a year ago when i got the mac it worked fine.. but now..
is there a program or differnt web browser i should use to avoid these problems? ( i wnat to stick wit apple tho)...
and another thing my time clock is gone as well how i get that back..
thank you all and i hope you can help me!!
good day!
opNecan no one help?
oNe -
How to tell Safari to open and save a web page
While on vacation, I would like to save a daily puzzle that occurs on a web site that uses the same URL for each new puzzle. Using the Energy Saver Preference panel, I can schedule a daily start up and shut down and add Safari to the Login Items. How would I tell Safari to open that URL and then save the web page to the Desktop?
Ralphjh,
An Automator workflow can save the page in a web archive format:
First download the Save Safari Web Archive workflow.
I would add two additional actions at the beginning of the workflow, each from Automator's Safari library, so that your final workflow would look like this:
1) *Get Specified URLs* -- Deselect or use the minus sign to remove the default www.apple.com address. Open your puzzle page in Safari, make it frontmost, and click on the Current Safari Page button.
2) *Display Webpages*
3) *Run AppleScript* -- No need to modify the existing script.
4) *Get Selected Finder Items*
5) *Move Finder Items* -- The desktop is default.
Save the Workflow as File Format: application, and add it to your Login Items.
It all may be for naught however. I suspect that upon your return from vacation, you'll be disappointed to find that opening any one of the saved files will only result in opening the current day's puzzle. For example, on Tuesday April 29th, the puzzle linked from the URL you posted opened the puzzle for +that day,+ Puzzle Number 760. A quick look at the 30 Day Killer archive (scroll down the page) reveals that each day's puzzle has its own URL. The actual URL for Puzzle Number 760 is:
http://www.sudoku.org.uk/DailyKiller.asp?day=29/04/2008
You'll notice that the URLs for the individual days' puzzles in the Killer Archive differ only in the dates appended to them. You would likely need to find a way to incorporate that pattern into an AppleScript -- correlating the current date to the updated URL.
Regards, Andrew99 -
How to save a web page on a safari?
How do I save a web page on a safari?
didn't notice any tabs that says file > save asride1992 wrote:
You might try reading the user guide. You would be surprised at the information it offers.
http://manuals.info.apple.com/enUS/iPad_UserGuide.pdf
I don't believe that there is anything in the iPad User Guide about saving Web pages as Web archives which is what the original poster asked.
With the current iPad Safari there is no way of saving web pages. Images on web pages can be saved and, of course, web pages can be bookmarked.
To save web pages you can use another app like Goodreader. A URL in Safari can be transferred to Goodreader for downloading (add "g" before http). -
Safari 5 not showing Web Archive pages
Hello, I have Safari 5, and I can't see some of the pages that I need to see. This has been a problem for me for a while, not matter what version of Safari I use. I click on the link from a website, and when the page loads, instead of getting the page that I should be getting, I get a very long page, covered in system codes, or something of the such, and at the top of the page, it says, in typewriter-like font,
"This document is a Single File Web Page, also known as a Web Archive file. If you are seeing this message, your browser or editor doesn't support Web Archive files. Please download a browser that supports Web Archive, such as Microsoft Internet Explorer."
After that, it goes into the code. I have looked other places on the internet, but never found anything like what I am experiencing. Any help from anyone? Thanks.HI,
.mht
Definitely Microsoft "The MHT file type is primarily associated with 'MHTML Document' by Microsoft Corporation. Archived Web Page. When you save a Web page as a Web archive in Internet Explorer, the Web page saves this information in Multipurpose Internet Mail Extension HTML (MHTML) format with a .MHT file extension. All relative links in the Web page are remapped and the embedded content is included in the .MHT file. The absolute references or hyperlinks on the Web page remain unchanged and the .MHT file is viewed using Internet Explorer. Note: This file type can become infected and should be carefully scanned if someone sends you a file with this extension."
http://filext.com/file-extension/MHT
with a program that will not be supported by Safari/Mac?
That's it. -
Hi. Not familiar with forums but hope to get some help.
Safari (7.0.6) on my iMac running Mavericks (10.9.4) is not saving web pages (web archives).
I've used the following path:
File > Save As >then Format > Web Archive
It saves a file but when I click on its icon it opens up to the 'Top Sites' page (as when you click on the + for a new tab) not a saved web archive of the web page I was on.
I have no such problem with Safari on my MacBook Pro running Lion.
The iMac hardware details are: 2.7 Gz Intel Core i5 with 8 gb of RAM . The software update tells me I'm up to date.
Any help would be much appreciated. Thanks in advance.
Errol.It could be malware that's causing the archive / Top Sites problem.
Download and run the adware removal tool here > The Safe Mac » Adware Removal Guide
Easy, safe, and only takes a minute or two.
If no malware is found ...
From your Safari menu bar click Safari > Preferences then select the Privacy tab.
Click: Remove All Website Data
Then delete the cache.
Open a Finder window. From the Finder menu bar click Go > Go to Folder
Type or copy paste the following
~/Library/Caches/com.apple.Safari/Cache.db
Click Go then move the Cache.db file to the Trash.
Quit and relaunch Safari to test.
If that didn't help, troubleshoot Safari extensions.
From the Safari menu bar click Safari > Preferences then select the Extensions tab. Turn that OFF, quit and relaunch Safari to test.
If that helped, turn one extension on then quit and relaunch Safari to test until you find the incompatible extension then click uninstall. -
Creating Web Archive in Safari ver. 5.1.5 (66534.55.3)
Does anyone know if safari change Version 5.1.5 (6534.55.3)?
I used to be able to right click on my macbook's trackpad and easily create a web archive file while viewing Internet pages.
thxWelcome to Apple Support Communities.
I'm on OS X 10.6.8 and Safari 5.1.5 (6534.55.3)
When you right-click, do you see this screen?
Choose Save Page As, and then you have the opportunity to (re)name the file and select the Web Archive format as shown below. -
Filenames getting truncated in Save for Web AppleScript
Hey all, running CS4 on an Apple Macbook Pro (using OS X 10.5.6). I am creating an AppleScript that saves out a number of files with different filenames using save for web. The problem is that Photoshop CS4 is truncating the filename, and using a shorter filename is not an option (there is identifying info in the filename that I need to retain). Is there a way within the script I can set it to allow longer filenames? My current save for the web script line looks as follows:
export current document in file filename as save for web with options {quality:60, web format:JPEG}
What happens is that a filename like this:
CANON-PRO8000MKII-ideal-ideal-24.jpg
gets cut down to this:
CANON-PRO8000MKII-ideal-ideal#0
I believe that there there is a PShop bug here that has a hard-coded limit on the filename, but which does not happen if you enter the file name manually: if I go into the save for web dialog and enter the full filename, it works fine. Any ideas?I recall having this problem too, my solution was to truncate the file name in the script prior to the save, then rename the saved file.
e.g.
if length of theNewFileName > 31 then
set theTempFileName to theNewFileName
set theNewFileName to (text 1 through (31 - (length of theFileExtension) - 1) of theNewFileName & "." & theFileExtension) as string
end if
set theNewFile to (saveToFolder & theNewFileName) as string
-- Save the file here
if theTempFileName = "" then
-- File name wasn't truncated
else
-- Rename truncated file
tell application "Finder"
try
set name of (theNewFile as alias) to theTempFileName
on error errMsg number ERRNUM
display dialog "Error renaming file - " & errMsg
end try
end tell
end if -
How do I save a document that says it is a web archive
I signed up to convert pdfs into word format but now I don't know how to save the converted document into my files. It says it is a web archive and I cannot work with the document. I can't cut and paste. What do I need to do?
When you say you want to convert PDFs to Word, there is no obvious link with Adobe's RoboHelp product. Are you in fact working with RoboHelp or have you perhaps asked in the wrong forum?
See www.grainge.org for RoboHelp and Authoring tips
@petergrainge -
Cant I save a web page with its pictures in safari?
hi there,
when i used save as... to save a web page, it can only save the page as text only, is there any way to save text and the pictures as well.
im using safari 1.3.1 in os 10.3.9You can only save a page with images in Safari 2.x which comes with OS X 10.3.x. For earlier versions you can go to Print > Save as PDF to capture and image of the page. You can use Firefox or Camino to save a page in standard html format along with a folder of images. This method allows you to view the page with any browser whereas Safari's webarchives can only be viewed in Safari.
-
Safari errors printing to PDF save / saving web file
Suddenly starting getting errors today when trying to save a web page by either method. Printing-> save as PDF gives "Print" "Error while printing" after selecting a destination folder. Trying to save it was web file gives "The document “Match.com - Find Singles with Match.com's Online Dating Personals Service” could not be exported as “JL”. You don’t have permission." The only workaround seems to be to save to desktop without giving a title, change title, then move to folder and authenticate the move after being told I don't have permission.
Any ideas? This has never happened before and I haven't changed any significant setting (that I know of!).HI and Welcome to Apple Discussions...
Try maintenance...
From the Safari Menu Bar, click Safari / Empty Cache. When you are done with that...
From the Safari Menu Bar, click Safari / Reset Safari. Select the top 5 buttons and click Reset.
Since you are running Snow Leopard, *make sure Safari is opening in 32-bit mode, not 64.* Right or control click the Safari icon in the Applications folder, then click: Get Info In the Get Info window click the black disclosure triangle next to General so it faces down. Select 32 bit mode. Also, (in that same window) *make sure Safari is NOT running in Rosetta*.
Web pages now include a small icon or 'favicon' which is visible in the address bar and next to bookmarks. These icons take up disk space and slow Safari down. It is possible to erase the icons from your computer and start fresh. *To delete Safari's icon cache using the Finder, open your user folder, navigate to ~/Library/Safari/ and move this file "webpageIcons.db to the Trash.*
Relaunch Safari.
Carolyn
Maybe you are looking for
-
Xorg 1.8 nvidia GeForce4 MX 4000 problem.
Hello! because the nvidia-96xx proprietary drivers are not compatible with the new version of xorg, I decided to use the free drivers nouveau. # pacman -S nouveau-dri xf86-video-nouveau But I can not get a hardware 3D rendering. #glxinfo | grep -i op
-
Dual boot with Ubuntu 13.0 using bootable USB problem (Y410P)
Hi I recently bought Lenovo Y410P and have been since trying for hours to dual boot with Ubuntu but I was unsuccessful. I formatted by USB drive in FAT format, and created a bootable usb using Pendrive. Then I changed the Boot Mode to Legacy Support
-
My Ipod crashed and I can't restore it.
When I try and restore it, it says I need to update my firmware but when I go to install the firmware it extracts but then I get an error message. It won't let me update to 5.0 or any earlier firmware. Help!!
-
Nikon d300s .avi files workflow
I was wondering if anyone else on the forum has ever shot video with a Nikon DSLR and then edited with FCP? If you have, what is your workflow? What I ended up doing is importing my .avi files from my Nikon CF Card onto my external hard drive. I put
-
Burn folder - dmg or zip?
Is there an archiving program that can take the aliases in a burn folder, resolve them, and create a dmg or zip archive?