Go Back   The macosxhints Forums > OS X Help Requests > AppleScript

Thread Tools Rating: Thread Rating: 2 votes, 5.00 average. Display Modes
Old 02-25-2012, 03:46 PM   #1
Join Date: Feb 2012
Location: Pleasant Hill, CA
Posts: 3
re-arrange file names

Total newbie to Applescript. I screwed up in naming about 600+ files and now wish to try and correct it.

My format: This is a movie file m_xxx_xxx.mov

Desired format: m_xxx_xxx.mov This is a movie file

xxx could be several not just three.

Trying to sort these files presents 'unusual' results as does looking for dupes.

I have done this manually and the selected file plays so the the location of the .mov in the file name does not seem to be a problem.
I have tried recording Applescript editor (selected: volume, directory, file, cut m_xxx_xxx.mov, pasted to start of line and saved) but it only works once. Assume I need a marker to tell the script to: use any file you find here and do this to it. But, of course I have no idea what that marker/code is.
I also trawled the web for cut and paste file changer programs but none include an ability to do what I want.

As I said I have a whole bunch of these files ANY help would be much appreciated.

Thanks in anticipation
mortond is offline   Reply With Quote
Old 02-25-2012, 07:07 PM   #2
Join Date: Apr 2008
Location: Berkeley CA USA
Posts: 1,191
You haven't tested enough. The position of the filename extension (".mov" in this case) does matter. If it's not at the end, the movie won't begin playing with a double-click, things that can be told to search for movie files (Spotlight, for example) won't find them, and things that group files by kind (Finder, for example) won't group them as movie files.

Might I suggest going to the format: m_xxx_xxx This is a movie file.mov

That keeps the .mov at the end, where it needs to be, but brings the xxx_xxx part up near the front, to facilitate sorting. (I assume the "m_" prefix is literal, the xxx_xxx part does not contain any spaces, and the "This is a movie file" part is a random comment distinguished only by being separated from the rest of the filename by a space. If I've read your mind incorrectly, I apologize. I'm still not very good at this telepathy stuff.)

The hard part is the tedious task of finding the files you want to maybe rename. Automator might be a good way to do that. Get a list of files by whatever method you find convenient, and pass it to a "Run AppleScript" action that looks like:
on run {input, parameters}
	set text item delimiters to " "
	repeat with theFile in input
		tell application "Finder" to set oldname to name of theFile
		set base to last text item of oldname
		if base begins with "m_" and base ends with ".mov" then
			set newname to (text 1 thru -5 of base) & " " & (text 1 thru (-2 - (length of base)) of oldname) & ".mov"
				tell application "Finder" to set name of theFile to newname
			end try
		end if
	end repeat
	return input
end run
It's OK to pass extra files to the script. It'll ignore any files whose names are not in your "My format", and silently ignore (and leave unchanged) any files it can't rename (perhaps because of duplicates or of permissions).
ganbustein is offline   Reply With Quote
Old 02-26-2012, 12:32 PM   #3
Join Date: Feb 2012
Location: Pleasant Hill, CA
Posts: 3
re-arrange file names

Hi ganbustein,

Thank you for your prompt reply.

Your mind reading is correct!
The file parameters are as you state, all the files I wish to change are within one directory.
Your solution would work even better than mine, I have many files of the same subject but in .rm format with no "This is a movie" portion just m_xxx_xxx.rm. Using your method I could modify all the .mov files, copy the .rm files to that directory and dupes would become immediately apparent.

How to "pass it to a Run Applescript action" is something I don't know how to do.
I assumed (very dangerous) I highlight file names, click on the saved script name, it takes highlighted files and modifies them. It didn't!

I cut and pasted your script to Applescript Editor and saved it as "File name changer" in Applications as a Script.
Also, saved as an Application (with and without "run only" checked)
I highlighted the .mov files in their directory and tried opening with "File name changer" scripts nothing.

I tried copying the files into the script below the "on run" line; error
I tried replacing {input, parameters} with file names; error
If I just click the run button in the editor window it returns Applescript error (null)

Sorry to be a PITA, but as I said I am completely new to any form of programming.

How do I go to my .mov directory, highlight all the files and run your script that modify the titles?

Thank you

mortond is offline   Reply With Quote
Old 02-26-2012, 01:36 PM   #4
Join Date: Feb 2012
Location: Pleasant Hill, CA
Posts: 3
Re-arrange file names


I messed with Automator and using Workflow: to select folder, copied your script into "run Applescript" (had to edit a few duplications) pressed run, it asked for file selection, selected files, press return and it worked 'just like it said on the tin'

Saved me hours of work

Thank you,
mortond is offline   Reply With Quote

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

All times are GMT -5. The time now is 04:37 AM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.