Trimming a file name from the beginning

Hello everyone, I searched the forum before I posted this. I hope I’m not repeating the question and being annoying…

I’d like to create a script that trims 9 characters from the beginning of a file name and leaves the rest intact. For example, when I import my pictures I get a file named like this: IMG_0001 2005-07-17.jpg

What I want to do is remove the IMG_0001 part, including the space between the 1 and 2005, and just leave the date of the file, so I figured it would be 9 characters to remove, including the space.

I’d like to incorporate such a script into my Automator workflow, but I don’t know how to write it. If you could help me out, I’d be very grateful.

Thanks in advance! :slight_smile:

What are the names of the rest of the files? If they’re named like IMG_0001 2005-07-17.jpg, IMG_0002 2005-07-17.jpg, IMG_0003 2005-07-17.jpg, etc., then taking the last part of the names will result in each having the same name (2005-07-17.jpg). If so, you might prefer it if it takes the number from the first part and shoves it on the end of the name (e.g. 2005-07-17 1.jpg or 2005/07/17 - 1.jpg).

The format is actually Year-Month-Day_Hours-Minutes-Seconds.jpg, so the duplicates thing shouldn’t be an issue.

vr, Look at /Library/Scripts/Finder Scripts/Trim File Names.scpt

I don’t know how you will store these files, but if they end up in the same folder eventually you will get duplicate names (only so many h/m/s in a day!). I’ve done this exact thing before. Will you post a filename exactly as it appears so we can see what you are working with?
SC

Simply takes the text after the first space in the name and renames it. If there is no space, or the resulting name already exists, then it should leave the file name unchanged (hopefully).

set this_folder to (choose folder with prompt "Pick the folder containing the files to process:") as string
tell application "System Events"
	set these_files to every file of folder this_folder
end tell
repeat with i from 1 to the count of these_files
	set this_file to (item i of these_files as alias)
	set this_info to info for this_file
	if visible of this_info is true and alias of this_info is false then
		set this_name to name of this_info
		if this_name begins with "IMG_" then
			set this_offset to offset of space in this_name
			if this_offset is not 0 and this_offset is greater than the length of this_name then
				set this_name to text (this_offset + 1) thru -1 of this_name
				tell application "System Events"
					try
						set name of this_file to this_name
					on error error_message number error_number
						display dialog "Error: " & error_number & return & error_message
					end try
				end tell
			end if
		end if
	end if
end repeat

This is task specific, but it works:

choose folder with prompt "Choose the folder containing the pictures to rename:" with multiple selections allowed

try
	repeat with thisFolder in result
		tell application "Finder" to get (every file in folder thisFolder whose name extension is "jpg")
		
		repeat with thisItem in result
			set originalName to (name of (info for (thisItem as alias)))
			if originalName starts with "IMG_" then
				tell application "Finder" to set name of thisItem to (characters 10 through -1 of originalName) as text
			end if
		end repeat
	end repeat
on error errorMsg number errorNum
	display dialog "Error (" & errorNum & "):" & return & return & errorMsg ¬
		buttons "Cancel" default button 1 with icon caution
end try

Qwerty and guardian, thanks a lot. Those work very well.

Now I have another idea in mind. Again, I don’t know where to start. You don’t have to write the script for me, because I’d like to learn it myself.

I want to grab the EXIF data from each picture, and then rename the imported pictures according to the EXIF in the same format as I mentioned above: YYYY-MM-DD_HH-MM-SS. There IS a program called ExifRenamer for the Mac, but I’d just like to know how to do this myself.

Sitcom, I dont know how you get the idea that I’ll have duplicate file name problems. See, one picture can be shot at 2:30:01pm - the file name would be 2005-07-19_14-30-01.jpg. If the time is in the AM hours, the file name would be 2005-07-19_02-30-01.jpg. That’s the format that I want to use.

Back to the EXIF data idea, is it even possible to fetch and rename the files accoding to the data? I’d love to make my own little app that does this automatically.

If you can point me into the right direction, I’d be very grateful… also remember that I’m a newb to OS X as well as AppleScript.

Again, thanks to Qwerty and guardian!

Maybe it would be something like

try
	repeat with thisFolder in result
		tell application "Finder" to get (every file in folder thisFolder whose name extension is "jpg")
		repeat with thisItem in result
			set originalName to (name of (info for (thisItem as alias)))
			if originalName starts with "IMG_" then
				tell application "Finder" to set name of thisItem to (creation date) as text
			end if
		end repeat
	end repeat
end try

Can anyone give some input, please?

Hey vr,

in order to work with EXIF tags you need some function to fetch them from the image file. I suggest you have a look at the “Image Events” Scripting Addition (comes standard with AppleScript), it seems to provide access to the data via the “metadata tag” class… I’ve never used this, so I cannot give you further hints on how to use it, sorry…

Regards,
Daniel

Model: iBook (g3/800, 14,1")
AppleScript: 1.9.3
Browser: Safari 312
Operating System: Mac OS X (10.3.9)

Thanks Daniel,

I found something about that in the AppleScript Dictionary. The only problem is, I don’t know how to use that, heh. :slight_smile: I’m new to Macs, and AppleScript, too, of course.

Apologies for misreading the post, I didn’t see the date stamp and the time stamp. Reading the EXIF data is as easy as


set thefile to choose file
set theData to read thefile

You will get a message telling you the file is large and can only display the first 2000 characters…Not to worry, there’s a party at the moontower…

The EXIF data is right at the top of the data, which we can now break into chunks and get the Cdate. You said you want to write a script yourself, so to “parse” your data out you need to break it into chunks and then get your data so that it meets some condition:

That gave me “2005:02:12 14:13:35…2…” as Basefilname
so I still had to parse the 2 and replace the illegal characters : and " "
I did that inside a repeat:

And the final product renamed a photo to:
“2005-02-12_14-14-10.jpg”
So your “parsing” scheme may be different depending on if all jpg files are formatted the same way as far as creation date. Here is my finished script:


set thefile to choose file
set theData to read thefile

set DataBlocks to paragraphs of theData
set ThisData to text items of DataBlocks
repeat with ThisBlock in ThisData
	if ThisBlock contains "2005:" then
		set BaseFilename to ThisBlock as string
	end if
end repeat

set NewFilename to ""

set Char1 to characters of BaseFilename

repeat with thisChar in Char1
		
	if (thisChar as string) is ":" then set thisChar to "-"
	if (thisChar as string) is " " then set thisChar to "_"
	
	set NewFilename to NewFilename & thisChar
	
	set FinishedParse to count NewFilename
	if FinishedParse is 19 then exit repeat
end repeat


tell application "Finder" to set the name of thefile to NewFilename & ".jpg" as string

SC

Sitcom,

Needless to say, I owe you big for this one. Thanks for the explanation, I think I understand this a lot better now.

If I understand correctly, your script searches for the data in the file that starts with “2005:”. I wonder if I could use a wildcard, because say if I took pictures in the future (in 2006, for example) the script wouldn’t find the “2005:” part because it would not be there. Or am I mistaken on that? I thought I could use something like

if ThisBlock contains "200*:**:**" then

Would that work? Maybe AppleScript uses different characters for wildcards, I don’t really know… It’s just an idea I had.

You got it right. The “if” statement is the essence of this script.
“if” sets up a condition, and you found out why “2005:” is a “weak” arguement.
"ThisBlock contains “200*::” This “phrase” is a value known as “Boolean”. The result window will return either “true” or “false” for this value.

How you decide to structure this statement will determine how good the script works. So what condition(s) are true or false about the block of text containing the Cdate? You just need what’s consistent in your files.

I realized one thing different about each data block was the number of characters in each block.

set CharCount to count ThisBlock

returns the number of characters in a block, and I found that the Cdate block had 35. I checked and all were different BEFORE it for sure. So I set up the condition

if CharCount is 35 then
set BaseFilename to ThisBlock as string
exit repeat

I added “exit” so that when it finds the block it stops searching. Now it is twice as fast becuase it only runs through 4 blocks. But I am not sure that every file will have the same number of characters in the block.


set thefile to choose file
set theData to read thefile

set DataBlocks to paragraphs of theData
set ThisData to text items of DataBlocks
repeat with ThisBlock in ThisData
	set CharCount to count ThisBlock
	if CharCount is 35 then
		set BaseFilename to ThisBlock as string
		exit repeat
	end if
end repeat

set NewFilename to ""

set Char1 to characters of BaseFilename

repeat with thisChar in Char1
	
	if (thisChar as string) is ":" then set thisChar to "-"
	if (thisChar as string) is " " then set thisChar to "_"
	
	set NewFilename to NewFilename & thisChar
	
	set FinishedParse to count NewFilename
	if FinishedParse is 19 then exit repeat
end repeat


tell application "Finder" to set the name of thefile to NewFilename & ".jpg" as string

an alternate condition:

This one seems more stable, as it returns the first datablock that contains 4 “:”


set thefile to choose file
set theData to read thefile

set DataBlocks to paragraphs of theData
set ThisData to text items of DataBlocks


repeat with ThisBlock in ThisData
	
	set BlockChars to text items of ThisBlock as string--break block into characters
	
	set ColonCount to 0
	repeat with TheChar in BlockChars
		if TheChar contains ":" then set ColonCount to (ColonCount + 1)--Add 1 for each ":"
	end repeat
	
	if ColonCount is 4 then--Snags the first block with 4 ":"
		set BaseFilename to ThisBlock
		exit repeat
	end if
	
end repeat

set NewFilename to ""

set Char1 to characters of BaseFilename

repeat with thisChar in Char1
	
	if (thisChar as string) is ":" then set thisChar to "-"
	if (thisChar as string) is " " then set thisChar to "_"
	
	set NewFilename to NewFilename & thisChar
	
	set FinishedParse to count NewFilename
	if FinishedParse is 19 then exit repeat
end repeat



tell application "Finder" to set the name of thefile to NewFilename & ".jpg" as string

SC

Post back if you need help turning this into a batch processing script, droplet, etc.