• 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: Verify if name contains an element of a list
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Verify if name contains an element of a list


  • Subject: Re: Verify if name contains an element of a list
  • From: "Adam Bell" <email@hidden>
  • Date: Wed, 19 Apr 2006 16:14:07 -0300

My variation on this (Kai is too quick for me) replaces the offending character in a list of filenames with an underscore "_"

set fileNames to {"My?File.scpt", "m/yF|le.txt", "my*File.txt "}
set delims to {"/", "|", "\\", "*", "?"} as Unicode text

set CleanNames to {}
set removals to delims
repeat with k from 1 to count fileNames
    set end of CleanNames to multiTID(item k of fileNames, removals)
end repeat
CleanNames --> {My_File.scpt, m_yF_le.txt, my_File.txt}

--- handlers ---
on multiTID(tString, delims)
    set {saveOTID, _marker_} to {atid("otid"), "#$%"}
    repeat with aDelim in delims
        my atid(aDelim)
        set tString to text items of tString
        my atid(_marker_)
        set tString to text items of tString as string
    end repeat
    my atid(_marker_)
    set tString to text items of tString
    my atid("_")
    set tString to text items of tString as string
    my atid(saveOTID)
    return tString as string
end multiTID

on atid(delim)
    if delim = "otid" then
        return AppleScript's text item delimiters
    else
        set AppleScript's text item delimiters to delim
    end if
end atid


On 4/19/06, kai <email@hidden> wrote:

On 19 Apr 2006, at 19:30, stephan wrote:

> Is there a way I can use AppleScript to verify if the name of a
> file or a
> folder contains an element in a list?
>
> Something like:
>
> myListofChar = {"/", "\", "*", "?", "|"}
>
> if FileName contains a element of myListofChar then
>
>
> I need to identify which element of the list and replace it by
> something
> else.

To replace all occurrences with a single character:

---------------

to switch_chars of someText from searchList to replaceChar
        set tid to text item delimiters
        repeat with i in searchList
                if i is in someText then
                        set text item delimiters to i
                        set someText to someText's text items
                        set text item delimiters to replaceChar
                        set someText to someText as string (* or unicode text *)
                end if
        end repeat
        set text item delimiters to tid
        someText
end switch_chars

set searchList to {"/", "\\", "*", "?", "|"}
set replaceChar to "_"
set someText to "*may* be this and/or that"
set newText to switch_chars of someText from searchList to replaceChar

--> "_may_ be this and_or that"

---------------

To replace each occurrence with a corresponding alternative:

---------------

to switch_chars of someText from searchList to replaceList
        set tid to text item delimiters
        repeat with i from 1 to count searchList
                set s to item i of searchList
                if s is in someText then
                        set text item delimiters to s
                        set someText to someText's text items
                        set text item delimiters to item i of replaceList
                        set someText to someText as string (* or unicode text *)
                end if
        end repeat
        set text item delimiters to tid
        someText
end switch_chars

set searchList to {"/", "\\", "*", "?", "|"}
set replaceList to {" A ", " B ", " C ", " D ", " E "}
set someText to "*may* be this and/or that"
set newText to switch_chars of someText from searchList to replaceList

--> " C may C  be this and A or that"

---------------

---
kai


_______________________________________________
Do not post admin requests to the list. They will be ignored.
 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: Verify if name contains an element of a list
      • From: kai <email@hidden>
References: 
 >Verify if name contains an element of a list (From: stephan <email@hidden>)
 >Re: Verify if name contains an element of a list (From: kai <email@hidden>)

  • Prev by Date: Re: Check FTP file
  • Next by Date: Re: Apply User and Groups to Items in a Folder
  • Previous by thread: Re: Verify if name contains an element of a list
  • Next by thread: Re: Verify if name contains an element of a list
  • Index(es):
    • Date
    • Thread