That is because AppleScript (like the sh shell) gives the backslash character in string literals a special meaning. The most common purpose is to let a string literal contain an internal double quote character. To do this you put a backslash in front of the embedded double quote character to prevent that double quote character from ending the string literal. But because of this special meaning of the backslash, if you want a backslash in a string literal you also have to put a backslash in before it. So to get * as the value of a string written as a string literal, you must write “*\**”.
Next, if you want to apply identical processing to all the files that have these undesirable characters, then you can test for them all at once with a slight change to your -name conditional in your find command. Try -name ‘[/\:?"|]*’ (the double backslash is for the filename pattern interpretation that find will do), which when you write it as an AppleScript string literal looks like this: “-name ‘[/\\:?"|]*’” (each backslash is doubled, and the double quote gets a backslash too; these escapes are undone when AppleScript converts the string literal to an internal string value).
If you would like to delete the characters or replace them with a specific character or string you could use some handlers like these (demonstration in the run handler):
on run
set str to "a fi|e name: with l*ts of \"b/\\d\" Chars?"
set replaced to replaceBadChars(str, "_")
set deleted to deleteBadChars(str)
{str, replaced, deleted}
--> {"a fi|e name: with l*ts of \"b/\\d\" Chars?", "a fi_e name_ with l_ts of _b__d_ Chars_", "a fie name with lts of bd Chars"}
end run
(* switchText From: http://bbs.applescript.net/viewtopic.php?pid=41257#p41257
Credit: kai, Nigel Garvey*)
to switchText from t to r instead of s
local d
set d to text item delimiters
try
set text item delimiters to s
set t to t's text items
-- The text items will be of the same class (string/unicode text) as the original string.
set text item delimiters to r
-- Using the first text item (beginning) as the first part of the concatentation means we preserve the class of the original string in the edited string.
tell t to set t to beginning & ({""} & rest)
set text item delimiters to d
on error m number n from o partial result r to t
set text item delimiters to d
error m number n from o partial result r to t
end try
t
end switchText
to replaceBadChars(str, replacementStr)
local badChars
set badChars to characters of "*/\\:?\"|"
-- The plural name of "text item delimiters" would tend to indicate that you could use a list many such delimiters. In practice (on my machine) only the first one is effective.
-- In other words, when I tried "switchText from str to replacementStr instead of badChars" the only character that was replaced was the first: asterisk.
repeat with ch in badChars
set str to switchText from str to replacementStr instead of contents of ch
end repeat
str
end replaceBadChars
to deleteBadChars(str)
replaceBadChars(str, "")
end deleteBadChars
You might use something like set zipName to deleteBadChars(each_record) then use zipName for the name of your zip file that contains each_record. Though if you are processing a folder that has subfolders and those subfolders have the undesirable characters in them, this will likely fail because you will be deleting/replacing the characters in the subfolder names too, likely resulting in pathnames that reference non-existent subfolders.
Usually I try to avoid making ‘archive’ files that contain only a single file, so I would probably try to package up the files into a single “.zip” file. But you are in the best position to make a decision like that since we do not know the details of why you need to package up the files in the first place. Yes, you say that they are going to Windows, but how are they going to be used there? How is the Windows user going to extract them from the “.zip” file? Will their extractor even be able to handle extracting archived filenames that have illegal characters in them? If so, what does it do with the characters? Individually replace them? Replace them as a group? Drop them from the resulting filename?
Also, this particular find technique will fail if you process any files that have carriage returns or linefeeds in their names.
Model: iBook G4 933
AppleScript: 1.10.7
Browser: Safari 3.0.4 (523.12)
Operating System: Mac OS X (10.4)