Automatically change the name of screenshot files

Mar 27, '09 07:30:03AM

Contributed by: Anonymous

One of the side-effects of running The MacTipper Blog is that I take a lot of screenshots. Right now, I have more than 150 screenshots in my trash can. My point with all this is that I deal with a lot of screenshots. One of my complaints when dealing with screenshots is that you cannot quickly select a screenshot with the keyboard. To do that, I need to navigate to the folder, hit Tab, then type in Picture, press the Space Bar, then type the number of the screenshot I want.

In order to solve this, I have written a script to rename any of the default-named screenshot files to a name you prefer with the number before the name. For example, this script will change Picture 1.png to 1 Screenshot.png. Here's the script:

--Written by MacTipper for The MacTipper Blog: http://www.mactipper.com/2009/03/script-to-change-default-name-of.html
--This script allows you to change the default name of screenshots by combining it with a LaunchD script.
--Everything there is to know about screenshots: http://www.mactipper.com/2008/08/everything-there-is-to-know-about.html

property the_suffix : " Screenshot" --Everything that will appear after the number except for the extension.
property folder_path : "/Users/oliver/Pictures/Screenshots/" --Posix path to your screenshots folder. You can change the folder with this tip: http://tinyurl.com/changescreenshots
property default_extension : ".png" --Change this to the extension of screenshots you take.

on run
  tell application "Finder"
    set action_folder to ((POSIX file folder_path) as alias)
    set folder_items to every item in action_folder
  end tell
  repeat with an_item in folder_items
    tell application "Finder"
      set item_name to name of (an_item as alias)
      set the_comment to (get comment of (an_item as alias))
      log item_name
    end tell
    if the_comment does not contain "managed" then
      set an_item to (an_item as alias)
      if (item_name ends with default_extension) then
        if (item_name begins with "Picture") then
          set x to 0
          repeat
            set x to x + 1
            set the_name to (x & the_suffix & default_extension) as string
            set the_path to (POSIX file (folder_path & the_name)) as string
            tell application "Finder"
              try
                (the_path as alias)
              on error err
                log err
                if err is (("File " & the_path & " wasn't found.") as string) then
                  exit repeat
                else
                  return err
                end if
              end try
            end tell
          end repeat
          tell application "Finder"
            set comment of an_item to (comment of an_item) & " managed"
            set name of an_item to the_name
          end tell
          
        end if
      end if
    end if
  end repeat
end run
You can also download the script if you prefer. Once you've created/downloaded the script, you'll need to set it up.

Here's how to get the script working:

  1. Put the script in a safe place.
  2. Open up the script and edit the first three properties: Once you're done editing the AppleScript, save it with Command-S.
  3. If you don't already have it, go get Lingon.
  4. Open up Lingon and make a new script by hitting Command-N.
  5. In the first dialog, select My Agents. For the name, use something like com.mactipper.screenshots.folderaction or anything else that you know will be unique. It is important that it has a unique name.
  6. For the 'What', type in osascript, press the Space Bar, then type a double-quote mark ("), then drag in the script you edited. Finally, end it with another double-quote mark.
  7. For the 'When', set Run it if this file is modified box to the folder that your screenshots go to. To make sure this is done properly, click on the text box then drag in the folder from Finder and drop it onto the box. Finally, add a trailing slash (/) to the file name.
  8. Hit Command-S to save the script.
  9. Restart your computer to load the launchd script.
Take a screenshot to make sure everything works. Note that the script can sometimes take a couple seconds to take action, but it does do the trick. If you need more details on the process, this post on my blog contains screenshots and more detail.

[robg adds: I haven't tested this one.]

Comments (12)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20090324075635255