Applescript - Browser Screenshots automatisieren: Unterschied zwischen den Versionen
(Die Seite wurde neu angelegt: „ https://mac.appstorm.net/general/applescript-automatically-create-screenshots-from-a-list-of-websites/ AppleScript: Automatically Create Screenshots from a L…“) |
|||
| (2 dazwischenliegende Versionen desselben Benutzers werden nicht angezeigt) | |||
| Zeile 1: | Zeile 1: | ||
| + | https://macscripter.net/viewtopic.php?id=45871 | ||
https://mac.appstorm.net/general/applescript-automatically-create-screenshots-from-a-list-of-websites/ | https://mac.appstorm.net/general/applescript-automatically-create-screenshots-from-a-list-of-websites/ | ||
| + | |||
| + | == Beispiele == | ||
| + | |||
| + | === Beispiel 1 (Image)=== | ||
| + | <pre> | ||
| + | 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 | ||
| + | </pre> | ||
| + | |||
| + | -l followed by the window ID specifies that screen capture operate on the specific window. | ||
| + | |||
| + | The call | ||
| + | |||
| + | |||
| + | Open this Scriplet in your Editor: | ||
| + | do shell script "screencapture -c -x -l " & winID | ||
| + | |||
| + | specifies to copy to the clipboard (-c), no sound (-x), at specified window (-l). | ||
| + | |||
| + | === Beispiel 2 (pdf)=== | ||
| + | PDF | ||
| + | |||
| + | </pre> | ||
| + | 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 | ||
| + | </pre> | ||
| + | === Beispiel 3 (Anleitung - Websiteliste) === | ||
AppleScript: Automatically Create Screenshots from a List of Websites | AppleScript: Automatically Create Screenshots from a List of Websites | ||
Aktuelle Version vom 4. Oktober 2019, 10:09 Uhr
https://macscripter.net/viewtopic.php?id=45871 https://mac.appstorm.net/general/applescript-automatically-create-screenshots-from-a-list-of-websites/
Beispiele[Bearbeiten]
Beispiel 1 (Image)[Bearbeiten]
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
-l followed by the window ID specifies that screen capture operate on the specific window.
The call
Open this Scriplet in your Editor:
do shell script "screencapture -c -x -l " & winID
specifies to copy to the clipboard (-c), no sound (-x), at specified window (-l).
Beispiel 2 (pdf)[Bearbeiten]
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
Beispiel 3 (Anleitung - Websiteliste)[Bearbeiten]
AppleScript: Automatically Create Screenshots from a List of Websites Joshua Johnson on June 17th 2011 AppleScript It’s been quite a while since we’ve had any fun with AppleScript so today we’re going to build a super basic script that automatically reads a list of URLs and turns them into screenshots.
If you’re new to AppleScript, be sure to read our introduction and advanced articles! We’ll be explaining things as we go but everything will make a lot more sense once you do your homework.
The Workflow As a full-time blogger, it’s quite often the case that I have to put together a list of well-designed websites or some other post that requires me to post screenshots of anywhere from fifty to one hundred web pages. This is an exhausting process!
Since I’m lazy and love to sit back and watch my computer do my work for me, I wrote a simple script to handle the process. Basically, we’ll be using a modified version of a TUAW script to take a screenshot and adding in some of our own code to progressively launch a list of links. Let’s get started!
Algorithm An algorithm is just a fancy word for the general path of our script. Before you write a single line of code, you should always outline the steps that you’ll need to take to reach your goal. For this post, ours is quite simple:
Create a list of URLs in Text Edit (manually) Count the URLs in TextEdit Grab the first URL Open the first URL in Safari Take a screenshot Repeat for the rest of the URLs That’s not so bad is it? We should be able to knock this out in no time.
Step 1: Declare Some Variables One of the first steps I like to take when coding is to think about the variables that I’ll need and declare them. For instance, here I know I’ll need one counter that will help me cycle through the URLs, a variable to hold the total number of URLs and a variable to hold a customizable file name for the resulting files.
To this end, we’ll declare our counter and filename first. I called the counter “whichUrl” because it will essentially be keeping track of which URL we’re on throughout the script. Notice that I’ll be using comments quite liberally throughout the script, this is a good practice to get into.
--Variables set whichUrl to 1 set fileNames to "webshot" Next up, we need to create a tell block that grabs the number of URLs from TextEdit. If we put each URL on a line of its own, AppleScript will see each URL as a paragraph, so we simply tell it to set our variable equal to the number of paragraphs in the front document. This effectively counts our URLs for us.
--Get Number of URLs tell application "TextEdit" set theCount to the count of paragraphs of front document end tell Step 2: Create a Repeat Block At this point of the script, we’ll have everything we need to start grabbing URLs and taking screenshots. What we need to do is create a loop of commands that will repeat for every URL. Since we took the number of URLs and put it into our “theCount” variable, we can tell AppleScript to repeat that many times.
--Repeat this for every URL repeat theCount times --some code here end repeat Step 3: Grabbing a URL The first step we want to do within our repeat block is to grab the first URL and put it in a variable so we can open it in Safari later. To do this, we take advantage of our whichURL variable, which is initially set to one.
--Repeat this for every URL repeat theCount times
--Get the next URL tell application "TextEdit" set currentUrl to paragraph whichUrl of front document as text end tell
end repeat As you can see, we told TextEdit to grab the contents of the first paragraph and throw it into the variable “currentURL”. Admittedly, my variables are a bit confusing and could be named a bit better! Feel free to change them to something that makes more sense to you.
Step 4: Open the URL in Safari Now that we have the URL set to a variable, it’s time to open a Safari window with the URL set to that value. Basically we just create a tell block and set the URL of document one to the variable we created in step three.
--Repeat this for every URL repeat theCount times
--Get the next URL tell application "TextEdit" set currentUrl to paragraph whichUrl of front document as text end tell
--Open the URL in Safari tell application "Safari" activate set the URL of document 1 to currentUrl end tell end repeat Step 5: Take the Screenshot One of the struggles that I had with creating this script is that the page wasn’t always fully loaded when the screenshot was taken, so the resulting file was either a blank Safari window or a half-loaded web page.
The simple solution is to insert a slight delay. If your Internet connection is slow, you might want to increase this value. After that, we then take and save the screenshot. The saving is done by determining the path to your desktop, then applying the filename variable we set up before plus a number on the end to indicate which URL it was. So the third URL should come out as “webshot-3.jpg” on your desktop.
--Repeat this for every URL repeat theCount times
--Get the next URL tell application "TextEdit" set currentUrl to paragraph whichUrl of front document as text end tell
--Open the URL in Safari tell application "Safari" activate set the URL of document 1 to currentUrl
--Wait until it loads, then take a screenshot delay 15 set saveToPath to ((POSIX path of (path to desktop)) & fileNames & "-" & whichUrl & ".jpg") as string do shell script "screencapture -tjpg " & quoted form of saveToPath end tell end repeat Step 6: Increase the Counter The last thing we need to do is increase the “whichURL” variable by one. This will cause the script to jump to URL #2 in your TextEdit Document and make sure the filename gets a “-2” on the end of it.
--Repeat this for every URL repeat theCount times
--Get the next URL tell application "TextEdit" set currentUrl to paragraph whichUrl of front document as text end tell
--Open the URL in Safari tell application "Safari" activate set the URL of document 1 to currentUrl
--Wait until it loads, then take a screenshot delay 5 set picPath to ((POSIX path of (path to desktop)) & fileNames & "-" & whichUrl & ".jpg") as string do shell script "screencapture -tjpg " & quoted form of picPath end tell
--Increase the counter for next time set whichUrl to whichUrl + 1 end repeat Putting it All Together That’s it! Our script is nice and short and will save you loads of time if you ever need to take a screenshot of a webpage. Here’s the full script, feel free to copy and paste it right into AppleScript Editor.
--Variables set whichUrl to 1 set fileNames to "webshot"
--Get Number of URLs tell application "TextEdit" set theCount to the count of paragraphs of front document end tell
--Repeat this for every URL repeat theCount times
--Get the next URL tell application "TextEdit" set currentUrl to paragraph whichUrl of front document as text end tell
--Open the URL in Safari tell application "Safari" activate set the URL of document 1 to currentUrl
--Wait until it loads, then take a screenshot delay 5 set picPath to ((POSIX path of (path to desktop)) & fileNames & "-" & whichUrl & ".jpg") as string do shell script "screencapture -tjpg " & quoted form of picPath end tell
--Increase the counter for next time set whichUrl to whichUrl + 1 end repeat Running the Script Before you test out the script, be sure to open TextEdit and copy in a list of links. You don’t need to save the document, just be sure that it’s the frontmost page in TextEdit and everything will work fine.
screenshot The Requisite TextEdit Document
Also, the script works best when you have maximized Safari windows, so before you begin, open a new window in Safari and stretch it as big as it’ll go. After that, you’re all set to hit play and watch the magic happen!
Room for Improvement As I said, this script saves me loads of time and I love using it, but I fully admit that it could stand to be improved. For starters, it’s limited to the visual portion of the page and doesn’t take a screenshot of the entire website. LittleSnapper works great for this but last I checked there wasn’t any AppleScript support (get on that RealMac).
Further, I’m not sure how to snap only preset, specific coordinates of the screen, so this script just grabs your entire screen, browser chrome, menu bar and all (check below for an alternate version). This is definitely undesirable but I always just automate the cropping as well. You could automate the crop with AppleScript but I find that it’s easier just to use Photoshop and the following steps:
Open one of the screenshots Create a new action and hit record Crop the image Close/save the image Stop recording Go to File>Automate>Batch and run the action on your entire folder of screenshots This is definitely a little annoying but running the script and action setup are a one-time process that can just be quickly run every time you use them. I’d definitely love to see some talented scripters make suggestions for how to improve this to be a little less clunky!
Alternative Version: Manually Select Grab Area If you really hate the cropping process, you can actually change the script so that you manually select the portion of the screen to capture. Everything else will stay automated, you simply have to draw a box for each capture to take place.
To accomplish this, change the shell script line near the bottom to the one shown below. All I added was a “-i” after the “screencapture” command.
do shell script "screencapture -i -tjpg " & quoted form of picPath Conclusion Taking screenshots of a huge list of websites for a roundup post can be time consuming and mind-numbing. Fortunately, with AppleScript and a little ingenuity, you can automate the entire process. Simply create your list of URLs then sit back and enjoy the show.
Leave a comment below if you have any suggestions for improving the script. Also, be sure to let me know what other tasks you find frustrating and wish you could automate, if I think it will help a lot of people, I’ll whip up a tutorial!