• 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: Moving a file and changing its name in X
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Moving a file and changing its name in X


  • Subject: Re: Moving a file and changing its name in X
  • From: Kai <email@hidden>
  • Date: Wed, 26 Feb 2003 04:35:12 +0000

on Tue, 25 Feb 2003 12:11:06 +0000, Mr Tea <email@hidden> wrote:

> [1] I seem to recall someone here suggesting that the 'repeat with i from 1
> to count of mylist' syntax used by Leif to define the scope of his repeat
> loop is preferable the simpler 'repeat with x in y' syntax that I have used.
> Is that so?

I believe there was such a discussion, Nick (and possibly some difference of
opinion). Personally (FWIW), I think it's a matter of horses for courses.

This is actually an excellent question, since the subject seems to cause
some confusion - especially when a script doesn't work as expected. Here's
my understanding of the situation (sorry about the naff examples):

As you know, this form of repeat loop uses a 'counter' to run through the
items in a list...

=====================================================

set jewels to {"emerald", "ruby", "paste", "diamond"}
repeat with stone from 1 to count jewels
if item stone of jewels is "paste" then display dialog "We've been had!"
end repeat

=====================================================

On each iteration, the loop evaluates 'item stone of jewels' as "emerald",
"ruby", "paste", etc. So when the comparison matches ("paste" = "paste"),
the anguished dialog results.

However, this next form simply runs through each item by using references to
the list...

=====================================================

set haystack to {"hay", "needle", "hay", "hay", "hay"}
repeat with thing in haystack
if thing is "needle" then display dialog "Found the pointy thing!"
end repeat

=====================================================

Trouble is, the script doesn't work this time. That's because each iteration
makes this sort of comparison:

item 1 of {"hay", "needle", "hay", "hay", "hay"} = "needle"
item 2 of {"hay", "needle", "hay", "hay", "hay"} = "needle"
...and so on.

Since 'item 2 of {"hay", "needle", "hay", "hay", "hay"}' is a reference, and
"needle" is a string, the comparison doesn't result in a match - and so no
triumphant dialog appears.

In some loops, this doesn't matter - since the value of each item is
automatically evaluated or coerced by some other action[1], such as 'set x
to x * 3' or 'set filePath to folderPath & filename', etc.

You could get the 'haystack' example to work with an explicit coercion in
the repeat loop:

=====================================================

if thing as string is "needle" then display dialog "Ouch!"

=====================================================

However, in the absence of silent coercions, the accepted way to evaluate
any object to which a reference refers is to use 'contents of':

=====================================================

set refrigerator to {"salad", "milk", "donuts", "cheese", "butter"}
repeat with hand in refrigerator
if contents of hand is "donuts" then display dialog "Mmm... donuts!"
end repeat

=====================================================

So, back to your question: Which is the 'best' type of repeat loop to use -
'repeat with i from 1 to count of mylist' or the simpler 'repeat with x in
y'?

For me, it's whatever seems the most appropriate in the circumstances. For
example, I was recently modifying a subroutine that needed to identify the
position in a string (txt) of a character (char). It went something like
this:

=====================================================

property validChars : "ABCDEFGHIJKLMNOPQRSTUVWXYZ[NO-BREAK]
_abcdefghijklmnopqrstuvwxyz1234567890"

on stripInvalidChars from txt
set {res, charCount} to {"", count txt}
repeat with charNo from 1 to charCount
tell txt's item charNo to if it is in validChars or [NO-BREAK]
it is "." and charNo is charCount - 3 then set res to res & it
end repeat
end stripInvalidChars

stripInvalidChars from "my.<Test>.File.doc"
--> "myTestFile.doc"

=====================================================

Unless it happened to signify the start of a file extension, the character
"." had to be stripped from a given string. In this case, counting each
iteration (using charNo) was helpful, since both the value of each character
- and its position in the string - could be checked.

However, in other circumstances, I'm generally happy to use the 'repeat with
x in y' form - adding 'contents of' wherever necessary.

HTH.

======================================================================

[1] Since this is the case with your line 'process_item(("" & this_folder &
this_item) as alias)', there's no problem.

--
Kai
_______________________________________________
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.

  • Follow-Ups:
    • Repeat loops [was - Moving a file and changing its name in X]
      • From: Mr Tea <email@hidden>
  • Prev by Date: Re: AppleScript Analysis
  • Next by Date: TIDs, AppleScript's or not
  • Previous by thread: Re: Moving a file and changing its name in X
  • Next by thread: Repeat loops [was - Moving a file and changing its name in X]
  • Index(es):
    • Date
    • Thread