renaming consecutive files from a list of names...

Hello all,

after two days of searching, the closest match for what I need to do is in this thread: http://bbs.applescript.net/viewtopic.php?id=10906. In the post it describes how to consecutively add alphabet letters to filenames. I need to do something similar, however instead of my list being the alphabet, I need to set my list from a text file (its a list of store numbers for a customer). I’ve found ways in my books to set a list to equal the contents of a text file, but I get an error whenever I try it " can’t set MapFiles to every word of mapnames2006.txt as string " or something like that.

Currently the files are jpgs generated from a 3002 page PDF, each file bearing the sequential page number its from in the PDF. The text file is composed of a list of six digit numbers seperated by a single space (I set it up like this thinking applescript could add by word) and the numbers are in the same order that they need to applied to the files. There are exactly 3002 files and 3002 six digit numbers… among other things could there be a limit to how many numbers (“words”) can be loaded into a list? Also, here’s a script I tried writing yesterday… I’m not very good at this yet… I’m using this task as a trainer for myself actually, so make fun of it all you want :slight_smile: :

set FileNames to (choose file) as string
set FileNamesB to (every word of FileNames) as string
set MapFiles to (every file of (choose folder))
display(MapFiles)
set CurNum to 1

repeat with CurFile in MapFiles
set FileNamesBNum to item CurNum of FileNamesB
–set MapFilesNum to item CurNum of MapFiles
set the name of item CurFile to FileNamesBNum
set CurNum to (CurNum + 1)
end repeat
end

I found it easier to have the script prompt me for the file and the directory it needs to work from, I kept getting errors about files that I knew were there being missing, so I figured this was an easier way for me to handle it. Any replies, advice, comments would be greatly appreciated. Thanks!

Mark

(this is copied from a reply I put in the original post, but I realized that it has more relevance here, since im using OS X)

Model: PowerMac G5 Dual 2ghz
AppleScript: 1.10.3
Browser: Firefox 1.5.0.1
Operating System: Mac OS X (10.4)

Hi Mark,

This line won’t work, because you need to read the file first. Change this:

set FileNamesB to (every word of FileNames) as string

to this:

set t to read FileNames
set FileNamesB to every word of t

This line for naming needs the Finder:

set the name of item CurFile to FileNamesBNum

Change to:

tell app “Finder”
set the name of CurFile to FileNamesBNum
end tell

‘item’ was not needed since CurFile is a reference.

I think it should work now.

gl,

Thanks for the reply Kel.

I made the suggested changes and now when it reaches line #2:

set t to read FileNames

I get the error: “Can’t make "Macintosh HD:Users:markmoziak:Desktop:fall2006.txt" into type file.”

Any ideas? is it suggesting it can’t interpet the file as a text file?

thanks again!

Mark

Hi Mark,

Yes. How did you make the file?

gl,

it was originally received as an excel spreadsheet. I deleted the other columns besides the numbers we needed for this job, making sure to keep them in order and then saved it as tab deliminated. Opened that in textedit just fine. even though it was opening with one number per line. I think I ended up taking it into BBEdit and doing a find and replace on all return codes and extra spaces to make it so the file is just the string of numbers with one space between each one… but made sure to keep it in a text format instead of any sort of BBEdit format… not that there’s much of a difference… or is there?

I found more mistakes, so quickly rewrote the script. I didn’t check it yet but it should be close:

set FileNames to (choose file)
set t to read FileNames
set FileNamesB to every word of t
set f to choose folder
tell application “Finder”
try
set MapFiles to (every file of f) as alias list
on error
set MapFiles to (first file of f) as alias as list
end try
end tell
–display(MapFiles)
set CurNum to 1

repeat with CurFile in MapFiles
set FileNamesBNum to item CurNum of FileNamesB
–set MapFilesNum to item CurNum of MapFiles
set the name of CurFile to FileNamesBNum
set CurNum to (CurNum + 1)
end repeat

gl,

oops, forgot the tell block:

set FileNames to (choose file)
set t to read FileNames
set FileNamesB to every word of t
set f to choose folder
tell application “Finder”
try
set MapFiles to (every file of f) as alias list
on error
set MapFiles to (first file of f) as alias as list
end try
end tell
–display(MapFiles)
set CurNum to 1

repeat with CurFile in MapFiles
set FileNamesBNum to item CurNum of FileNamesB
–set MapFilesNum to item CurNum of MapFiles
tell application “Finder”
set the name of CurFile to FileNamesBNum
end tell
set CurNum to (CurNum + 1)
end repeat

gl,

Wow, that worked. I had to play with my original filenames, removing the other items and placing the appropiate amount of zeroes in front of the numbers (1.jpg becoming 0001.jpg, 993.jpg becoming 0993.jpg and so on) but that seems to have done it. Thanks a lot for the help Kel, really appreciate it. What were the other errors? Im reading yours and I think I see how it works, im just trying to learn from the whole thing. Thanks again!

Mark

Hi Mark,

One other mistake was partly my error. This line:

set FileNames to (choose file)

returns an alias reference which is needed to read the file it points to. It was:

set FileNames to (choose file) as string

which returns a string. This is what was causing the error you were getting and not a bad file. The other one was mainly cosmetic. When you get references to items in the Finder, it returns a Finder reference which looks something like this:

item “a” of folder “b” of folder “c” of startup disk. New scripters often take this refernce and try to use it in some app that doesn’t know how to use Finder references. Most apps know how to use alias references. that’s why I added the coercion to ‘alias list’. Showing new scripters this way is better to avoid future troubles.

The other thing I changed was pulling the ‘choose file’ out of the Finder tell block. A lot of people do it, but it’s good as a rule to leave scripting additions commands out of an application tell block when possible. With some scripting addition commands and their parameters, you get conflicts with application keywords.

Glad it worked for you and nice to know you want to learn how to do it yourself.

gl,