Re: Replace characters in filename
Re: Replace characters in filename
- Subject: Re: Replace characters in filename
- From: Graff <email@hidden>
- Date: Sat, 12 Jun 2004 22:52:16 -0400
On Jun 12, 2004, at 7:39 PM, Rick Davis wrote:
On Jun 12, 2004, at 5:54 PM, Ken wrote:
On Jun 12, 2004, at 3:49 PM, Rick Davis wrote:
How would I find certain characters (any of a list of 13) in a
filename and replace them with one common character?
Heh, I just posted this yesterday. It's the same solution for
cleaning
up files so they don't have any non-Windows characters, I just flipped
the logic in the CleanNames handler. Just change the
disallowedCharacters property to contain the characters you want to
strip and the replacementCharacter property to the character you want
to replace them with. If you set the replacementCharacter property to
"" then the script will just strip out the disallowed characters.
You are applesolutely right. I wrote a script a couple of weeks ago
to help teachers transfer their documents from the Mac network to
their district windoze network. It converts their Appleworks documents
to either Word or Excel depending upon the file type. It also
truncates the name to 27 characters and adds the dot plus the
appropriate extension. I was just made aware of the "disallowed
characters" on the darkside. I just ran your script and it worked
great. Now I hope I can figure out how to incorporate it into my
script to make it a one step operation for the already over-worked
teachers. Someone suggested I also strip or replace common windoze
expressions like "com1 thru com9, lpt1 thru lpt9, con, nul, and prn".
Any thoughts on that suggestion? I think I am going to assume that no
user has a file saved with any of those expressions and just let them
know to manually change any that may.
You should not need to limit the length of the file names. I believe
almost all modern Windows systems support up to 255 characters in their
file names. I'm also pretty sure that you don't have to worry about
those expressions you listed, so long as they aren't file extensions.
If you want to incorporate it into your script here's the bare-bones
handler that does the cleaning:
----
property allowedCharacters :
"1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ$%'-_@~`!{}()#&+,;=[]. "
property replacementCharacter : "!"
on CleanName(theName)
set newName to ""
repeat with i from 1 to length of theName
set theCharacter to character i of theName
if (theCharacter is in allowedCharacters) then
set newName to newName & theCharacter
else
set newName to newName & replacementCharacter
end if
end repeat
return newName
end CleanName
----
This works on characters which are allowed in Windows file names,
rather than those that aren't. This is best because it also strips
stuff like diacriticals and other non-ANSI characters. The set of
characters that are allowed is probably smaller than the set of
characters which are not allowed and by only keeping characters that
are allowed you are assured of having a valid name. If you try to keep
track of those not allowed you might miss adding one to the list.
- Ken
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.