Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Illegal filenames

Status
Not open for further replies.

Zelandakh

MIS
Mar 12, 1999
12,173
GB
I have been asked to stop people creating files using illegal filenames. It has been suggested I use Applescript.

Want to stop people creating files with / \ ? * & " and so on.

Anyone got any bright ideas?
 
I would assume that you want to strip these characters before the file goes on a Windows- or Unix-compliant server, right? (If these were in fact illegal characters on a Mac system, the only ones where you could do an Applescript, then the system itself (the "Finder") would already block it. )

How about a folder where all files to transfer get dropped, with a folder action script that will look at each name as it comes in and either a) strip the character, b) replace the offending character with a dummy, like an underscore, or c) put up a dialog asking whether the "puttee" wants to rename the file to something else.

Such a script would be a bit long to post here. Check the AppleScript Users mail list (from the Apple Web site) if you would like help writing such a filter.
 
We have a load of files on a Unix server that means when the Mac desktop does a Sherlock the server bombs.

Need to rename the existing files and stop the new ones too.

Anyone want to write a little Applescript and post it?
 
Here is a sample script to get you started. Copy this, paste into Script Editor, and compile (watch out for line breaks that wrap here but aren't supposed to!) Save as a "Script Application". The script application will take a file dropped on it and replace any spaces in the name with underscores. You can modify the first "AppleScript text item delimiters" line to any other character that is giving you problems, and replace the underscore in the second text item delimiter line with any other replacement character you want. You can also just stack up a series of replacements and do them all at once.

Once you get that working (on a sample file named using all the wild characters you expect to encounter), you have to negotiate the following: 1) accessing and changing the names on the server (possibly by copying to your desktop, deleting from the server, changing the names and copying back) and 2) setting up a repeat loop that will handle not only a number of files but the possibility of nested folders as well. Again, that can be lengthy, so I suggest you post the question to the AppleScript Users List, accessible from the AppleScript home page.

----- script begins. Watch for broken line wraps!
on open (somefile)
tell application "Finder"
set SomeName to name of somefile
set name of somefile to my replaceThisText(SomeName)
end tell
end open

on replaceThisText(Incoming)
--recognize divisions by spaces
set AppleScript's text item delimiters to {" "}
copy every word of Incoming to Outgoing
set AppleScript's text item delimiters to {"_"}
--put words back together divided by underscores
set Outgoing to Outgoing as string
--reset divisions to default for any other scripts
set AppleScript's text item delimiters to {""}
return Outgoing
end replaceThisText

-- ----- end script
 
There was just a thread on this kind of topic at the AppleScript user's list in the last week. Sign up through applescript.com and check the archives -- you may find just the ticket already developed for you!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top