File renaming for Spotlight searches

Spotlight under 10.4 makes searching by filename really easy. However for images and movies it appears to rely only filenames. So I’m trying to write a script to append my file names with part of the file pathname. I can do a wee bit of scripting (mostly adjusting others’ work) but this is beyond me. Can you help?

Basically I need write a script to rename files in the following manner.

Keeps the original file name - say “dsc001.jpg”

Adds onto the filename part, additional data that is taken from the enclosing folder - so a file with the path

pictures/landscape/urban/london/nightscene/dsc001.jpg

is renamed to

dsc001-landscape-urban-london-nightscene.jpg

The “-” separator can be a space but bear in mind that many of my files already use the “-” in their existing filenames

I’m also using other file formats so I need to maintian the extension and not just “add” .jpg to the end of the filename.

If I can adjust how much of the path it adds (3,4,5 directory names etc) that would be useful.

Hopefully there is already a script out there, if so please let me know. Else can you help me with part or all of the scripting. ( i’m having particulare difficulty with the renaming and keeping the extension intact.

Lastly but not least. I’ve got thousands of files and so a folder action is not really going to help me (i think) Will it be possible to have a script that will just run until it’s been through all the directories in my pictures fiolder?

Hope I’ve been clear enough and you can help me out,

Regards Ray

PS. When people ask me why is the Mac so much better, I tell 'em it’s because other mac users appreciate the smart choice they made and so want to try and help them along.

Model: mac mini
Browser: Internet Explorer 5.23
Operating System: Mac OS X (10.4)

This is no problem, as I’m sure you know… Filename manipulation is one of the most common Applescript functions, done with a hundred variations. I’ll get this one out to you, unless someone beats me to it :stuck_out_tongue: …Check in around 5pm central.
SC

Would you prefer a droplet, where you drop the folder to process, or a “choose folder” prompt where you select the folder for processing?

Thanks for replying, Yes I’ve seen loads of scripts and even tried to write a few myself but although I can do some parts of what I need I can’t seem to get the whole thing together, esp the filename extension issue. I think for robustness maybe a dialog box type folder selection would be best. I’m not sure how the script would handle having a directory containing several; thousand files in hundr5eds of folders dropped onto it!

Thanks again, Ray

I’m almost done and here’s a hint for you:


I need I can't seem to get the whole thing together, esp the filename extension issue.

Is answered with:

There, you just solved the filename extension issue!
SC

There are a couple of ways to get the file extension, and since your keeping the filename I used “parsing”. Set applescript’s delimiter to a period “.”
In “dsc001.jpg”, text item 1 will be the string “dsc001” and text item 2 will be “jpg”, and so on for gif, tiff, whatever. Then you assemble the pieces in the renaming handler.
name + paths + . + extension

On to the goods:


set MasterFolder to choose folder with prompt "Choose a folder for filename processing" --Select folder
property UI : 3 --user interface constant.

set PathsInName to {"3", "4", "5"}
choose from list PathsInName with prompt "How many paths in these filenames?" --dialog to set filename structure

try
	set UI to result as string
end try




tell application "Finder"
	--Reference to a folder
	set FolderRef to MasterFolder as string
	--Gets contents of folder ready for processing
	set FolderContents to items of folder FolderRef
end tell

repeat with TheItem in FolderContents --Begins processing master folder
	
	
	tell application "Finder"
		set theINFO to info for file (TheItem as string) --Gets the info where the folder record is
	end tell
	if folder of theINFO is true then
		process_folder(TheItem) --Folders go to process folder handler
	else
		if folder of theINFO is false then process_item(TheItem) --Files go to process item handler
		
	end if
	
end repeat



-- this sub-routine processes folders in folders
on process_folder(this_folder)
	tell application "Finder"
		--Reference to a folder
		set FolderRef2 to this_folder as string
		--Gets contents of folder ready for processing
		set FolderContents2 to items of folder FolderRef2
	end tell
	
	repeat with TheItem in FolderContents2
		tell application "Finder"
			set theINFO to info for file (TheItem as string)
		end tell
		if folder of theINFO is true then
			process_folder(TheItem) --If a folder is found it is sent back to this handler
		else
			if folder of theINFO is false then process_item(TheItem) --This filters out files for processing
			
		end if
	end repeat
	
end process_folder

-- this sub-routine processes files
on process_item(this_item)
	set Namo to name of this_item as string --To solve your file extenstion problem, get the filename
	set AppleScript's text item delimiters to "."
	set TheName to text item 1 of Namo as string --Get the filename w/o fileext
	set FileExt to text item 2 of Namo as string --Get fileext
	set AppleScript's text item delimiters to ""
	
	
	set thePath to this_item as string
	set AppleScript's text item delimiters to ":"
	set DirNum to count text items of thePath --Finds out how many folder names are in the path. Each one becomes a numbered item, where your hard drive is 1 and the file is last
	
	--Sets up the cases for naming the files. DirNum - x is each folder before the file.
	try
		set Path3 to TheName & "-" & (text item (DirNum - 3) of thePath) & "-" & (text item (DirNum - 2) of thePath) & "-" & (text item (DirNum - 1) of thePath) & "." & FileExt
		set Path4 to TheName & "-" & (text item (DirNum - 4) of thePath) & "-" & (text item (DirNum - 3) of thePath) & "-" & (text item (DirNum - 2) of thePath) & "-" & (text item (DirNum - 1) of thePath) & "." & FileExt
		set Path5 to TheName & "-" & (text item (DirNum - 5) of thePath) & "-" & (text item (DirNum - 4) of thePath) & "-" & (text item (DirNum - 3) of thePath) & "-" & (text item (DirNum - 2) of thePath) & "-" & (text item (DirNum - 1) of thePath) & "." & FileExt
		set AppleScript's text item delimiters to ""
	end try
	
	
	if UI is "3" then --Gets the case you chose for naming
		set name of this_item to Path3 --Rename statements
	else
		if UI is "4" then set name of this_item to Path4
		
		if UI is "5" then set name of this_item to Path5
	end if
	
end process_item --Fin

First off, save as an application and get a few testfolders. I nested 4 folders deep and filled each one with pics for testing. I debugged it as I was going, and it works without error on my machine. If you get any errors in testing, post back. This is a very common routine so you have a pretty good shot at dialing this in exactly how you want it. Also, your chances of someone seeing this and presenting a consolidated version are pretty damned good.

SC

Yes it all works as you stated - first time without any problems. thank you very much for such a great job! A couple of bits of your code look familiar (as I’ve tried to resolve this for myself), so as well as solving the problem, your nicely designed code (recognisable variable names, comments etc.) will help me work out how to do things for myself in the future. thanks…

…howver one last little point… I did forget to mention this in my first post… Many of the files that i have to process with this script are from sys9 and as such do not have a file name extrention. When the script comes across one of these (and there are many) it produces an error that “it can find the second item”.

I actually experienced this error when trying out a few bit of my own code. Is there a simple IF statement or other bit of code you can suggest I use to get around this problem by either:

1- Still rename the file but leave the extension matter unchanged

2- Sense the file type and add the appropriate extension

I suppose that’s the thing about programming - it just never ends!

Thanks again for your great help, it is very much appreciated, if you can help me out with this last point it would save me eons of time, but youv’ve been very generous already and I do appreciate the effort.

Regards, Ray

Yes sir, every condition requires a little bit of code, so the more “limits” you define … the more you will find yourself in a Script Editor window :lol:

You can access a file’s name extension with the “info for”, but that is not always reliable (that comes from a very reliable source :rolleyes: )

So let’s leave well enough alone, and put in a small error handler:
Find this part of the code


-- this sub-routine processes files
on process_item(this_item)
	set Namo to name of this_item as string --To solve your file extenstion problem, get the filename
	set AppleScript's text item delimiters to "."
	set TheName to text item 1 of Namo as string --Get the filename w/o fileext
	try--Sets up a 'do this if you can'
		set FileExt to text item 2 of Namo as string --Get fileext
	on error--Sets up a "If you can't do case 1 then try this" 
		set FileExt to ""--The variable FileExt will be used later so it needs a value. Here it is a blank space.
	end try--One of the two conditions will be met, because you can always set a variable to ""
	set AppleScript's text item delimiters to ""

That keeps her running smoove!
So if you have a file “dsc001” it will be renamed to “dsc001-FolderA-FolderB-FolderC.”
Notice the period at the end. That will come from the naming statement:

set Path5 to TheName & … (text item (DirNum - 1) of thePath) & “.” & FileExt

When the file reaches this point, you see we told it to end the filename with a period and FileExt (in this case a blank space), so you will get “.” at the end. You could set up a condition for the PathX variable so that

if FileExt is “” then set Path5 to TheName & … (text item (DirNum - 1) of thePath)
else
if FileExt is not “” then set Path5 to TheName & … (text item (DirNum - 1) of thePath) & “.” & FileExt

Which you would have to do for each PathX definition. I’d go with the period at the end.
Post back, let us know how things go.
SC

Hi again SC

Yes, again all works perfectly and precisely as you described. I can appreciate the simple elegance of your code. I very much apreciate your efforts in helping me out on this, so a big thank you!

The apparent (I say “apparent” as I know deft programming skills are hard won) simplicity and ease with which you resolved this for me brings to mind a now slightly ageing programming issue that I have to resolve - and have in the past briefly thought of using AppleScript.

A couple of years ago I programmed (I use the term loosley) scripts in Macomedia Director (Lingo) that messes about with QT video and audio (8 channels). Now I either have to update this work for OSX or look for another programming method. Maybe it can be done in AppleScript?

The matter involves progrtamming active control of quicktime movies during their playback. Can AppleScript be used to change the playback frame rate (speed up, slow down) and move the playback head to given points in the movie (sort of jump cut editing). Oh yes and can it all be done on the fly at 25 frames per second?

Just thought youmight ber able to shed some light on this.

Any thoughts ideas and links to illustrative bits of Applescript would be most appreciated.

Again my warmest thanks for your help, Ray

Glad to help :slight_smile:
You need to write out a set of conditions exactly as they would happen, so you can begin to attempt finding code to do those conditions.

Example:

Run script
Play movie at normal rate
Choose from list dialog floating in background
-LIst options->
*speed up playback frame rate
*slow down playback frame rate
*move the playback head forward 5 seconds
*move the playback head backward 5 seconds
Choice is executed and floating dialog returns for next command
End run

That way you define what your program does in action, and you can create code to meet each item.

When asked about making a part for a machine 2000 times before finding the correct one, ie “Don’t you feel like your wasting your time?”
The inventor’s reply: “No, I’ve just learned 1999 ways how NOT to make that part”.
SC

Ok, here’s what I’ve got… Save the following as an application that does not stay open. Call it Custom Controller or something cause that’s about what it is.
Open a movie in Quicktime. Then, click the application you saved. The movie is reset to 360 X 240, half size for ntsc DV. You can change that in the code easily. Then it is moved to the center of the screen under a prompt that says “Custom Controls”. You can play, slow play and fast play all on the fly at 29fps. The “forward 5 sec” and “back 5 sec” do have a small lag time. The code is basic so you can easily see where to change varible settings.


tell application "QuickTime Player"
	set position of window 1 to {218, 260}--Play with these numbers and watch what happens
	set dimensions of movie 1 to {360, 240}--Here you can adjust the size
end tell

repeat
	MyController()
	
	set button_name to result as string
	if button_name is "Speed Up" then
		tell application "QuickTime Player"
			set rate of movie 1 to 1.5
		end tell
	else
		
		if button_name is "Slow Down" then tell application "QuickTime Player"
			set rate of movie 1 to 0.5
		end tell
		
		if button_name is "Play Normal" then tell application "QuickTime Player"
			set rate of movie 1 to 1
		end tell
		
		if button_name is "Forward 5 sec" then tell application "QuickTime Player"
			
			step forward movie 1 by 150 --About 5 seconds
			play movie 1
		end tell
		
		if button_name is "Back 5 sec" then tell application "QuickTime Player"
			
			step backward movie 1 by 150 --About 5 seconds
			play movie 1
		end tell
		
		if button_name is "False" then exit repeat
	end if
	
end repeat

on MyController()
	with timeout of 6000 seconds --Controller will stay open for 100 minutes on each loop
		set CustomController to {"Speed Up", "Play Normal", "Slow Down", "Forward 5 sec", "Back 5 sec"}
		set UI to choose from list CustomController with prompt "*Quicktime Custom Controls*"
	end timeout
end MyController

SC