• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag
 

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Removing characters from a string
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Removing characters from a string


  • Subject: Re: Removing characters from a string
  • From: Paul Skinner <email@hidden>
  • Date: Mon, 4 Nov 2002 14:44:17 -0500

Bonus points for actually testing code Kai. I should know better than to throw something up on the list before running it through the grinder.

As to your points; they're all good ones. Excepting [4]. Mea culpa on all the others.
As for [4]...
[4] BTW, while we're doing some trimming, you might want to try running your
own initial suggestion (Tue, 29 Oct 2002 00:28:59 -0500) _without_ the three
lines involving text item delimiters. (You may find they're not entirely
essential to the operations involved there.) ;-)

No they're not essential, but I don't consider a script to be polite if it doesn't store the TIDs and reset them at the end. I didn't support this idea of resetting the TIDs for a long time, later I cursed myself for being stupid and hardheaded. I now store and restore like I should. If you don't it WILL bite you. The middle of the three TIDs lines is only unnecessary if I hadn't erred in leaving off the last null in the list of illegal characters. It was a piece of duct tape (a bad fix to the problem).

I should clarify that I really don't want low character count or low line count. That would be foolish in applescript. Like trying to trim weight off a train by limiting luggage!
The minimal code I posted in response to your first post was intended to emphasize the terseness of your TIDs based replacement line, not to be a proper handler.
Reusability is more important to me than size, clarity or even speed to a point. It's hard to recoup coding time by saving milliseconds up. I want to write a thing once (ok, not once, but until I get it right) and then I never want to think about how convoluted it is to remove illegal characters from a potential filename again.
I actually took your nice clean bit of code and created a standard handler (my standard of course). I post it below as proof that I will burn as many characters as necessary to make reuse easy. I don't claim it to be shortest, fastest or perfect, since I haven't used it for months and fixed additional issues that will arise, but it doesn't make stupid mistakes and it's not slow either.



LegalizeFileName({"the name to clean", "name.toclean", ".", "this/might not.be a legal:name, then again, it could be.", "This23!.oneTOO!", "/./", "Tue, 29 Oct 2002 00:28:59 -0500"})
-->{
"the name to clean",
"nametoclean",
"file 1",
"thismight notbe a legalname, the",
"This23!oneTOO!",
"file 2",
"Tue, 29 Oct 2002 002859 -0500"
}

on LegalizeFileName(parameters)
set previousTIDs to AppleScript's text item delimiters --Store the TIDs' initial value.
set i to 0
try
----------------MultipleArgumentCore---------------
set the output to {} --Prepare an outer 'container' list for the output.
set illegalCharacters to {"\"", "/", ".", ":", ""}
set AppleScript's text item delimiters to ""
repeat with thisPotentialFilename in (parameters as list)
repeat with d in illegalCharacters
set {thisPotentialFilename, text item delimiters} to {(thisPotentialFilename as string)'s text items, d's contents}
end repeat
set thisPotentialFilename to thisPotentialFilename as string
if thisPotentialFilename's length > 32 then
set the end of output to thisPotentialFilename's text 1 thru 32
else
if thisPotentialFilename's length > 1 then
set the end of output to thisPotentialFilename
else --creat a valid filename.
repeat
set i to i + 1
set tempName to ("file " & i) as text
if output does not contain tempName then
set the end of output to tempName
exit repeat
end if
end repeat
end if
end if
end repeat
if class of the output is list and length of the output is 1 then
set output to item 1 of the output --Return a single result for a single argument.
end if
--------------------------------------------------
set AppleScript's text item delimiters to previousTIDs --Restore the TIDs to their initial value.
return the output
on error errorMessage number errorNumber partial result errorResult from errorFrom to ErrorTo
set AppleScript's text item delimiters to "" --Modify unhandled errors to include the class and name of this object.
set errorMessage to ("The " & (class of LegalizeFileName) & " '" & "LegalizeFileName" & "' generated an error." & return & the errorMessage) as text
set AppleScript's text item delimiters to previousTIDs
error errorMessage number errorNumber partial result errorResult from errorFrom to ErrorTo
end try
end LegalizeFileName



--Documentation for LegalizeFileName.
--These properties can be referenced for documentation and for version and dependency checking.
property name : "LegalizeFileName" --The name of this script object. <TEXT>
property objects : {LegalizeFileName} --A list of objects that are within this script object. <LIST>
property version : 1.0 --A version code for this object. <REAL>
property dependencies : {asDependencies:{"1.9"}, osDependencies:{"10.2.1"}, osaxDependencies:{}, handlerDependencies:{}, applicationDependencies:{}} --<RECORD>
property documentation : "LegalizeFileName Object.

LegalizeFileName will remove 'illegal' characters from the text that is passed as argument.

Parameters: STRING or LIST of STRINGS.

Output: STRING or LIST of STRINGS not containing any of the illegal characters.

Error Handling: If processing a parameter results in a null string then LegalizeFileName will create a filename in the format 'file n' where n will be a unique sequential number starting with 1 and incrementing by 1.
Errors that are not handled by LegalizeFileName internally will be modified to contain the name and class of LegalizeFileName before being thrown.

Comments:<{thisPotentialFilename, text item delimiters} to {(thisPotentialFilename as string)'s text items, d's contents}> by Kai Edwards 2002.

Paul Skinner 2002
email@hidden
" --<TEXT>
--
Paul Skinner
_______________________________________________
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.

References: 
 >Re: Removing characters from a string (From: Kai Edwards <email@hidden>)

  • Prev by Date: Re: File Mod Date
  • Next by Date: Scriptable Video Importing??
  • Previous by thread: Re: Removing characters from a string
  • Next by thread: Re: Removing characters from a string
  • Index(es):
    • Date
    • Thread