2

I want to take a screenshot of a window on a mac.

I'm currently using shift + cmd + 4 then space then mouse click.

The process is very awkward, you got to do it in that order or you'll have to repeat it again..

I'm wanting a simple keyboard shortcut to do what shift + cmd + 3.

I want to use as simple a shortcut for taking a screenshot of a window.

Is it possible?

2

Here's my solution, which I named Window-Snapshot, base on a very simple AppleScript, either saved as an application from within the Script Editor and or used as part of an Automator Service with keyboard shortcut assigned. Although as a Service it uses the third-party utility used in the Window-Snapshot AppleScript application's one* mouse-click solution detailed below.

  • Note: Automator used as a Service is, by itself, a nice feature however when used with a global keyboard shortcut it can be problematic unless the keyboard shortcut used, is both usable and not in use elsewhere in OS X or any opened Applications whose window is to be captured. Which is why I've included using the AppleScript both as an AppleScript Application and or an Automator Service with keyboard shortcut assigned. While some keyboard shortcuts assigned to an Automator Service will work with most application windows nonetheless it may not work with some, even if that keyboard shortcut isn't assigned to that application of the window being captured. In such cases the Window-Snapshot AppleScript application can be used over the Automator Service.

As an AppleScript Application

This can be setup to be either a two mouse-click solution (the default) or a one* mouse-click solution.

  • * Note: As a one* mouse-click solution it requires the use of a third party utility called CLICLICK. As far as I know, there is no direct mouse/left mouse down event in AppleScript and thus the use of either a third party utility or otherwise the use of additional code then what's already being presented to employ methods that use other resources, native and or third-party.

As a two mouse-click solution the AppleScript it's saved as an application, I saved it as Window-Snapshot in Applications, and then placed it in the Dock by dragging and dropping the application bundle onto the Dock where I wanted it to be.

  • What I like about the two mouse-click solution is when the Dock Tile of the Window-Snapshot application is clicked, it's literally like I had manually pressed shift-command-4 then Space bar on the keyboard and now all I have to do is mouse-over the target window I want to capture and then mouse-click it. Thus a two mouse-click solution! This method allows pressing the esc key to abort, just as the manually pressed keyboard shortcut would have. Also it doesn't rely on using a third party utility and a second delay command as a timer to allow positioning the mouse pointer before it times out and automatically takes the snapshot used in the one* mouse-click solution. By itself the two mouse-click solution is the easiest solution presented herein.

As a one* mouse-click solution the AppleScript is saved as an application and the third party utility CLICLICK is added to the application bundle and corresponding code is uncommented in the script.

  • Note: The one* mouse-click solution uses a second delay command to allow time in positioning the mouse pointer anywhere over the target window to be captured and doesn't matter whether or not the window is the top-most window, when the second delay command times out whichever window the mouse is over that's the one captured in the snapshot. The capture can be aborted by pressing the esc key before the second delay command times out.

As an Automator Service with keyboard shortcut

As Automator Service it's created using the ApplesScript code, modified slightly, and a keyboard shortcut is assigned to the Service in: System Preferences > Keyboard Shortcuts > Services

  • Note: The keyboard shortcut I assigned is shift-command-5 or ⇧⌘5, which seemed a logical one to use, and whichever window the mouse is over is the window that will be captured by the snapshot. The window does not need to be totally visible to be captured or be the top-most window.

Window-Snapshot AppleScript Code

tell application "System Events"

    (* Note: If used as the default two mouse-click solution then use as is, nothing else needs to be done. *)

    keystroke "$" using {command down}
    delay 0.25
    keystroke space

    (* Note: If using as the one mouse-click solution or Automator Service, uncomment the 
    two lines below having double-dashes in front by removing the double-dashes. 
    Also when using as the one mouse-click solution, set a value on the 'delay' command 
    below to an appropriate value which allows you the time necessary to move the mouse  
    over the target window before that delay command times out and the snapshot is taken.
    The default of '1.5' is in this use case is a somewhat arbitrary value, set it to suite.

    Note: When using as an Automator Service set the 'delay' command below to '0.25', 
    the same as in the first 'delay' command. *)

    -- delay 1.5
    -- do shell script "/Applications/Window-Snapshot.app/Contents/Resources/cliclick c:."

end tell
  • Note: As you can see there is a 0.25 second value, delay 0.25, on the first delay command, this short delay is necessary as there needs to be a delay between the two keystroke commands. If using the one* mouse-click solution the second delay command needs to be something more realistic. Set a value that allows you the time necessary to move the mouse over the target window before the delay command expires, e.g. 1.5 etc.

