• 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: Text items
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Text items


  • Subject: Re: Text items
  • From: "Stockly, Ed" <email@hidden>
  • Date: Wed, 17 Jan 2007 15:57:41 -0800

	 >>Ken>>Wow. Thanks everyone for all the great suggestions. I certainly have plenty to work with now.

	>>>I don't expect the lists to be longer than 100-200 files at any time, so really any of these solutions is good--even my original loops aren't too bad.

You're right about that, Ken, with a small number of files the overhead from a clear, simple AppleScript loop is minimal.

	>>>>However, I think Kai's technique is certainly a more clever, efficient, and general solution.

	I would agree that it's very clever, but I'm not sure if it's any more efficient than using a simple AppleScript loop and I'm not sure if it's a more general solution or if you even want a more general solution.

	>>>I would really like to understand how it works, but I just can't seem to follow the logic.

	That's due to the way the script was written. Kai is a brilliant script writer but his script's can be difficult to follow. Sometimes I think that's by design!  I personally have learned a lot from his posts and his scripts, but I worry when novice applescripters try to learn from his scripts. They're definitely not for beginners.

For example, the script uses single letters for many of its variables. That makes the script harder to write, harder to read and harder to maintain. It also uses a script object for no apparent advantage, although it may have some value in a more complex script.

	>>>>Would someone like to give a line-by-line explanation of how it does what it does?

	Adam has  done that but I don't think he got the purpose of using the script object just right, the script object holds a copy of the list in a property, which allows the script to refer to the property of the script object with a tell. Again, in some contexts that may be helpful, but here IMHO it's overkill.


	Compare the two versions below. I'm far from a great script writer, but you won't need anyone to explain how my version works, you will find it very easy to maintain yourself, or modify for other purposes and, at least on my mac, it runs faster.

	ES

> on run
  set AppleScript's text item delimiters to return
  set names1 to {"231-001_M.tif", "231-001.tif", "231-001.jpg", "231-001-a_M.tif", "231-001-a.tif", "231-001-a.jpg"}
  set strippedNames1 to strip1extensions(names1, "_M")
  set strippedNames2 to strip2extensions(names1, {"_M.tif", ".tif", ".jpg"})
  display dialog ("Result 1:" & return & strippedNames1 as string) & return & "Result 2:" & return & strippedNames2 as string
> end run
>
> on strip1extensions(q, e)
  script o
    property l : q
  end script
  set c to -1 - (count e)
  set tid to text item delimiters
  set text item delimiters to "."
  considering case
  repeat with i from 1 to count o's l
      tell text item 1 of o's l's item i to if it ends with e then
        set o's l's item i to text 1 thru c
      else
        set o's l's item i to it
      end if
    end repeat
  end considering
  set text item delimiters to tid
  return o's l
> end strip1extensions
>
> on strip2extensions(myText, textToRemove)
  set oldDelims to AppleScript's text item delimiters
  set AppleScript's text item delimiters to return
  set myText to myText as string
  repeat with thisStripper in textToRemove
    set AppleScript's text item delimiters to thisStripper & return
    set myText to every text item of myText
    set AppleScript's text item delimiters to return
    set myText to myText as string
  end repeat
  set AppleScript's text item delimiters to oldDelims
  return myText
> end strip2extensions
>
 _______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/mailman//archives/applescript-users

This email sent to email@hidden

  • Follow-Ups:
    • Re: Text items
      • From: kai <email@hidden>
  • Prev by Date: Detecting print job - Multiple printers
  • Next by Date: Re: Text items
  • Previous by thread: Re: Text items
  • Next by thread: Re: Text items
  • Index(es):
    • Date
    • Thread