Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

10.6: Rename screenshots with Automator and AppleScript System 10.6
Snow Leopard only hintScreen capture on Mac OS is a fantastic way to take information around with you on a mobile phone. Of course, if capturing many different items, you need to label them. This hint shows how to use Automator and AppleScript to make this a one click process.

First, build an Automator workflow (choose Application from the template chooser) as follows:
  1. Finder » Find Finder Items: Search Desktop for Name -- begins with -- Screen Shot
  2. Finder » Rename Finder Items: Set this to Make Sequential, then set it to create a new name (Picture), with Place Number 'after name' and Start Numbers At set to '1'.
  3. Utilities » Run Applescript: Replace all the editable script with the script below, including the run as, etc.
    on run
      
      (*
    Replace Text In Item Names
    
    This script is designed to replace text in the names of specific files and/or folders in the front window of the desktop. 
    If no folder windows are open, the script will affect files and/or folders on the desktop.
    
    Copyright  20012007 Apple Inc.
    
    You may incorporate this Apple sample code into your program(s) without
    restriction.  This Apple sample code has been provided "AS IS" and the
    responsibility for its operation is yours.  You are not permitted to
    redistribute this Apple sample code as "Apple sample code" after having
    made changes.  If you're going to redistribute the code, we require
    that you make it clear that the code was descended from Apple sample
    code, but that you've made changes.
    *)
      
      --set the source_folder to choose folder with prompt "Folder containing items to edit:"
      
      -- get the path to the folder of the front window
      -- if no windows are open, the desktop folder will be used
      try
        tell application "Finder" to set the source_folder to (folder of the front window) as alias
      on error -- no open folder windows
        set the source_folder to path to desktop folder as alias
      end try
      
      set the search_parameter to "File Names"
      
      -- display dialog "Replacing Picture  with your text in File names:" buttons {"Cancel", "OK"} default button 2
      set the search_string to the "Picture"
      
      
      repeat
        display dialog "Enter replacement text:" default answer "" buttons {"Cancel", "OK"} default button 2
        set the replacement_string to the text returned of the result
        if the replacement_string contains ":" then
          beep
          display dialog "A file or folder name cannot contain a colon (:)." buttons {"Cancel", "OK"} default button 2
        else if the replacement_string contains "/" then
          beep
          display dialog "A file or folder name cannot contain a forward slash (/)." buttons {"Cancel", "OK"} default button 2
        else
          exit repeat
        end if
      end repeat
      
      display dialog "Replace " & the search_string & " with " & the replacement_string & " in every item name?" buttons {"Cancel", "OK"} default button 2
      
      set the item_list to list folder source_folder without invisibles
      set source_folder to source_folder as string
      repeat with i from 1 to count of the item_list
        set this_item to item i of the item_list
        set this_item to (source_folder & this_item) as alias
        set this_info to info for this_item
        set the current_name to the name of this_info
        set change_flag to false
        if the current_name contains the search_string then
          if the search_parameter is "Folder Names" and 
            folder of this_info is true then
            set the change_flag to true
          else if the search_parameter is "File Names" and 
            folder of this_info is false then
            set the change_flag to true
          else if the search_parameter is "Both" then
            set the change_flag to true
          end if
          if the change_flag is true then
            -- replace target string using delimiters
            set AppleScript's text item delimiters to the search_string
            set the text_item_list to every text item of the current_name
            set AppleScript's text item delimiters to the replacement_string
            set the new_item_name to the text_item_list as string
            set AppleScript's text item delimiters to ""
            my set_item_name(this_item, new_item_name)
          end if
        end if
      end repeat
      
      beep 2
    end run
    
    on set_item_name(this_item, new_item_name)
      tell application "Finder"
        --activate
        set the parent_container_path to (the container of this_item) as text
        if not (exists item (the parent_container_path & new_item_name)) then
          try
            set the name of this_item to new_item_name
          on error the error_message number the error_number
            if the error_number is -59 then
              set the error_message to "This name contains improper characters, such as a colon (:)."
            else --the suggested name is too long
              set the error_message to error_message -- "The name is more than 31 characters long."
            end if
            --beep
            tell me to display dialog the error_message default answer new_item_name buttons {"Cancel", "Skip", "OK"} default button 3
            copy the result as list to {new_item_name, button_pressed}
            if the button_pressed is "Skip" then return 0
            my set_item_name(this_item, new_item_name)
          end try
        else --the name already exists
          --beep
          tell me to display dialog "This name is already taken, please rename." default answer new_item_name buttons {"Cancel", "Skip", "OK"} default button 3
          copy the result as list to {new_item_name, button_pressed}
          if the button_pressed is "Skip" then return 0
          my set_item_name(this_item, new_item_name)
        end if
      end tell
    end set_item_name
  4. Save the workflow as an application.
Now when you screen grab anything which appears as Screen shot xxxxx on the Desktop, just double-click the application you created. You will be asked for a name which will become the prefix for each file, and multiple files will be suffixed with a number in time sequence. I stuck my application in the Toolbar of my Finder window.

Details:

You will note that the process is a bit cumbersome -- Automator first renames files to Picture..., then the script asks you for what you want to replace Picture... with. This is just customized from Apple's own Finder script. The latter used to work fine on its own pre-Leopard, when files were prefixed just with Picture....

The cleverest part of getting this to work is not mine. I had to edit the script because, in Automator, the number of items in command kept giving an error (No result was returned from some part of this expression). I needed to replace this with count as I read in this forum post.

OK that's all to read. Hope it saves somebody lots of paper like it did me!

[robg adds: I tested this, and it worked, but only if I set the search scope in the first command to Desktop. When set to Computer, it didn't seem to work. Also, if Automator would simply let you place a variable into the "New Name" field in the "Make Sequential" step, the entire AppleScript could be avoided. Alas, I couldn't find any way to do that.]
    •    
  • Currently 2.00 / 5
  You rated: 2 / 5 (17 votes cast)
 
[12,218 views]  

10.6: Rename screenshots with Automator and AppleScript | 15 comments | Create New Account
Click here to return to the '10.6: Rename screenshots with Automator and AppleScript' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
10.6: Rename screenshots with Automator and AppleScript
Authored by: TonyT on Dec 07, '09 10:10:48AM

Why not just highlight the items on the desktop and use an Automator rename service? If you use the "Rename Finder Items" action and select the "show this action..." option and save as a service, you can control-click (or right click) the highlighted items and run the service.

With this service you can either Add Date or Time, Add Text, Change Case, Make Sequential, Replace Text, or Name Single Item. (if you name sequential, be sure to put the Sort Finder Items first as part of this Automator Service)



