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

Re: repeat problem


  • Subject: Re: repeat problem
  • From: Christopher Nebel <email@hidden>
  • Date: Tue, 2 Oct 2001 10:55:21 -0700

On Tuesday, September 25, 2001, at 12:30 PM, Don Thompson wrote:

This is a problem that I have experienced in other environments. I *think*
it may be that as you move the items from the folder, Applescript is
becoming confused...

This is more or less correct, but it's not really AppleScript getting confused -- it's a natural side effect of how your script is written and the semantics of getting things by absolute index. Modifying a container as you iterate over it is inherently problematic, so you either have to write around it or use a method that makes it someone else's problem.

Work arounds:

1. Create some arrays and populate them with the full paths to the files you
want to move and to where as you go through the loop. Then loop through that
array to do the actual moves.

2. Instead of moving, copy, then go back and delete.

3. Use a repeat until structure, manually count iterations, and then at the
bottom of the loop check to see if your counter exceeds the number of files
in the folder.

If you were willing to give up the "display dialog", you could do this in one line:

tell application "Finder"
move every file of theFolder whose file type is "EPSF" or file type is "TIFF" to folder "LogoFolder" of folder "MerionRecruitmentAds"
-- if using Mac OS X 10.1, you can say 'whose file type is in {"EPSF", "TIFF"}'.
end tell

(OK, that was actually three lines. Two of them were boring, though.) "Every" and "whose" were invented so you wouldn't have to write loops -- the loop is now the Finder's problem, and it handles it correctly, so you don't have to worry about it.


--Chris Nebel
AppleScript Engineering


From: Steve Suranie <email@hidden>
Date: Tue, 25 Sep 2001 14:19:50 -0400
To: "'email@hidden'"<applescript-
email@hidden>
Subject: repeat problem

Hi folks:

Can anyone give me a hint as to why this is happening:

I am using a repeat statement to go through the contents of a folder and
determine the file type of the files in that folder. If they match a certain
type I want to do one thing, if not I want to ignore them. (For now I am
just moving the ones I want to do something to to another folder.

I have this script:

...set itemCount to count of every item of theFolder...

...repeat with i from 1 to itemCount
set itemName to name of item (i) of theFolder
set itemType to file type of item (i) of theFolder

if itemType = "EPSF" then
display dialog "This is an eps file"
move item (i) of theFolder to folder "LogoFolder" of folder
"MerionRecruitmentAds"
else if itemType = "TIFF" then
display dialog "This is a tif file"
move item (i) of theFolder to folder "LogoFolder" of folder
"MerionRecruitmentAds"
else
display dialog itemName
end if

end repeat...

this loops fine if I do something like this

...repeat with i from 1 to itemCount
set itemName to name of item (i) of theFolder
set itemType to file type of item (i) of theFolder

display dialog itemType
end repeat...

but if I throw in the if statement I get an error message stating it can't
get the last item of folder...blah, bah, blah.

Why won't the loop work with the if statement?

Muchos grandees

Steve
_______________________________________________
applescript-users mailing list
email@hidden
http://www.lists.apple.com/mailman/listinfo/applescript-users
_______________________________________________
applescript-users mailing list
email@hidden
http://www.lists.apple.com/mailman/listinfo/applescript-users


  • Follow-Ups:
    • Re: repeat problem
      • From: Bill Cheeseman <email@hidden>
  • Prev by Date: Re: Is there a "wake up" detector?
  • Next by Date: Re: Is there a "wake up" detector?
  • Previous by thread: TCP/IP, Serial Ports...etc..!!!
  • Next by thread: Re: repeat problem
  • Index(es):
    • Date
    • Thread