Skip to content

Instantly share code, notes, and snippets.

@anilnatha
Last active August 29, 2015 14:04
Show Gist options
  • Save anilnatha/bac5749484a4804011d2 to your computer and use it in GitHub Desktop.
Save anilnatha/bac5749484a4804011d2 to your computer and use it in GitHub Desktop.
This AppleScript makes it easier to save screenshots to your filesystem than by using Apple's built-in keyboard shortcuts that do the same. I find Apple's keyboard shortcuts difficult to remember, which is why I created this script.
(*
Author: Anil Natha
Description: This applescript captures a screenshot and stores it at "~/Pictures/Screenshots" using a date/timestamp as its filename and once the screenshot is stored, opens a Finder Window scoped to the "~/Pictures/Screenshots" folder for quick access. This AppleScript provides the user with three screen capture options, "Desktop", "Window", and "Selection".
If "Desktop" is chosen, it simply takes a screenshot of the currently selected desktop (this distinction is important for computers with multiple monitors).
If "Window" is chosen, the user can choose which window they wish to capture a screenshot of.
If "Selection" is chosen, the user can choose an area that should be used to capture a screenshot of.
Installation:
If you want this script accessible to all users of a machine:
Store this script at the following location of your OS X disk, /Libary/Scripts
If you want this script accessbile to only your user account on a machine:
Store this script at the following location, where '~' is the path to your user folder: "~/Libary/Scripts"
Known Issues/Limitations:
1. Once this script is executed, there is no way to "cancel" it. You must select an option. Unfortunately due to a limitation by Apple, Button List objects can only contain a max of three items, so I was unable to add a "cancel" button.
Recommendations:
1. While this script is useful on its own, I personally use FastScripts to provide very easy access to all of the scripts housed in the scripts folder in my user account (~/Library/Scripts). By using FastScripts you can not only execute scripts via the OS X Menu Bar, but it allows you to map a keyboard shortcut to your individual scripts to quickly trigger them. Unfortunately, FastScripts isn't free, but it is very cheap. Once FastScripts is setup, I simply mapped the keyboard shortcut, COMMAND + SHIFT + S to easily capture screenshots.
2. Whether you use FastScripts or not, one of the ways I keep my scripts organzed is bycreating folders in my 'Scripts' folder and grouping similarly themed scripts. Within FastScripts, this provides the extra benefit of submenus for scripts, with each submenu being a folder in your Scripts folder. You can also prepend your scripts filenames with "01)", "02)" to change the order in which they appear in FastScripts (this is only useful to fastscripts), if you do not do this, your scripts will be alphabetically ordered.
*)
-- prompt user for type of screenshot they would like to take
set myButtonList to {"Desktop", "Window", "Selected Area"}
try
set response to button returned of (display dialog "Take screenshot of..." buttons {item 1 of myButtonList, item 2 of myButtonList, item 3 of myButtonList})
if response = item 1 of myButtonList then
--process the creation of a screenshot of the desktop
set options to ""
else if response = item 2 of myButtonList then
--process the creation of a screenshot of a selected window
set options to "-Wo"
else if response = item 3 of myButtonList then
--process the creation of a screenshot of a selected area
set options to "-s"
end if
end try
-- determine if path to '~/Pictures/Screenshots' exists, if not, create it
set homeFolderPath to POSIX path of (path to home folder as string)
set screenshotStoragePath to homeFolderPath & "Pictures/Screenshots/"
tell application "System Events"
if (not (exists folder screenshotStoragePath)) then
tell application "Finder"
make new folder at (path to home folder as string) & "Pictures" with properties {name:"Screenshots"}
end tell
end if
end tell
-- initialize storage location to be used for screenshot
set timeStamp to do shell script "date '+%Y-%m-%d at %I.%M.%S %p'"
set screenshotFilePath to screenshotStoragePath & "Screen Shot " & timeStamp & ".png"
-- save screenshot
do shell script "screencapture " & options & " " & quoted form of screenshotFilePath
-- open the newly created screenshot in Finder
tell application "Finder"
activate
set filepath to POSIX file screenshotFilePath as alias
reveal filepath
end tell
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment