rename file based on folder

I’m trying to script a filename change to one based on the folder that contains the actual file. Hear me out:

Every iMovie Project has a cache folder (Show Package Contents) which in turn contains a Timeline.mov file. This is simply a reference video that is hidden within the project. I’ve set up a Folder Action that runs this Timeline file thru a Compressor Droplet to convert the video. That works fine. The only problem is that every project’s ref movie is called Timeline. I’d like to set it so that when the file is dropped in the watch folder, the name is first changed to the name of the actual project before it goes to Compressor. Here’s an example:

/Volumes/SULU 500/Movies/TiVo Pulls/CBS Evening News.iMovieProject/Cache/Timeline Movie.mov

If I let it run as, I would end up with a file called Timeline Movie.wmv whereas I would rather it be called CBS Evening News and the date.wmv

Am I making sense? I’ve seen some posts that ae kind of doing what I want, but not quite…

Thoughts?

Thanks.

You’re making sense, but have you tried renaming it manually to assure yourself that a renamed entry still works?

huh, hadn’t even thought about that… i feel a touch sheepish. okay, so plan b would simply be to change the resulting wmv file based on the containing folder. is there a way to grab just a part of that folder –
/Volumes/SULU 500/Movies/TiVo Pulls/CBS Evening News.iMovieProject/Cache/Timeline Movie.mov–

(the CBS News part), set it as a variable, and then rename the resulting file based on the variable?

Every file will have the same folder structure, so I’d want the 5th bit of text to become the new file name.

okay, i was able to get the part of text i wanted using this:


set {ASTID, text item delimiters} to {text item delimiters, ":"}
set theFile to choose file with prompt "select file" as string
set filePath to theFile as Unicode text
say -2th text item of filePath

I was just using the “say” command so I could test if I was getting the right text item. However, what I realized is that when the file is dropped on the watch folder, it no longer has the path that contains the iMovie project name. Duh, its a totally new file!
However, opening that copied file in quicktime and showing movie properties reveals that name is still there as the source for the first of the resources.

If this were all happening on the same computer, I could simply ask the user what they wanted to call the resulting file. However, the iMovie project is on one computer and we are dropping the Ref movie onto the watch folder on a computer with more power to handle the compression. Unless there’s a way to send dialogue to a networked computer? Just thinking out loud…

These should help you…

This one will rename something

on set_item_name(this_item, new_item_name) -- Note: this_item is a path to the file where new_item_name is just the new name
	set this_item to this_item as string
	tell application "Finder"
		set parent_container_path to (container of item this_item) as text
		if not (exists item (parent_container_path & new_item_name)) then
			try
				set the name of item 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
				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
			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

This one will make the current date into a format that can be used in a file name:

on currentdate_for_filename()
	set a to current date
	set b to a as «class isot» as string
	set {TIDs, text item delimiters} to {text item delimiters, "-"}
	set c to text items of b
	set text item delimiters to ""
	set c to c as string
	set text item delimiters to ":"
	set d to text items of c
	set text item delimiters to ""
	set d to d as string
	set e to text items 3 thru -1 of d as string
	set text item delimiters to TIDs
	return e
end currentdate_for_filename

This will open the movie reference file and give you the folder name you’re looking for:

set a to choose file
set folder_name to my get_folderName(a)

on get_folderName(movie_path)
	tell application "QuickTime Player"
		open movie_path
		set a to path of movie 1
		close movie 1 saving no
	end tell
	set {TIDs, text item delimiters} to {text item delimiters, "/"}
	set b to text items of a
	set text item delimiters to TIDs
	return item -3 of b
end get_folderName

thanks!

so am i supposed to run them from the script menu or as a folder action? i can’t seem to get anything to happen…

They’re called subroutines or handlers. Basically you feed information into them and get information out of them. For example, to use “on set_item_name(this_item, new_item_name)”. The items in the parenthesis are information you feed into the subroutine and, as the name implies, it will change the file name you feed into it. Suppose in your script you had a file called “test.txt” on your desktop and you wanted to rename it to “testing.txt”. Here’s how you would do it with the subroutine. Create a TextEdit document on your desktop called “test.txt” and run this script. It will rename it to “testing.txt”. Note how in my script I have variables for the path to the original file and the new name of the file. I just put them in the parenthesis in the right order and the subroutine does all the work.

This line in the following script is how you call the subroutine and insert your variables:
my set_item_name(the_file_path, new_name)

set desktop_folder to path to desktop as string
set the_file_path to desktop_folder & "test.txt"
set new_name to "testing.txt"

my set_item_name(the_file_path, new_name)

on set_item_name(this_item, new_item_name) -- Note: this_item is a path to the file where new_item_name is just the new name
	set this_item to this_item as string
	tell application "Finder"
		set parent_container_path to (container of item this_item) as text
		if not (exists item (parent_container_path & new_item_name)) then
			try
				set the name of item 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
				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
			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

Other subroutines don’t need an input such as the date one as evidenced by the “()” in the subroutine name. This subroutine just gives an output so you would call the subroutine with this line in a script and set a variable to the output of the subroutine.

set fileNameDate to my currentdate_for_filename()

If you don’t understand subroutines you’ll definitely want to because they’re a basic form of applescripting, so search this site for tutorials on how to work with them.

Here’s a 3-part tutorial on handlers (aka subroutines). There’s others too so search around if you need more information.

http://macscripter.net/articles/417_0_10_0_C/
http://macscripter.net/articles/427_0_10_0_C/
http://macscripter.net/articles/433_0_10_0_C/

awesome! thanks very much. this will give me some light reading for tomorrow morning. :stuck_out_tongue: