How can I rename a file with a drag and drop process, using a list as my source data?

Joined
Oct 13, 2011
Messages
15
Reaction score
0
Points
1
Hi, I'm looking for a way to automatically rename files using a drag and drop process using OS X. My idea is this:

- I have several video files called, i.e.
10025.mov
10026.mov

Also, I always have a master list of each number and its description. The list is an Excel file that I am able to pull from a program pretty quickly, and it provides the number in the A column, and the description in the B column, like this:

10025 DOOR CLOSEUP 10S
10026 DRIVEWAY 101
10027 DRIVEWAY 102
10028 LICENSE PLATE
10029 GARAGE DOOR

What I want to do is drag files like 10025.mov onto this script/droplet, and it would rename it as "10025_door closeup 10s.mov"

My question is, does anyone know how to do this? The script would have to identify "10025" and find that particular row in the Excel document- then rename the file with an underscore, then the description from the B column. If necessary (since I don't have Excel on my Mac) I could also use a .txt file instead of Excel if that makes it easier. Any insight or Applescript ideas would be greatly appreciated. Thanks
 
Joined
Aug 2, 2011
Messages
16
Reaction score
0
Points
1
G'day

i wrote a script which does something similar to what you're looking for. but it starts off with one column of original names and one column for the new names.

it works on folders of files rather than as a droplet but it might be a good starting point for you — RenameByList . it wouldn't be too hard to adapt it for your specific workflow.

for the script as it is, you'd have to adapt your excel file — you could use excel's concatenate feature to get your column of new names (number, undescore, description)

hope it helps

m.
 
OP
F
Joined
Oct 13, 2011
Messages
15
Reaction score
0
Points
1
Thank you

Thanks MacGrunt for looking into this. I like the script that you have written. However, it still won't work with all the elements in the same folder, for some reason. It's saying ":" can't be found.

This does appear to be a good starting point for me though. Do you know if it's possible to run this without opening Excel? If not, would a txt file work? (it would have about 5 spaces between column A and column B)

Also, can the script refer to a list which is saved at a specified network path? The .mov files would be in a local folder, and my list would be in a network path. Also, the script itself would need to be in a separate local folder too.

Let's say that we call our network folder with the list SMB://FOLDER PATH FOR LIST/LIST NAME.TXT
Let's call our local folder with the .mov files LOCAL FOLDER WITH QUICKTIME FILES/SUBFOLDER.

Where would these all caps reference names go in your original script?

I know I'm throwing a lot out there... Many thanks for your help already, and any more insight would be greatly appreciated.

Jeff
 
Joined
Aug 2, 2011
Messages
16
Reaction score
0
Points
1
G'day again

Sorry, I missed the bit about how you don't have Excel.

OK, if you're able to export the list as a .csv (comma delimited) file then you could do something like this :

Code:
set mgOriginalNames to {}
set mgNewNames to {}

tell application "Finder"
	set mgTheList to "Colon:Delimited:Path:ToYour:ListFile.csv" as alias
	set mgFolderPath to "Colon:Delimited:Path:ToYour:Folder:" as alias
end tell

open for access mgTheList
set mgList to (read mgTheList)
close access mgTheList

tell application "Finder"
	-- split original list into two lists
	set text item delimiters of AppleScript to ","
	repeat with x from 1 to count paragraphs of mgList
		set end of mgOriginalNames to text item 1 of paragraph x of mgList
		set end of mgNewNames to text item 2 of paragraph x of mgList
	end repeat
	set text item delimiters of AppleScript to ""
	-- get the folder items
	tell application "Finder"
		set mgItems to items of (mgFolderPath as alias)
	end tell
	-- check name and rename if match is found
	repeat with mgItem in mgItems
		set mgName to name of mgItem
		set mgExt to name extension of mgItem
		if mgExt is not "" then set mgExt to "." & mgExt
		set text item delimiters of AppleScript to {"."}
		set mgOldName to text item 1 of mgName
		set text item delimiters of AppleScript to ""
		repeat with x from 1 to count items in mgOriginalNames
			if mgOldName = item x of mgOriginalNames then
				set mgNewName to item x of mgOriginalNames & "_" & item x of mgNewNames
				tell application "Finder"
					set name of mgItem to (mgNewName & mgExt)
				end tell
			end if
		end repeat
	end repeat
end tell

If you can only use a .txt file, you'll have to know exactly how many spaces between entries ('about 5' won't help you). Then relace the comma in this line — set text item delimiters of AppleScript to "," — with that number of spaces

