Get Completely Sized macOS Window Screenshots Each Time With AppleScript

If you have a lot of screenshots to take on your Mac, you might like this trick to get them the right size every time.

Taking screenshots has always been something macOS did very well. Shortcuts allow you to send screenshots to the clipboard or save them to the desktop. You can capture the entire screen or any rectangle. You can also capture a specific window with an automatic drop shadow.

The window image in particular provides a very nice result. But it's not easy to take a screenshot with exact dimensions. Resizing a window to an exact pixel width and height is tricky enough. The extra shadow makes it even more difficult.

So you can take perfect size window screenshots every time.

Step 1. Learn how to run AppleScript

Although you can resize images using Photos or Previews, there is a lot of effort involved, especially when it comes to window-based screenshots. Our technique uses AppleScript, a scripting language that enables Mac applications to be accessed. It is exactly what you need to set the exact size of a window.

You can run AppleScript commands and programs in two ways:

  1. The script editor app has a GUI and accessible documentation.

  2. You can run the osascript program from the command line with Terminal. You can send AppleScript as input to Osascript as follows:
    echo & # 39; … Applescript goes here … & # 39; | Osaskript

Which option you use depends on your personal preference.

See also: What is AppleScript? Write your first Mac automation script

Step 2. Resize a window precisely using AppleScript

The command to enlarge a window in AppleScript is:

Tell the Finder application to set the front window limits to {25, 50, 1025, 650}

Note that this is pretty verbose. So enter it exactly as shown. The individual parts work as follows:

Say "Finder" to the application sets up the app you want to work with. Here it's the Finder, but you can use any running app with an open window.

put on the borders of the windshield describes the action to be performed. You set the size of the front window so make sure you bring the one you want to the front first.

{25, 50, 1025, 650} are the limits. Note that these are the x and y coordinates of the upper left and lower right corners, respectively. To do this, the actual values ​​are less important than the difference between them – width and height.

Step 3. Calculate the shadow size

The final piece of the puzzle is to work out the size of the drop shadow so you can take it into account. This involves a little work, let's just tell you the shadow is 112 × 112 pixels.

However, in case that changes or you ever need to work it out from scratch, here's what to do.

  1. Start by getting the dimensions of a window, each window will do the following:
    Tell the application "Finder" to get the boundaries of the front window

  2. Subtract the first two values ​​from the second two to get the width and height.

  3. Now take a screenshot of the window (Shift + Command + 4, then place, then return).

  4. Select the screenshot image file in the Finder and note the dimensions in the preview window (View> Show Preview). These should be bigger than the width and height you got in the last step. The difference is in the size of the shadow.

Step 4. Put everything together

Let's say you want a screenshot that is exactly 1024 × 768 pixels. Here is what you need to do:

  1. Start by adding the shadow dimensions. If they are 112 × 112, then our last window is (1024 + 112) × (768 + 112) – that's 1136 × 880.

  2. Now choose coordinates for the upper left corner and add them to these dimensions to get the lower right corner e.g. {25, 50, 1136 + 25, 880 + 50} which is {25, 50, 1161, 930}.

  3. Run the AppleScript command:
    Tell the Finder application to set the front window limits to {25, 50, 1161, 930}

  4. Finally, take the screenshot (Shift + Cmd + 4, then Space, then click).

If this looks like a lot of work, don't worry – you only need to do steps 1-2 once. If you regularly use several different image sizes, you can save an AppleScript file for each.

Use AppleScript to perfect your window screenshots

This technique requires some setup effort to begin with, but it can save you a lot of time in the long run. There's a surprising lack of window sizing features in macOS, but AppleScript helps you work around those shortcomings. For perfectly sized screenshots without the hassle of cropping, nothing beats pixel precision!

If you have a lot of screenshots to work with, consider using a batch resizing method.

How to batch convert and scale images on your Mac

Don't waste time converting and resizing images separately! Here's how to quickly batch convert and resize images on your Mac in bulk.

Continue reading

About the author

Bobby Jack
(50 articles published)

Bobby is a technology enthusiast who has worked as a software developer for nearly two decades. Passionate about games, he works as a review editor at Switch Player Magazine, and delves into all aspects of online publishing and web development.

More
From Bobby Jack

Subscribe to our newsletter

Subscribe to our newsletter for tech tips, reviews, free e-books, and exclusive offers!

One more step …!

Please confirm your email address in the email we just sent you.

Leave a Reply

Your email address will not be published. Required fields are marked *