Creating the Window-Snapshot Application

  1. Open Script Editor (or AppleScript Editor if applicable).

  2. Copy and paste the Window-Snapshot AppleScript Code, from above, into the Untitled document window and then press Compile.

  3. Press S to save and set the File Format: to Application saving it in the Applications folder.

  4. Give the Window-Snapshot application bundle a custom icon. I used an icon of a camera found at Find Icons and used this one in particular clicking the 'Download ICNS' button. I opened the downloaded "scanners_cameras.icns" file in Preview and copied it and then pasted it into the Get Info sheet of the Window-Snapshot.app by first selecting the little icon in the upper left corner of the Window-Snapshot Info sheet. (In Applications select the Window-Snapshot.app and press: I)

  5. Now drag and drop the Window-Snapshot application bundle from the Applications folder onto the Dock where you would like it to be.

It's now ready to be used, click the Window-Snapshot Dock Tile and then click the target window to be captured.

  • Note: The snapshots are saved with the same naming and location settings as if ⇧⌘3 was pressed only saving just the target windows. The default is on the Desktop using Screen Shot yyyy-mm-dd at hh.mm.ss AM|PM or if the default has been changed it will be to however it's currently set.

Setting up the Window-Snapshot.app to be a one* mouse-click solution using CLICLICK

  1. Download CLICLICK and mount the cliclick.dmg file and place a copy of the cliclick executable inside the Window-Snapshot.app application bundle at the path show in the script. Ctrl-Click the Window-Snapshot app selecting Show Package Contents and navigate to the Resources folder placing a copy of the cliclick executable there.

  2. Uncomment the two lines below the (* Note: If using as the one mouse-click solution or Automator Service, comments by removing the double-dashes, --, in front of the code.

  3. Save the changes made to the AppleScript.

It's now ready to be used as a one* mouse-click solution.

Creating the Window-Snapshot Automator Service

  1. Open Automator selecting Service.

  2. Set Service receives selected to no input leaving in any application as is. Add a Run AppleScript and then copy and paste the Window-Snapshot AppleScript Code above into the Run AppleScript window overwriting the default code.

  3. Uncomment the two lines below the (* Note: If using as the one mouse-click solution or Automator Service, comments by removing the double-dashes, --, in front of the code and set the value of the second delay command to 0.25.

  4. Save the Service pressing S naming it as Window-Snapshot.

  5. Open System Preferences and navigate to Keyboard > Keyboard Shortcuts > Services and scroll to the bottom, then under General you'll see Window-Snapshot and select it. Now click on add shortcut and press shift-command-5 or: ⇧⌘5

It's now ready to be use as a Service using the keyboard shortcut: ⇧⌘5

  • Note: If you only want to use it as a Service you'll need to adjust the pathname to the cliclick executable to wherever you installed it. You could try putting it in the Services bundle at, e.g. /Users/$USER/Library/Services/Window-Snapshot.workflow/Contents, although I did not try this as I like having it as both an AppleScript application using it as a two mouse-click solution and an Automator Service with keyboard shortcut assigned.

In closing, I'd like to say that this very simple AppleScript certainly can be expounded upon to add additional functionality both as an application or a service, i.e. adding code to save the snapshot with different names and locations etc., however I'll leave that to you to modify if you so choose.

  • It works perfectly, ⇧⌘5 and it saves, your solution will help many people. Thank you. – Paul Aug 15 '15 at 23:33
  • @Paul, Glad you can use it. I use a different ver. of the AppleScript App and did not include the code here as it uses another 3rd-party executable. The 2nd executable is called checkModifierKeys and does what the name implies and I have it coded so that if I press the shift key when I click the Dock Tile it toggles the disable-shadow key in the screencapture's .plist file so I can take snapshots without the drop shadow easily. Both executables I built from source code the authors provided although they both provide the binaries. If you're interested, let me know I'll update the answer. – user3439894 Aug 16 '15 at 0:21
1

One approach would be to use the screencapture command:

screencapture -w window.png

which will capture the first window you click after you run the command, and save it as window.png.

There are a few different ways you could bind this command to a keyboard shortcut, e.g. by creating an Automator service that runs it as a shell script, or by using a third-party launcher like Alfred. Either way, you'd probably also want to add some smarts to the process that chose a different filename each time, so you didn't keep overwriting your previous screenshot.

I don't know an easy way to always just capture the front-most window... perhaps an AppleScript guru can help with that?

  • Where did the window.png save? also with that method using terminal; still have to mouse click, it does make it easier and I'll look into automator and alfred, thankyou. – Paul Aug 10 '15 at 16:13
  • With the command above, it will save in whatever folder you ran the command from. If you wanted to always save on the desktop, for example, you could specify that location as part of the filename, e.g. 'screencapture -w ~/Desktop/window.png'. Yes, appreciate mouse click is still required though... wasn't able to figure out a way around that so far. – calum_b Aug 10 '15 at 17:10
0

Why not just enable enhanced dictation commands, then create a new voice command? In the example below I created a voice command that when I say "Grab Picture" it will take a screen shot. Completely bypass the keyboard LOL

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .