Monday, December 18, 2017

#1 2017-08-02 08:17:11 am

Adam239
Member
Registered: 2016-10-14
Posts: 70

Applescript - Take Screenshot of ENTIRE Web Page - Safari

Hi there,

Is it possible to take a screenshot of the entire web page in safari?
I need an applescript that will do this - the page itself would require scrolling in order to see the entire thing.

Thanks!


running Sierra 10.12.5

Offline

 

#2 2017-08-02 09:43:15 am

haolesurferdude
Member
Registered: 2010-02-19
Posts: 109

Re: Applescript - Take Screenshot of ENTIRE Web Page - Safari

Open Safari to the url you would like to get the image of.  Then run this:

Applescript:


tell application "Safari"
   activate
   set winID to id of window 1
end tell

do shell script "screencapture -c -x -l " & winID

Offline

 

#3 2017-08-02 10:24:41 am

Adam239
Member
Registered: 2016-10-14
Posts: 70

Re: Applescript - Take Screenshot of ENTIRE Web Page - Safari

Thanks for your reply Halosurferdude,

But that script does nothing on my machine sad


running Sierra 10.12.5

Offline

 

#4 2017-08-02 10:55:59 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3203

Re: Applescript - Take Screenshot of ENTIRE Web Page - Safari

It certainly worked : it put the screenshot in the clipboard.

The problem is that it captures only what is displayed on the screen, not the entire page.

If you want to get the entire page you may use Print in a PDF.

Applescript:

# Build the name of the PDF to fit your needs
set thePDFname to "beurk.pdf"
set thePDFPath to (path to documents folder as text) & "the PDFs:"
set posixFolderPath to POSIX path of thePDFPath





tell application "System Events"
   set frontmostProcess to first process where it is frontmost -- this will be the script process
   if name of frontmostProcess is in {"Script Editor", "AppleScript Editor"} then # CAUTION the name changed
       set visible of frontmostProcess to false -- hide the script process
       repeat while (frontmostProcess is frontmost) -- wait until the script is hiden
           delay 0.2
       end repeat
       tell (first process where it is frontmost)
           set theProcess to its name
           set theApp to its file
       end tell
       set frontmost of frontmostProcess to true -- unhide the script process
   else
       tell frontmostProcess
           set theProcess to its name
           set theApp to its file
       end tell
   end if
   set theApp to name of theApp
end tell

activate application theApp
tell application "System Events" to tell process theProcess
   set nbw to count windows
   keystroke "p" using command down
   (*
Wait for the availability of the sheet *)

   repeat
       if exists sheet 1 of window 1 then exit repeat
   end repeat
   --name of menu buttons of sheet 1 of window 1
   --> {"PDF"} # but I don't know if it's spelled this way worldwide
   tell sheet 1 of window 1
       set PDFButton to first menu button
       click PDFButton
       -- name of menu items of menu 1 of PDFButton
       --> {"Ouvrir le PDF dans Aperçu", "Enregistrer au format PDF…", "Enregistrer au format PostScript…", "Faxer le document PDF…", missing value, "@ PDF-BAT.qfilter", "@ PDF-prépresse CMJN.qfilter", "@ PDF-web.qfilter", "@ PDFX3-ISO.qfilter", "Add PDF to iTunes", "Envoyer le document PDF par courrier électronique", "Enregistrer le document PDF dans le dossier de reçus web", missing value, "Modifier le menu…"}
       
       click menu item 2 of menu 1 of PDFButton
       (*
Wait for the availability of the Print sheet *)

       repeat
           if exists sheet 1 then exit repeat
       end repeat
       
       (*
Set the name of the new PDF *)

       tell sheet 1
           # Set the Desktop as destination folder
           
           set value of text field 1 to thePDFname
           keystroke "g" using {command down, shift down}
           repeat until exists sheet 1
               delay 0.02
           end repeat
           tell sheet 1
               # CAUTION. before 10.11 the target field was a text field. Now it's a combo box
               if class of UI elements contains combo box then
                   --> {static text, combo box, button, button}
                   set value of combo box 1 to posixFolderPath
               else
                   --> {static text, text field, button, button}
                   set value of text field 1 to posixFolderPath
               end if
               get name of buttons (*Aller, Annuler*)
               keystroke return # ditto click button 1 (ou Aller)
           end tell
           get name of buttons
           --> {missing value, missing value, missing value, "Enregistrer", "Nouveau dossier", "Annuler"}
           --click button -3 -- ditto keystroke return
           keystroke return
       end tell # sheet 1 (the Save As one)
   end tell # sheet 1 of window 1
end tell

# Now the pdf is available in the defined folder.

Last edited by Yvan Koenig (2017-08-03 05:20:08 am)

Offline

 

#5 2017-08-02 11:44:41 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3203

Re: Applescript - Take Screenshot of ENTIRE Web Page - Safari

If you may have use for the screencapture feature, here are several instructions using it :

Applescript:


tell application "Safari"
   activate
   set winID to id of window 1
end tell

# store the screenshot in the clipboard
--do shell script "screencapture -c -x -l " & winID

# WARNING : I found no explanation about the -l parameter.

# store the screenshot in the defined location
--do shell script "screencapture -x " & quoted form of POSIX path of ((path to desktop as text) & "my screenshot.png") & space & winID

# store the screenshot in the defined location and open it in Preview
--do shell script "screencapture -x -P " & quoted form of POSIX path of ((path to desktop as text) & "my screenshot.png") & space & winID

# store the screenshot in the defined location and open it in Preview. It allow you to define interactively the area to capture.
do shell script "screencapture -x -i -P " & quoted form of POSIX path of ((path to desktop as text) & "my screenshot.png") & space & winID

I would interested if somebody may explain what the -l parameter is supposed to rule.

Yvan KOENIG running Sierra 10.12.6 in French (VALLAURIS, France) mercredi 2 aout 2017 18:44:21

Offline

 

#6 2017-08-02 01:46:38 pm

haolesurferdude
Member
Registered: 2010-02-19
Posts: 109

Re: Applescript - Take Screenshot of ENTIRE Web Page - Safari

Yvan,

Bonjour! (Well its day here and I lived in Bordeaux when in first and second grade.)

-l followed by the window ID specifies that screen capture operate on the specific window.

The call

Applescript:

do shell script "screencapture -c -x -l " & winID

specifies to copy to the clipboard (-c), no sound (-x), at specified window (-l).

Your solution to get the entire page and not just the screen shot is nicely done.

Best!

Offline

 

#7 2017-08-02 02:25:31 pm

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3203

Re: Applescript - Take Screenshot of ENTIRE Web Page - Safari

Thanks

I asked because there is no parameter -l in the man page.
I just found the parameter -i whose description is :

-i      Capture screen interactively, by selection or window.  The con-
             trol key will cause the screen shot to go to the clipboard.  The
             space key will toggle between mouse selection and window selec-
             tion modes.  The escape key will cancel the interactive screen
             shot.

Yvan KOENIG running Sierra 10.12.6 in French (VALLAURIS, France) mercredi 2 aout 2017 21:25:17

Offline

 

#8 2017-08-03 05:09:29 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3203

Re: Applescript - Take Screenshot of ENTIRE Web Page - Safari

For the fun I built an alternate version which extract the localized strings used from system resources.

Applescript:

# Build the name of the PDF to fit your needs
set thePDFname to "beurk.pdf"
set thePDFPath to (path to documents folder as text) & "the PDFs:"
set posixFolderPath to POSIX path of thePDFPath

# Extract the localized strings
set theBundle to (path to library folder from system domain as text) & "PrivateFrameworks:PrintingPrivate.framework:Versions:A:Plugins:PrintCocoaUI.bundle:Contents:Resources:"
set x to "64.title"
set SaveAsPDFellipsis_loc to localized string x from table "CocoaPrintWindow" in bundle (theBundle as «class furl») --> "Enregistrer au format PDF…"
(*
set x to "64.ibExternalAccessibilityDescription"
set SaveAsPDF_loc to localized string x from table "CocoaPrintWindow" in bundle (theBundle as «class furl») --> "Enregistrer au format PDF" # I don't know where it's used
*)

set x to "65.title"
set PDF_loc to localized string x from table "CocoaPrintWindow" in bundle (theBundle as «class furl») --> "PDF"


tell application "System Events"
   set frontmostProcess to first process where it is frontmost -- this will be the script process
   if name of frontmostProcess is in {"Script Editor", "AppleScript Editor"} then # CAUTION the name changed
       set visible of frontmostProcess to false -- hide the script process
       repeat while (frontmostProcess is frontmost) -- wait until the script is hiden
           delay 0.2
       end repeat
       tell (first process where it is frontmost)
           set theProcess to its name
           set theApp to its file
       end tell
       set frontmost of frontmostProcess to true -- unhide the script process
   else
       tell frontmostProcess
           set theProcess to its name
           set theApp to its file
       end tell
   end if
   set theApp to name of theApp
