Rectangle 27 2

Get Current Webpage from Safari
Download Urls

osx - Save the source of an open webpage from Safari with AppleScript ...

osx safari applescript
Rectangle 27 2

I have found what I believe to be a better / easier solution:

tell application "Safari"
activate
set URL of document 1 to "http://www.apple.com"
delay 5
set myString to source of document 1
end tell
set newFile to POSIX file "/Users/myUsername/test.html"
open for access newFile with write permission
write myString to newFile
close access newFile

osx - Save the source of an open webpage from Safari with AppleScript ...

osx safari applescript
Rectangle 27 2

This is a way to save a window full of tabs. The original UI handler was written by StefanK aka. Stefan Klieme of Macscripter fame. It considers webarchives file endings, when Safari is in doubt, you can adjust whether you want to overwrite or ignore already written files. It don't save duplicate tabs, and you may set a property to decide whether it shold close the tab when it is saved.

You can overcourse use wget, but I settled for UI Scripting, as wget has download stuff that is already in your browser, and is a kluge to program as well.

osx - Save the source of an open webpage from Safari with AppleScript ...

osx safari applescript
Rectangle 27 1

set hyperlink to "http://www.google.com/"

set sourceCode to (do shell script "curl " & hyperlink)

do shell script "echo " & quoted form of sourceCode & " >> /Users/name/Desktop/test.csv"

You can throw this in a repeat and it will append each source code from every listed stite to the end of your created document. i.e.

set hyperlink to "http://www.aRepetitivePageSite.com/2014?page="
set your_count to 1
repeat until your_count = 10
  set sourceCode to (do shell script "curl " & (hyperlink & your_count as string as text))
  do shell script "echo " & quoted form of sourceCode & " >> /Users/name/Desktop/test.csv"
  set your_count to your_count + 1
end repeat

osx - Save the source of an open webpage from Safari with AppleScript ...

osx safari applescript
Rectangle 27 0

I have found what I believe to be a better / easier solution:

tell application "Safari"
activate
set URL of document 1 to "http://www.apple.com"
delay 5
set myString to source of document 1
end tell
set newFile to POSIX file "/Users/myUsername/test.html"
open for access newFile with write permission
write myString to newFile
close access newFile

osx - Save the source of an open webpage from Safari with AppleScript ...

osx safari applescript
Rectangle 27 0

set hyperlink to "http://www.google.com/"

set sourceCode to (do shell script "curl " & hyperlink)

do shell script "echo " & quoted form of sourceCode & " >> /Users/name/Desktop/test.csv"

You can throw this in a repeat and it will append each source code from every listed stite to the end of your created document. i.e.

set hyperlink to "http://www.aRepetitivePageSite.com/2014?page="
set your_count to 1
repeat until your_count = 10
  set sourceCode to (do shell script "curl " & (hyperlink & your_count as string as text))
  do shell script "echo " & quoted form of sourceCode & " >> /Users/name/Desktop/test.csv"
  set your_count to your_count + 1
end repeat

osx - Save the source of an open webpage from Safari with AppleScript ...

osx safari applescript
Rectangle 27 0

This is a way to save a window full of tabs. The original UI handler was written by StefanK aka. Stefan Klieme of Macscripter fame. It considers webarchives file endings, when Safari is in doubt, you can adjust whether you want to overwrite or ignore already written files. It don't save duplicate tabs, and you may set a property to decide whether it shold close the tab when it is saved.

You can overcourse use wget, but I settled for UI Scripting, as wget has download stuff that is already in your browser, and is a kluge to program as well.

osx - Save the source of an open webpage from Safari with AppleScript ...

osx safari applescript
Rectangle 27 0

Get Current Webpage from Safari
Download Urls

osx - Save the source of an open webpage from Safari with AppleScript ...

osx safari applescript
Rectangle 27 0

If you were to do this task manually, you would View Source in Safari, Copy the source to the clipboard, go into an HTML source code editor and make a new document, Paste the source code in, choose Save and navigate to the Documents folder, name the document, and then Save it.

So when you want to write an AppleScript to do this task, a key thing is that you still want to use those same apps, but instead of running them manually, you will run them with AppleScript. A great AppleScriptable HTML source code editor is TextWrangler, which is free from Mac App Store.

Once you have both a Web browser (Safari) to get the HTML source from the network and an HTML source code editor (TextWrangler) to create and Save the HTML document, you can write a very small, very easy to write, very easy to read, very easy to maintain AppleScript like this one:

tell application "Safari"
    activate
    if document 1 exists then
        set theDocumentTitle to the name of document 1
        set theDocumentSource to the source of document 1
        tell application "TextWrangler"
            activate
            set theNewDocument to make new document with properties {name:theDocumentTitle, text:theDocumentSource}
            set theDocumentsFolderPath to the path to the documents folder as text
            set theSaveFilePath to theDocumentsFolderPath & theDocumentTitle & ".html"
            save theNewDocument to file theSaveFilePath
        end tell
    end if
end tell

which will simply ask Safari to provide the name and source code of its frontmost document, and then ask TextWrangler to use that information to create and Save a matching HTML document in your Documents folder. Those are tasks that those 2 apps are each very good at. You sort of dont have to ask twice or do a lot of explaining.

osx - Save the source of an open webpage from Safari with AppleScript ...

osx safari applescript