[ Reply to This | # ]
10.6: Rename screenshots with Automator and AppleScript
Authored by: DamnItsHot on Dec 07, '09 10:53:42AM

Why not just use Automator's 'Take Screenshot' in a service which lets you specify the name, place etc... (or 'screencapture' in shell script to get a little more control if you prefer) when you actually take the screenshot. I do the former and add '_' + 'YearMonthDay' to file name to make sure they are always sequential. It avoids the desktop mess.

Note: To change screen capture behavior:
defaults write com.apple.screencapture name "New FileName"
killall SystemUIServer

defaults write com.apple.screencapture location "New Path"
killall SystemUIServer

defaults write com.apple.screencapture type [new format] // supports pdf, jpeg, tiff - default is png
killall SystemUIServer

jOhn

Edited on Dec 07, '09 10:56:00AM by DamnItsHot



[ Reply to This | # ]
10.6: Rename screenshots with Automator and AppleScript
Authored by: drudus on May 09, '10 04:00:09PM

Genius, I love defaults write.

The above commands make a of the need for the script redundant (if not all of it).



[ Reply to This | # ]
10.6: Rename screenshots with Automator and AppleScript
Authored by: llee on Dec 07, '09 01:35:11PM
It's been a while since I thought about this, but whenever I take a screen shot I always think "One of these days, I'm going to start managing screen shot names again." Here's what I've decided to try. What I really want is to be prompted to name my screen shot whenever I take one. So I put together this AppleScript and attached it to the Desktop folder as a folder action. If I'm not satisfied with it after trying it for a while, I'll consider the matter further.
on adding folder items to this_folder after receiving added_items
	tell application "Finder"
		set screenshot_name to (get displayed name of (item 1 of added_items))
	end tell
	if screenshot_name starts with "screen shot" then
		try
			tell application "Finder"
				set name_extension to (get name extension of (item 1 of added_items))
				set the folder_name to the name of this_folder
			end tell
			set the item_count to the number of items in the added_items
			set alert_message to "Folder Actions Alert:" & return & return
			set alert_message to alert_message & "A new screen shot exists in the " & the folder_name & "."
			repeat
				set the alert_message to (the alert_message & return & return & "You can enter a new name for it if you like..")
				display dialog the alert_message buttons {"OK", "Cancel"} default button 1 with icon 1 default answer "Enter new name here."
				set the new_item_name to ((text returned of the result) & "." & name_extension)
				if the new_item_name contains ":" then
					display dialog "A file or folder name cannot contain a colon (:), please rename." buttons {"Cancel", "OK"} default button 2
				else if the new_item_name contains "/" then
					display dialog "A file or folder name cannot contain a forward slash (/), please rename." buttons {"Cancel", "OK"} default button 2
				else
					repeat with i from 1 to (count added_items)
						my set_item_name((item i of added_items), new_item_name)
					end repeat
					exit repeat
				end if
			end repeat
		on error ther
			display dialog ther
		end try
	end if
end adding folder items to

on set_item_name(this_item, new_item_name)
	tell application "Finder"
		repeat
			set the parent_container_path to (the container of this_item) as text
			if not (exists item (the parent_container_path & new_item_name)) then
				try
					set the name of this_item to new_item_name
					set the error_message to error_message -- "The name is too long."					
					tell me to display dialog the error_message default answer new_item_name buttons {"Cancel", "OK"} default button 3
					copy the result as list to {new_item_name, button_pressed}
					my set_item_name(this_item, new_item_name)
				end try
				exit repeat
			else --the name already exists
				tell me to display dialog "This name is already taken, please rename." default answer new_item_name buttons {"Cancel", "OK"} default button 2
				copy the result as list to {new_item_name, button_pressed}
				my set_item_name(this_item, new_item_name)
				exit repeat
			end if
		end repeat
	end tell
end set_item_name
Edited on Dec 07, '09 01:39:45PM by llee


[ Reply to This | # ]
10.6: Rename screenshots with Automator and AppleScript
Authored by: mark hunte on Dec 08, '09 04:43:34PM
As a Quick example, using a hotkey app.
there is no need to change the name of the file after it has been created, You can set the name before you
do the screen capture.
The script below will ask for the folder you want to save to, the name of the captured file and delay to when the capture is taken.
I have set the format here as jpg.

As I said a quick example, no checking if file exist, and other stuff that could be added.


tell me to set thePath to (choose folder)
set thePath to (POSIX path of thePath)
set format to "jpg"
       set defAns to "." & format as string
tell me to display dialog thePath default answer defAns buttons {"Cancel", "OK "} default button 2
  copy the result as list to {text_returned, button_pressed}
tell me to (display dialog "set Delay" default answer 2 buttons {"Cancel", "OK "} default button 2)
  copy the result as list to {text_returned2, button_pressed2}
 set Delay_ to text_returned2 as number
 delay Delay_
set doit to do shell script ("screencapture   -t" & format & space & (POSIX path of thePath) & text_returned as string)
 
---
mh


[ Reply to This | # ]
10.6: Rename screenshots with Automator and AppleScript
Authored by: llee on Feb 01, '10 07:49:30AM
The only problem I see with that is that you have to replace or supplement the OS X default shortcuts with equivalents using either the same or different shortcuts that employ your script. (I know it's possible, I already do it with command-f.)The default screen shot shortcuts offer quite a bit of varied functionality, and I think it would be quite an exercise to duplicate it all just to employ a script. I think the folder action provides a better compromise.

[ Reply to This | # ]
10.6: Rename screenshots with Automator and AppleScript
Authored by: bcometa on Dec 10, '09 02:40:22PM

thanks llee, works great!



[ Reply to This | # ]
10.6: Rename screenshots with Automator and AppleScript
Authored by: homeshire on Dec 09, '09 03:41:18AM

can't get the above script to work. have tried as both a spark action and a simple script, and it stumbles with

can't set format of "jpg" to .

have tried on both a leopard and tiger machine. is this a snow leopard thingie?

I am referring to the script authored by mark hunte.

Edited on Dec 09, '09 03:46:58AM by homeshire



[ Reply to This | # ]
10.6: Rename screenshots with Automator and AppleScript
Authored by: mark hunte on Dec 09, '09 09:14:27AM

hmm,
I did do do this on 10.6, but it should work on 10.5,
Will run it on 10.5 and see whats up.

---
mh



[ Reply to This | # ]
10.6: Rename screenshots with Automator and AppleScript
Authored by: mark hunte on Dec 09, '09 09:41:41AM
Ok, I tested the script as a normal script on 10.5.7, work with no problem, In Sparkv3.0.beta, got an Issue with Having to stop
and startthe enabled of Spark, before it would pick the hot key up, seems to be a bug in spark.
Which I also have had on 10.6.
In Spark v2.1 worked with no issue.

So at a loss of why your copy can not set the jpg. Try and copy and paste the script again. There should be no
space between the -t and the jpg.. i.e : -tjpg, and do not use jpeg.
Also try changing the variable format to CaptureFormat, You may have a scripting addition that uses the word format .

---
mh

---
mh
Edited on Dec 09, '09 09:44:08AM by mark hunte


[ Reply to This | # ]
10.6: Rename screenshots with Automator and AppleScript
Authored by: homeshire on Dec 09, '09 10:31:19AM

Oh my --
at least I'm having fun. I just don't know what's going on. did not understand the part concerning no space between -t and extension because I could find no such spot in the script.

so instead I tried your idea about using the variable captureformat rather than plain format. that did the trick!!! sortof.

now when I run the script choosing the desktop folder as destination, my capture appears momentarily, then poofs into the aether. as well, the title is .png , not the extension -- the extension is gibberish that was different gibberish on three different captures.

then I tried it with a different destination. it stayed put, and even moved to my desktop without poofing. but it turned up as a plain text file of 2.2 mb size. so I changed the extension to .png and it opened just fine.

I'll paste the code I've used exactly so you can peruse it. maybe I'm just not seeing something:

tell me to set thePath to (choose folder)
set thePath to (POSIX path of thePath)
set captureformat to "png"
set defAns to "." & (captureformat) as string
tell me to display dialog thePath default answer defAns buttons {"Cancel", "OK "} default button 2
copy the result as list to {text_returned, button_pressed}
tell me to (display dialog "set Delay" default answer 2 buttons {"Cancel", "OK "} default button 2)
copy the result as list to {text_returned2, button_pressed2}
set Delay_ to text_returned2 as number
delay Delay_
set doit to do shell script ("screencapture -t" & (captureformat) & space & (POSIX path of thePath) & text_returned as string)

btw --10.5.8 on a g5 iMac

thanks for your attention mh

p.s. I too am using spark 3.0b9

p.p.s. mh -- if I delete the leading period before png in the script which is defined as the start of the variable defAns, the file does not poof when the script is run. it stays on the desktop as a plain file with no extension - with the title png. if I manually add the extension, it transforms into a png that opens as expected.

Edited on Dec 09, '09 10:42:45AM by homeshire



[ Reply to This | # ]
10.6: Rename screenshots with Automator and AppleScript
Authored by: mark hunte on Dec 09, '09 11:57:20AM
The fact that the format thing worked points to you running additions,
There maybe more running that are also a problem.. I never use them, but I think you can check in the Prefs, under plugins. As for the disappearing, do you have some sort of tidy up app, script or folder action running to put file in folders by kind.

The -t part is -t" & captureformat , no space after the t

Also Here is a slightly better script which will take care of any file names you use with spaces in them.

Look at the Event log, to see what the script is doing. You should see something like:
do shell script "screencapture -tpng '/Users/userName/Desktop/Safe 1.png'"


tell me to set thePath to (choose folder)
set thePath to (POSIX path of thePath)
set captureformat to "png"
set defAns to "." & captureformat as string
tell me to display dialog thePath default answer defAns buttons {"Cancel", "OK "} default button 2
copy the result as list to {text_returned, button_pressed}
tell me to (display dialog "set Delay" default answer 2 buttons {"Cancel", "OK "} default button 2)
copy the result as list to {text_returned2, button_pressed2}
set Delay_ to text_returned2 as number
delay Delay_
set apath to (POSIX path of thePath) & text_returned as string
set doit to do shell script ("screencapture -t" & captureformat & space & quoted form of apath as string) 
log ("screencapture -t" & captureformat & space & quoted form of apath as string) 
---
mh


[ Reply to This | # ]
10.6: Rename screenshots with Automator and AppleScript
Authored by: homeshire on Dec 10, '09 03:02:07AM

mh --

I am running no additions that I am aware of. I have no tidy up apps that move things automatically.

When I run your latest version of the script, the event log shows the following:

tell current application
choose folder
alias "Das Urall:Users:hmshr:Desktop:"
display dialog "/Users/hmshr/Desktop/" default answer ".png" buttons {"Cancel", "OK "} default button 2
{text returned:"thisisit", button returned:"OK "}
display dialog "set Delay" default answer 2 buttons {"Cancel", "OK "} default button 2
{text returned:"0", button returned:"OK "}
do shell script "screencapture -tpng '/Users/hmshr/Desktop/thisisit'"
""
(*screencapture -tpng '/Users/hmshr/Desktop/thisisit'*)
end tell

indeed, the file is still on the desktop. it is not a png or jpg or any kind of picture file. it has no extention whatever. I can manually apply an extension and it magically transforms into a picture file of exactly what it should be. but does not default to it.

if I do not change the title and leave the default .png in the dialog box of the script, it still goes poof after appearing momentarily on the desktop with the title .png followed by gibberish as some sort of data-like extension.

I'm flummoxed.

this is the event log after leaving the default title of .png in the dialog box:

tell current application
choose folder
alias "Das Urall:Users:hmshr:Desktop:"
display dialog "/Users/hmshr/Desktop/" default answer ".png" buttons {"Cancel", "OK "} default button 2
{text returned:".png", button returned:"OK "}
display dialog "set Delay" default answer 2 buttons {"Cancel", "OK "} default button 2
{text returned:"0", button returned:"OK "}
do shell script "screencapture -tpng '/Users/hmshr/Desktop/.png'"
""
(*screencapture -tpng '/Users/hmshr/Desktop/.png'*)
end tell

I wonder what's going on?


p.s. mh --

look at the following script as I have modified:

tell me to set thePath to (choose folder)
set thePath to (POSIX path of thePath)
set captureformat to "png"
set defAns to text returned of (display dialog "Title ...?" default answer "") & "." & captureformat as string
tell me to display dialog thePath default answer defAns buttons {"Cancel", "OK "} default button 2
copy the result as list to {text_returned, button_pressed}
tell me to (display dialog "set Delay" default answer 2 buttons {"Cancel", "OK "} default button 2)
copy the result as list to {text_returned2, button_pressed2}
set Delay_ to text_returned2 as number
delay Delay_
set apath to (POSIX path of thePath) & text_returned as string
set doit to do shell script ("screencapture -t" & captureformat & space & quoted form of apath as string)
log ("screencapture -t" & captureformat & space & quoted form of apath as string)

specifically I altered the definition of the variable defAns. this script works as it should. I get the pic file I want on the desktop.

your opinion?




Edited on Dec 10, '09 03:08:24AM by homeshire



[ Reply to This | # ]
10.6: Rename screenshots with Automator and AppleScript
Authored by: homeshire on Dec 10, '09 06:18:30AM

DOH!

I was on my morning walk mulling it all over. I was thinking about how each of these files was titled ..png -gibberish

I was suddenly thunderstruck by the realization that the leading . was hiding the files.

I revealed hidden files and folders, and there they were on my desktop (and probably other various places I haven't hunted down while playing around with various forms of the script).

I can't decide if I should feel proud or embarrassed.



[ Reply to This | # ]
10.6: Rename screenshots with Automator and AppleScript
Authored by: tangawk on Jan 24, '10 12:51:01AM

It's been a while since I posted this hint and just got back to it.

Glad to see it's generated some interest - especially from some hardcore scripters.

To explain why I wanted it to work this way: Whenever I'm about to go somewhere, I tend to grab several (maybe up to 10) screenshots relating to something. Perhaps maps of a particular route (no, I don't have an iPhone) or opening times and location maps of Eurodisney etc. So I end up with multiple grabs of essentially the same topic. I then want to rename all of these as a batch (eg. Eurodisney 1 - Eurodisney 10). I then dump these onto a Symbian phone via Bluetooth.

For the times where I"m grabbing images of a different subject, sure, the rename as you grab would be great. But most of the time, I find myself wanting to rename batches of files of a similar subject.

Adrian



[ Reply to This | # ]