Annoying names of screenshots

Discussion in 'macOS' started by sOid, Aug 30, 2009.

  1. sOid macrumors member

    Joined:
    Aug 8, 2009
    #1
    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...
     
  2. mysterytramp macrumors 65816

    mysterytramp

    Joined:
    Jul 17, 2008
    Location:
    Maryland
    #2
    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
     
  3. sOid thread starter macrumors member

    Joined:
    Aug 8, 2009
    #3
    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.
     
  4. mysterytramp macrumors 65816

    mysterytramp

    Joined:
    Jul 17, 2008
    Location:
    Maryland
    #4
    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
     
  5. sOid thread starter macrumors member

    Joined:
    Aug 8, 2009
    #5
    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?
     
  6. mysterytramp macrumors 65816

    mysterytramp

    Joined:
    Jul 17, 2008
    Location:
    Maryland
    #6
    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
     
  7. rorschach macrumors 68020

    rorschach

    Joined:
    Jul 27, 2003
    #7
    Just use Automator, you can literally do it with 2 actions:

    [​IMG]
    (image)

    Just make sure you create it as a Folder Action.
     
  8. sOid thread starter macrumors member

    Joined:
    Aug 8, 2009
    #8
    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!
     
  9. mysterytramp macrumors 65816

    mysterytramp

    Joined:
    Jul 17, 2008
    Location:
    Maryland
    #9
    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
     
  10. sOid thread starter macrumors member

    Joined:
    Aug 8, 2009
    #10
    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!
     
  11. sOid thread starter macrumors member

    Joined:
    Aug 8, 2009
    #11
    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.
     
  12. mysterytramp macrumors 65816

    mysterytramp

    Joined:
    Jul 17, 2008
    Location:
    Maryland
    #12
    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
     
  13. sOid thread starter macrumors member

    Joined:
    Aug 8, 2009
    #13
    Thanks man, it works great! The script gets triggered by a Folder automator thingy. Work amazing together.

    Problem solved, thanks again :)
     
  14. btracy macrumors newbie

    Joined:
    Oct 21, 2009
    #14


    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!
     
  15. AdeFowler macrumors 68020

    AdeFowler

    Joined:
    Aug 27, 2004
    Location:
    England
    #15
    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.
     
  16. btracy macrumors newbie

    Joined:
    Oct 21, 2009
    #16
    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?
     
  17. bravelyranaway macrumors newbie

    Joined:
    Jan 31, 2010
    #17
    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.

    [​IMG]

    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.
     
  18. btracy macrumors newbie

    Joined:
    Oct 21, 2009
    #18
    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 :(
     

Share This Page