Re: How to delete text items?
Re: How to delete text items?
- Subject: Re: How to delete text items?
- From: Paul Berkowitz <email@hidden>
- Date: Sun, 03 Mar 2002 14:03:37 -0800
On 3/3/02 1:39 PM, "Martin Michel" <email@hidden> wrote:
>
Hi,
>
>
I have a problem and I think you can help me.
>
>
Imagine you have a file comment which contains the characters ":" & "/".
>
Now you want to name a file or folder after this file comment. But you
>
can't at first sight because characters like ":" & "/" are not allowed
>
in file- or folder names. So what do you do using AppleScript to get rid
>
of those characters in the file comment? I know I can count characters
>
in text but how do you delete specific characters?
>
By "file comment" do you mean what you see when you Get-Info for a file? Any
text character can be changed easily using AppleScript's text item
delimiters, which are always {""} by default. Turning the string into a list
of strings using your chosen delimiter excludes all instances of that
delimiter (":" or "?" here.) Then replace it by an acceptable character ("-"
for example) , and coerce the list back to a string using the new delimter.
You always have to reset the delimiters to what they were at the end of your
operations, because they apply globally in the application running the
script until you quit it, and would mess up other scripts if you don't. So:
set theFile to (choose file)
tell application "Finder"
set theComment to comment of theFile
set oldDelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to {":"}
set ls to text items of theComment
set AppleScript's text item delimiters to {"-"}
set theComment to ls as string -- replaces : with -
set AppleScript's text item delimiters to {"/"}
set ls to text items of theComment
set AppleScript's text item delimiters to {"-"}
set theComment to ls as string -- replaces / with -
set AppleScript's text item delimiters to oldDelims
set name of theFile to theComment
end tell
(Here's a case where it's just fine to have AppleScript commands inside a
Finder tell block, but you could easily do this outside it if you wanted to:
set theFile to (choose file)
tell application "Finder" to set theComment to comment of theFile
set oldDelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to {":"}
set ls to text items of theComment
set AppleScript's text item delimiters to {"-"}
set theComment to ls as string -- replaces : with -
set AppleScript's text item delimiters to {"/"}
set ls to text items of theComment
set AppleScript's text item delimiters to {"-"}
set theComment to ls as string -- replaces / with -
set AppleScript's text item delimiters to oldDelims
tell application "Finder" to set name of theFile to theComment
--
Paul Berkowitz
_______________________________________________
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.