Trouble ending a loop..

So working on this mastermind script, I’ve come a long way and got almost all of it nailed. Really stuck on what I’m guessing is a simple instruction, but I can’t figure out or find any other examples of anything like it.

The script is doing a fantastic job on moving the files and waiting for the target folder to be empty, before moving the next one (Thanks Marc!!!), but it’s getting to the last file, moving it, then getting an error because the originating folder is empty. How do I tell it that’s ok, and to just stop? It’s saying ‘Can’t make file 1 of «class cfol» “1” of application “Finder” into type alias.’

This code (part of a bigger script) is;

tell application "Finder"
	repeat count (folder "1")'s files times
		repeat
			count files of entire contents of (folder "2")
			if the result = 0 then
				set (move (folder "1")'s file 1 as alias to folder "2")'s name to "data.rtf"
			else
				delay 3
				
			end if
		end repeat
	end repeat
end tell
tell application "Finder"
	repeat until (count (folder "1")'s files) = 0
		if (count files of entire contents of (folder "2")) = 0 then
			set (move (folder "1")'s file 1 as alias to folder "2")'s name to "data.rtf"
		else
			delay 3
		end if
	end repeat
end tell

HA! Until…? I’m sure I tried that (not this exact code obviously) and it said ‘nah’…
Awesome, thanks!

Hi bugjuice,

Your scripts have a lot of redundancy in them. You should try to think about a step by step approach to your scripts. Once you have something why get it again? Otherwise, they’re getting better.

Keep going,
kel

Maybe it has something to do with count command. Count is very greedy:

# count is greedy so it will try the results of the entire line
count (scripting components) = 0 --result: 0
# unless you use parentheses
(count (scripting components)) = 0 --result: false

The second line is correct because now a value of at least 1 (in Yosemite 2) will be compared with the integer 0

So I’ve managed to make another loop with a similar script. I thought I could use what I’ve learnt in this thread to tweak to another part that I need, but it’s not happened, and I really don’t understand why… Please could someone show me the error of my ways…?

set fileCount to read file ("Megatron:Users:kit:Desktop:File count.txt")
set PDFCount to fileCount as integer

tell application "Finder"
	repeat
		if (count (folder "3")'s files) = PDFCount then
			
			move every file of (folder "3") to ("Megatron:Users:kit:Desktop")
		end if
	end repeat
end tell

It works great, but it just keeps running after the correct amount of files have been moved. I’ve been using random numbers then duping the file to create more, and then when the number is reached, it moves everything. Really stoked about that! But it doesn’t quit at the end…

Hi,

the script does one of the following scenarios:

If the number of files in folder “3” is not equal to the value in the count.txt file the script is trapped in a infinite loop
If the number of files in folder “3” is equal to the value in the count.txt all files are moved to the desktop and then the script is trapped in a infinite loop

If files are dynamically added while the script is running the files are always moved when the number of files reaches the PDFcount value, but the script will never quit

ok, I understand that, and that’s kind of what I was thinking. So how do I make it stop after the files have been moved? I’ve tried placing the repeat in different spots, but it doesn’t work as well as this, and doesn’t stop the loop. Or it’ll loop once and if there isn’t the right number it stops. If there is the right number, then it moves the files and stops. So it’s almost one or the other (problem) at the moment. I can fix one, but not both!

The first part you mention is fine. I want it to keep looping until all the files are in there, because they’re definitely coming. But it’s the second part I need it to stop after the files have been moved.

just exit the loop after moving the files


set fileCount to read file ("Megatron:Users:kit:Desktop:File count.txt")
set PDFCount to fileCount as integer

tell application "Finder"
	repeat
		if (count (folder "3")'s files) >= PDFCount then
			move every file of folder "3" to desktop
			exit repeat
		end if
		delay 1
	end repeat
end tell

I recommend to add a delay at the end of the loop to save CPU time

ahhhhhh ‘exit’ repeat!! one thing I didn’t find…

Yeah, I’m going to be putting a delay in two spots - first is where you’ve put it, but more 60 probably, and then a delay to move the files as InDesign is creating PDFs, so the script will need to give InDesign a chance to finish saving before moving. Just trying to work out one kink at a time :wink:

Thanks heaps for this!!

I’m also using Indesign to create PDF files but I’m scripting Indesign.

yeah, that’s what I’ve done too.

I’m creating data merged docs and uploading them to an FTP. But the volume has been causing this poor mac to crumble, so a friend has sorted out the .csv to break the output down in to smaller chunks so it can digest it better, get it out the other end, and then merge the PDF in to one, then upload it, with error docs created if it stops anywhere. There’s a lot of scripting involved, but it means an half n hour or so each day, in just a couple of minutes, and I don’t have to even be sat at the Mac!! I’m quite pleased with what’s been achieved!