Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

sOid

macrumors member
Original poster
Aug 8, 2009
52
0
Installed SL yesterday, and started to customizing my installation today.

I've got a script that automatically changes the names of screenshots I take, but it doesn't work any more since SL because SL uses way too long and strange screenshot names.

In Leopard the names of my screenshots were "Afbeelding 1" (Dutch for Image 1) and my script changed it to "1_screenshot".

However, when I take a screenshot in Snow Leopard, the default name is "Schermafbeelding 2009-08-30 om 11.03.04" (translated: Screenimage 2009-08-30 at 11.03.04).

I hate long filenames, and I want to change them. I just don't know how.

I tried to change my script, but to no avail...

Code:
--Written by MacTipper for The MacTipper Blog: http://mactipper.com/
--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/Jeroen/Pictures/Screenshots/" --Posix path to your screenshots folder. You can change the folder with this tip: http://tinyurl.com/changescreenshots
property default_extension : ".jpg" --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 "Schermafbeelding") 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

Can anyone help? I'm sure I'm not the only one who finds this frustrating...
 
Judging by the release notes for the new Applescript, it looks like the script makes a fairly common "mistake." That is, it has non Finder commands inside the tell block. Namely, the lines containing "path to ..."

From the release notes:

It is generally considered bad AppleScript form to unnecessarily place commands within tell blocks, because:

* Terminology conflicts may arise between application terminology and the command you intended to use. An application may change in the future to support a new term whose name or event code is the same as a scripting addition (or vice-versa) and change the meaning of the script. (For example, both Standard Additions and Finder define “startup disk”, but one is an enumerated value and the other is a property, which can cause confusing behaviors.)
* Sending events to other applications takes longer and may make scripts unnecessarily slower.
* It makes some scripts more difficult to read and understand, because it isn't always clear to the reader which commands are intended to be sent to the application and which are only incidentally being sent because they happen to be within a tell block.

Unfortunately, many scripts may happen to work when written with excessive tell blocks, so script writers may unintentionally leave code within tell blocks. Generally, you should try to avoid using tell blocks and instead use tell statements “tell application "Foo" to …” to limit the scope of tells. Try to be aware of which commands and other terms are actually a part of an application’s terminology and which are scripting addition commands, terms defined by your script or terms of the application running the script, and try to keep the latter ones out of application tell blocks.

What I'd suggest is rewrite the code with fewer tell blocks. It's probably going to be frustrating because I'm pretty sure some of your file commands will definitely need to be inside a tell block, and some apparently don't. You're in for some trial and error.

mt
 
Judging by the release notes for the new Applescript, it looks like the script makes a fairly common "mistake." That is, it has non Finder commands inside the tell block. Namely, the lines containing "path to ..."

From the release notes:



What I'd suggest is rewrite the code with fewer tell blocks. It's probably going to be frustrating because I'm pretty sure some of your file commands will definitely need to be inside a tell block, and some apparently don't. You're in for some trial and error.

mt

Thanks a lot! I was hoping it would be easier though... I'm not that great in scripting myself.

I'll contact the original author to see if he has written a new script for SL.
 
Try this. I don't have SL installed yet.

Code:
-- outside the tell app finder block

set pathToDesktop to path to desktop folder
set pathToDesktopStr to pathToDesktop as string
set theComment to ""

on whichName(pathToDesktopStr)
	set num to 0
	tell application "Finder"
		
		repeat
			set fullPath to pathToDesktopStr & (num as string) & "_screenshot.png"
			if file fullPath exists then
				set num to num + 1
			else
				exit repeat
				
			end if
		end repeat
	end tell
	return (num as string) & "_screenshot.png"
	
end whichName



tell application "Finder"
	
	set pictureList to items of pathToDesktop whose name begins with "Schermafbeelding"
	if pictureList ≠ {} then
		repeat with phyle in pictureList
			try
				set theComment to (get comment of phyle)
			on error errMsg number errNum
				display dialog ("Error: " & errNum as string) & return & errMsg
			end try
			
			if theComment does not contain "managed" then
				try
					set the comment of phyle to theComment & " managed"
				on error errMsg number errNum
					display dialog ("Error: " & errNum as string) & return & errMsg
				end try
				
				
				set newName to my whichName(pathToDesktopStr)
				try
					set name of phyle to newName
				on error errMsg number errNum
					display dialog ("Error: " & errNum as string) & return & errMsg
				end try
			end if
		end repeat
	end if
		
end tell

mt
 
Try this. I don't have SL installed yet.


mt

Wow, that's some service. Thanks! I've tried it, and it works (although I don't store my screenshots on the Desktop, but that's a minor change). However, it is a LOT slower than the previous script. I'm not sure why.

It takes about a minute to change one filename, while the other script was nearly instant (max 10 secs).

Any suggestions of why this is happening?
 
It takes about a minute to change one filename, while the other script was nearly instant (max 10 secs).

Any suggestions of why this is happening?

One minute?!?!?!

Wow, it's near instantaneous on my machine (Intel mini).

You could try eliminating the try blocks. I like to use them as often as possible because it helps debug when things go wrong. But if the script is functional on your machine, then there a low likelihood of errors.

Possibly there's overhead with the "on whichname" function.

You could try changing

set newName to my whichName(pathToDesktopStr)

to:

set newName to "1_screenshot"

In Leopard, if a script tries to rename a file that already has that name, it doesn't cause much havok. It just doesn't rename the file. Don't know what will happen on SL -- so use expendable data -- but it might help show whether it's calling the function that eats up cycles.

BTW, you needed a third-party app to move the screenshots, right? Wouldn't that software also let you use an alternative naming convention?

And if my script remains so slow, you might try using Automator.

mt
 
Just use Automator, you can literally do it with 2 actions:

01screenshot.png

(image)

Just make sure you create it as a Folder Action.
 
Thanks guys. I've actually tried to do it with Automator too (a couple of months ago) but didn't succeed.

Bedtime now but I'll go try it out tomorrow. I'll let you know if it worked!
 
I retested my script on Snow Leopard. It renamed eight files in about 1 second. Not sure what your issue is.

Code:
set startTime to the current date

set pathToDesktop to path to desktop folder
set pathToDesktopStr to pathToDesktop as string
set theComment to ""

on whichName(pathToDesktopStr)
	set num to 0
	tell application "Finder"
		
		repeat
			set fullPath to pathToDesktopStr & (num as string) & "_screenshot.png"
			if file fullPath exists then
				set num to num + 1
			else
				exit repeat
				
			end if
		end repeat
	end tell
	return (num as string) & "_screenshot.png"
	
end whichName



tell application "Finder"
	
	set pictureList to items of pathToDesktop whose name begins with "Schermafbeelding"
	if pictureList ≠ {} then
		repeat with phyle in pictureList
			try
				set theComment to (get comment of phyle)
			on error errMsg number errNum
				display dialog ("Error: " & errNum as string) & return & errMsg
			end try
			
			if theComment does not contain "managed" then
				try
					set the comment of phyle to theComment & " managed"
				on error errMsg number errNum
					display dialog ("Error: " & errNum as string) & return & errMsg
				end try
				
				
				set newName to my whichName(pathToDesktopStr)
				try
					set name of phyle to newName
				on error errMsg number errNum
					display dialog ("Error: " & errNum as string) & return & errMsg
				end try
			end if
		end repeat
	end if
	
	
end tell

display dialog ("Time: " & (the (current date) - startTime) as string) & return & "Number of items: " & (count of pictureList)

mt
 
I retested my script on Snow Leopard. It renamed eight files in about 1 second. Not sure what your issue is.


mt

Sorry for my late response; been busy the last couple of days.

Anyway, I think the problem isn't the script, nor my Mac. The problem is probably Snow Leopard. Somehow, everything seems to work slow. Finder crashes quite often, as does iTunes. Search in Mail doesn't work, and more of that kind of stuff.

So I think I'll just have to re-install SL and try it again.

Thanks for the confirmation that the script works flawless!
 
Well, I've done a clean install of Snow Leopard again. I don't understand why, but everything works a lot better and faster now, including the script.

However, I don't store my screenshots on the desktop, but in /Users/Jeroen/Pictures/Screenshots

I've tried to edit your script, mysterytramp, but didn't succeed. Any chance you can give me some pointers about this? Would be greatly appreciated!

Ps. I've tried the automator workflow thing stated above too, which only works for when the folder is empty.

If the folder contains, for example, these files: 01_screenshot.png and 02_screenshot.png, it doesn't rename the next file to 03_screenshot.png but wants to rename it as 01_screenshot.png, thus resulting in an error.
 
However, I don't store my screenshots on the desktop, but in /Users/Jeroen/Pictures/Screenshots

You're going to have to use an HFS style folder path (use colons) instead of Posix (use slashes).

The easiest way would be to change the two variables at the start of the script. Instead of:

Code:
set pathToDesktop to path to desktop folder
set pathToDesktopStr to pathToDesktop as string

Do this instead:

Code:
set pathToDesktop to alias "Macintosh HD:Users:Jeroen:Pictures:Screenshots:"

set pathToDesktopStr to "Macintosh HD:Users:Jeroen:Pictures:Screenshots:"

And this assumes your hard drive is named "Macintosh HD." If it's something else, use that name.

mt
 
You're going to have to use an HFS style folder path (use colons) instead of Posix (use slashes).

The easiest way would be to change the two variables at the start of the script. Instead of:

Code:
set pathToDesktop to path to desktop folder
set pathToDesktopStr to pathToDesktop as string

Do this instead:

Code:
set pathToDesktop to alias "Macintosh HD:Users:Jeroen:Pictures:Screenshots:"

set pathToDesktopStr to "Macintosh HD:Users:Jeroen:Pictures:Screenshots:"

And this assumes your hard drive is named "Macintosh HD." If it's something else, use that name.

mt

Thanks man, it works great! The script gets triggered by a Folder automator thingy. Work amazing together.

Problem solved, thanks again :)
 
Just use Automator, you can literally do it with 2 actions:

01screenshot.png

(image)

Just make sure you create it as a Folder Action.



Where is that thing? I can't find where to do that?

I found this in the hopes of changing the way the computer names the files to begin with, not change the names after the fact.

Thanks!
 
It's in the Applications folder in Leopard, or use Spotlight to find it (Automator).

I'd be very careful before renaming files installed by OSX.
 
Ok, I found it, but I can't find "Make Finder Item Names Sequential"

If I get this right, will this change the way the computer names the files straight away, or is this just another way to rename the files afterwords?
 
I apologize for the necro, but this just helped me solve this exact problem.

The action is named Rename Finder Items, and the name changes in the workflow when you set its mode.

screenshot-0003.PNG


Thanks for this - the timestamps were bugging the hell out of me and many of the other remedies on the web didn't address it or didn't work in Lion.
 
So that's still just a way to rename the files after the Finder has created them.

I'm still looking for a way to change the way that the Finder names the screenshot files as they're being created. So instead of it being: "Screen shot 2012-05-17 at 6.35.27 AM.png" it would just be "01.png" and so on.

I guess there's just no such thing :(
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.