At the top of that script there's a place for you to enter a "Colon:Delimited:path:To... " for the list file and the folder containing the files to rename. The easiest way to get this path is to have the file or folder selected in the Finder and then run this script from Script Editor :
Code:
tell application "Finder"
	get selection as alias
end tell
Then just copy and paste the quoted path from the Result window into the appropriate place in the main script.

Note that the script in its current form doesn't have any sort of filter to only deal with .mov files — it will rename any files or folders stored in that folder you specify.

To change the all caps to lower case you could run the whole lot through the Rename Finder Items action in Automator.

Hope that helps

m.
 
OP
F
Joined
Oct 13, 2011
Messages
15
Reaction score
0
Points
1
That's definitely the right direction- this may work after all! The script was able to find my list file and see the text inside. I could tell from the Applescript summary. However, I got this error even after experimenting with both csv and txt files:
----> "Can't get text item 2 of \"10000 AMBASSADOR 2 \"." number-1728 from text item 2 of "10000 AMBASSADOR 2"

I should mention that the first column was my original filenames (the first entry being 10000)
...and the 2nd column was the new names (the first entry being AMBASSADOR 2 - without the quotes)

Have you heard of this error message?

Another question...
I had these four QuickTimes in the destination folder (in which the file names would be changed):
14751.mov
AEF_NTSC 486 ProRes.mov
SPBBLANT1039.mov
SP WHI2KITNPR59.mov
SP WHEHE.mov

I really only needed 14751 changed at the moment, for my test. Would you anticipate the script being able to ignore the last four because they don't match anything on the list?

Thanks so much for your help so far,

Jeff
 
OP
F
Joined
Oct 13, 2011
Messages
15
Reaction score
0
Points
1
additonal note

I should also mention that when it gave me the error message, it would highlight this line of the script:

"set end of mgNewNames to text item 2 of paragraph x of mgList"
 
Joined
Aug 2, 2011
Messages
16
Reaction score
0
Points
1
I would have expected a csv to render the first line as "10000,AMBASSADOR 2" not "10000 AMBASSADOR 2". If there's only one space between the two elements we'll have to tackle it slightly differently.

Upload your txt file Jeff. Hopefully I'll get a chance to look at it again tonight.

To your other question — yes, files with no match get ignored. That's what we're doing here :
Code:
repeat with x from 1 to count items in mgOriginalNames
if mgOldName = item x of mgOriginalNames then ...
That is, if mgOldName matches nothing in mgOriginalNames then do nothing and move on

m.
 
OP
F
Joined
Oct 13, 2011
Messages
15
Reaction score
0
Points
1
Text file samples

Hi MacGrunt:

I use a data transfer program from our company's database to creat the list, and it gives different options for what type of text is used. I can also change the extension to create a different file type when I export.

I have attached two examples. The name of each shows which "text/file type option" I chose.

At the moment I am trying to figure out a way to make my 2nd column = (house number) + (title) so that I don't get rid of the number when renaming. If there's a way to do this in the Applescript then great - otherwise I can probably figure out a more practical export option.

Jeff

View attachment Comma Separated Variable option.txt

View attachment ASCII text option.txt
 
Joined
Aug 2, 2011
Messages
16
Reaction score
0
Points
1
G'day Jeff

Using your ASCII text file will be OK — but for some reason it's failing at the END of that repeat loop that builds the two lists. We should be able to get over that by adding a try statement.

Here's the replacement for the section immediately after "tell application "Finder" :
Code:
	-- split original list into two lists
	set text item delimiters of AppleScript to "     "
	repeat with x from 1 to count paragraphs of mgList
		try
			set end of mgOriginalNames to text item 1 of paragraph x of mgList
			set end of mgNewNames to text item 2 of paragraph x of mgList
		end try
	end repeat
	set text item delimiters of AppleScript to ""

As it stands, the script will keep both the number and the new name (with an underscore between). That's what this line is doing :
set mgNewName to item x of mgOriginalNames & "_" & item x of mgNewNames

hope that gets you closer

m.
 
OP
F
Joined
Oct 13, 2011
Messages
15
Reaction score
0
Points
1
It didn't just get closer- it worked exactly as I wanted it to! Thanks a million for this.
 

Shop Amazon


Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.
Top