end tell # System Events

activate application theApp
tell application "System Events" to tell process theProcess
   --set nbw to count windows # unused
   set frontmost to true
   keystroke "p" using command down
   (*
Wait for the availability of the sheet *)

   repeat
       if exists sheet 1 of window 1 then exit repeat
   end repeat
   --class of UI elements of sheet 1 of window 1
   --> {group, static text, group, static text, pop up button, static text, pop up button, static text, text field, checkbox, static text, radio button, radio button, text field, static text, text field, static text, pop up button, static text, static text, radio group, static text, text field, pop up button, checkbox, checkbox, button, menu button, button, button, button}
   --name of menu buttons of sheet 1 of window 1
   --> {"PDF"} # but I don't know if it's spelled this way worldwide
   tell sheet 1 of window 1
       click menu button PDF_loc
       -- name of menu items of menu 1 of PDFButton
       --> {"Ouvrir le PDF dans Aperçu", "Enregistrer au format PDF…", "Enregistrer au format PostScript…", "Faxer le document PDF…", missing value, "@ PDF-BAT.qfilter", "@ PDF-prépresse CMJN.qfilter", "@ PDF-web.qfilter", "@ PDFX3-ISO.qfilter", "Add PDF to iTunes", "Envoyer le document PDF par courrier électronique", "Enregistrer le document PDF dans le dossier de reçus web", missing value, "Modifier le menu…"}
       
       click menu item SaveAsPDFellipsis_loc of menu 1 of menu button PDF_loc
       (*
Wait for the availability of the Print sheet *)

       repeat
           if exists sheet 1 then exit repeat
       end repeat
       
       (*
Set the name of the new PDF *)

       tell sheet 1
           # Set the Desktop as destination folder
           
           set value of text field 1 to thePDFname
           keystroke "g" using {command down, shift down}
           repeat until exists sheet 1
               delay 0.02
           end repeat
           tell sheet 1
               # CAUTION. before 10.11 the target field was a text field. Now it's a combo box
               (*
               if class of UI elements contains combo box then
                   --> {static text, combo box, button, button}
                   set value of combo box 1 to posixFolderPath
               else
                   --> {static text, text field, button, button}
                   set value of text field 1 to posixFolderPath
               end if
               *)

               # An alternate way would be
               set value of UI element 2 to posixFolderPath
               # But there is no guarantee that the index remains equal to 2
               get name of buttons (*Aller, Annuler*)
               keystroke return # ditto click button 1 (ou Aller)
           end tell
           get name of buttons
           --> {missing value, missing value, missing value, "Enregistrer", "Nouveau dossier", "Annuler"}
           --click button -3 -- ditto keystroke return
           keystroke return
       end tell # sheet 1 (the Save As one)
   end tell # sheet 1 of window 1
end tell

# Now the pdf is in the defined folder.

Yvan KOENIG running Sierra 10.12.6 in French (VALLAURIS, France) jeudi 3 aout 2017 12:09:22

Offline

 

#9 2017-08-03 08:01:04 am

Adam239
Member
Registered: 2016-10-14
Posts: 70

Re: Applescript - Take Screenshot of ENTIRE Web Page - Safari

Thank you Yvan!

I made a small modification, so that it sets date and time as the filename and saves it into a screenshot folder on the desktop.

(my management of dates and times in applescript needs some work, I know.)

Your solution works perfectly!

Applescript:

set {year:y, month:m, day:d} to (current date)
set CurrentTime to (do shell script "date +\"%l %M %p\" | awk '{$1=$1;print}'")
set theDate to y & space & m & space & d as text
set ssfileName to theDate & " - " & CurrentTime & ".pdf"

set thePDFPath to (path to desktop folder as text) & "Screenshots"
set posixFolderPath to POSIX path of thePDFPath


tell application "Safari"
   activate
   delay 1
   set the URL of the front document to "[url]http://www.google.com[/url]"
   delay 30
end tell

tell application "System Events"
   set frontmostProcess to first process where it is frontmost -- this will be the script process
   if name of frontmostProcess is in {"Script Editor", "AppleScript Editor"} then # CAUTION the name changed
       set visible of frontmostProcess to false -- hide the script process
       repeat while (frontmostProcess is frontmost) -- wait until the script is hidden
           delay 0.2
       end repeat
       tell (first process where it is frontmost)
           set theProcess to its name
           set theApp to its file
       end tell
       set frontmost of frontmostProcess to true -- unhide the script process
   else
       tell frontmostProcess
           set theProcess to its name
           set theApp to its file
       end tell
   end if
   set theApp to name of theApp
end tell

activate application theApp
tell application "System Events" to tell process theProcess
   set nbw to count windows
   keystroke "p" using command down
   
   repeat
       if exists sheet 1 of window 1 then exit repeat
   end repeat
   
   tell sheet 1 of window 1
       set PDFButton to first menu button
       click PDFButton
       click menu item 2 of menu 1 of PDFButton
       
       repeat
           if exists sheet 1 then exit repeat
       end repeat
       
       
       tell sheet 1
           # Set the Desktop as destination folder
           
           set value of text field 1 to ssfileName
           delay 2
           keystroke "g" using {command down, shift down}
           repeat until exists sheet 1
               delay 2
           end repeat
           tell sheet 1
               # CAUTION. before 10.11 the target field was a text field. Now it's a combo box
               if class of UI elements contains combo box then
                   --> {static text, combo box, button, button}
                   set value of combo box 1 to posixFolderPath
               else
                   --> {static text, text field, button, button}
                   set value of text field 1 to posixFolderPath
               end if
               get name of buttons
               keystroke return
           end tell
           get name of buttons
           
           keystroke return
       end tell
   end tell
end tell


running Sierra 10.12.5

Offline

 

#10 2017-08-03 08:24:07 am

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5200

Re: Applescript - Take Screenshot of ENTIRE Web Page - Safari

Here's an alternative approach. It creates an invisible Web view of the same size, downloads the URL again, then saves it a PDF. About the only difference in the result is that the resulting PDF is a single page, no breaks.

Applescript:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use framework "AppKit"
use framework "WebKit"
property theView : missing value -- the WebView
property pdfData : missing value -- the PDF data
property theTitle : missing value -- the frame title
property readyFlag : true -- flag that signifies done
use scripting additions

tell application "Safari"
   tell (window 1 whose visible is true)
       set {x1, y1, x2, y2} to bounds
       set theURL to URL of tab 1 whose visible is true
   end tell
end tell

if current application's NSThread's isMainThread() as boolean then
   -- set up WebView
   my setUpWebviewSized:{x2 - x1, y2 - y1}
   -- load the URL
   my processURL:theURL
else
   -- set up WebView
   my performSelectorOnMainThread:"setUpWebviewSized:" withObject:{x2 - x1, y2 - y1} waitUntilDone:true
   -- load the URL
   (my performSelectorOnMainThread:"processURL:" withObject:theURL waitUntilDone:true)
end if
repeat while not my readyFlag
   delay 0.3
end repeat
set thePath to current application's NSString's stringWithFormat_("%@/%@.pdf", POSIX path of (path to desktop), theTitle)
pdfData's writeToFile:thePath atomically:true
display dialog "Finished" buttons {"OK"} default button 1

-- needs to be done on the main thread
on setUpWebviewSized:theSize
   -- make a WebView
   set my theView to current application's WebView's alloc()'s initWithFrame:{origin:{x:0, y:0}, |size|:theSize}
   -- tell it to call delegate methods on me
   theView's setFrameLoadDelegate:me
end setUpWebviewSized:

-- needs to be done on the main thread
on processURL:thePageURL
   -- load the page
   (my theView)'s setMainFrameURL:thePageURL
   -- set ready flag to false
   set my readyFlag to false
end processURL:

-- called when the WebView loads a frame
on WebView:aWebView didFinishLoadForFrame:webFrame
   -- the main frame is our interest
   if webFrame = aWebView's mainFrame() then
       set my theTitle to aWebView's mainFrameTitle()'s stringByReplacingOccurrencesOfString:"/" withString:"_"
       set docView to webFrame's frameView()'s documentView()
       set my pdfData to docView's dataWithPDFInsideRect:(docView's frame())
       set my readyFlag to true
   end if
end WebView:didFinishLoadForFrame:

on WebView:WebView didFailLoadWithError:theError forFrame:webFrame
   -- got an error, bail
   WebView's stopLoading:me
   error theError's localizedDescription() as text
end WebView:didFailLoadWithError:forFrame:


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#11 2017-08-03 08:38:54 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3203

Re: Applescript - Take Screenshot of ENTIRE Web Page - Safari

Thank you Shane.

Will be part of my library of scripts.

Yvan KOENIG running Sierra 10.12.6 in French (VALLAURIS, France) jeudi 3 aout 2017 15:38:48

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)