Up for a challenge? Renaming and moving files around scripts

Hi,
long time reader, first time poster. I know a little about AS, but a lot is just do and learn as I go! Forgive me if this is in the wrong forum.

I’ve created a script that activates from a csv being dropped in my Dropbox folder (so can activate this from my phone), moves the file to a folder where Excel picks it up, does some formatting and sorting, saves in another folder which then opens InDesign, opens a template, opens the data.csv, merges the doc, saves as a PDF, closes both docs (without saving, they’re not needed), trashes the .csv, rename the PDF with the date and timestamp of that second, uploads the PDF to an FTP and back to Dropbox where I get a notification on my phone.

That part was easy (if you want this script, let me know and I’ll post it up, except for the Excel script as it’s not mine to distribute…).

What I need to do is insert an action after Excel has done its’ bit. I started writing the script, but I’m having all sorts of issues, so it’s not really worth posting up how little I made.

Excel is going to break the csv down in to smaller bite-size chunks so InDesign can process it quicker/easier as there’s a varying amount of letters to merge. Could be 1,000 could be 10,000. Excel is going to save them in 2,000 chunks.

Here’s the challenge;
An unknown amount of data.csv’s (data.csv, data1.csv, data2.csv etc) will be in saved to folder1 and this number needs to be saved for later. I’d like a script that moves ‘data.csv’ to folder2 which will trigger the above action. That much I can do. Then when the above script delets the data.csv, the new script must rename the next file in the queue (so data1.csv) to data.csv and move it to folder2 to fire InDesign again, and so forth until folder1 is empty. The the saved file amount number is passed to the next part of the script (or a new one) where it’ll wait for the same number of PDFs to be made and put in to folder3, then combine all the PDFs and move to the last folder4 (where it gets renamed and moved accordingly).

It’s taken a while just to think of how I can break this down like this, open to suggestions if anyone else knows better…?
I’ve been breaking down a possible script in to chunks, but the waiting for folder2 to be empty to rename and move the next file is a bit of a kicker for me!

Any thoughts? Thanks in advance for any replies! :smiley:

So after ditching what I started with, and breaking it down, I’ve managed to move a file to folder2, rename the next file in folder1 (altho cheated in a way as it’s specific, but that’s ok), and got it to wait until folder2 is empty, then it moves that. But it’s throwing Finder in to a fizz and it’s looping too much. I thought after it had done the script and there’s no more files, then it’d end repeat? Any clues on this part? The script just keeps running.
Once this is done, then I’ll move on to the next part…

So far, it looks like (the .rtf is just a junk test file, I’ll replace names with .csv and paths etc later);

set theFile to ("Macintosh HD:Users:kit.greer:Desktop:1:data.rtf")
set fileName to ("data.rtf")

tell application "Finder"
	repeat
		try
			move document file "Macintosh HD:Users:kit.greer:Desktop:1:data.rtf" to "Macintosh HD:Users:kit.greer:Desktop:2"
			
			set the name of document file "Macintosh HD:Users:kit.greer:Desktop:1:data 1.rtf" to fileName
			
			if FileExists(theFile) then
					if exists file theFile then
						delay 5
					else
						return false
					end if
			else
				
				if not (exists file theFile) then
					set the name of document file "Macintosh HD:Users:kit.greer:Desktop:1:data 2.rtf" to fileName
					
					move document file "Macintosh HD:Users:kit.greer:Desktop:1:data.rtf" to "Macintosh HD:Users:kit.greer:Desktop:2"
					
				end if
			end if
		end try
	end repeat
end tell

My assumption is that the trigger is being “pulled” with every file moving into folder “2.” This is a common problem with folder actions that can be fixed by not doing anything further inside the trigger folder.

As a sidenote, if you’re going to use the Finder, full paths aren’t necessary; first folder levels are addressable by name. I’d probably rewrite your code to get rid of the conditionals, but be careful with your naming convention; there’s a big difference in folder 1 vs. folder “1.”


tell application "Finder"
	repeat count (folder "1")'s files times
		try --won't exist on first pass
			delete folder "2"'s file 1 --unclear what's to be done with the file, but this gets it out of the way
		end try
		set (move (folder "1")'s file 1 as alias to folder "2")'s name to "data.rtf"
	end repeat
end tell

haha wow! That worked way better than the tons of lines I’ve got in front of me at the moment!!

With regards to the <delete folder “2”'s file 1> command, a different script will be deleting it when InDesign has finished with it. I’ve had a little luck with <if exists file (“data.rtf”) then> command so that it’s waiting for the folder to be emptied before copying the next one in, but I don’t think I can adapt it in this instance. What would be the best command for it to wait, instead of delete? The time taken for it to be dumped is unknown, so it just has to watch and wait.

Thanks for this tho, so awesome!

oh, and the folders are structured and named way better than 1, 2 etc… This is just for setting up and basic testing. Once it’s all done, then the folder path will be corrected. With notes to the not having to use the full path, does that apply to any folder on the mac in any location? The folders it’ll be pointing to are 2 or 3 deep within other folders. Will it hunt them down? Or this is just a ref to them being on the desktop? Cheers!

Ah, okay. The path shortcut is really just applicable to the first folder level on the desktop. As long as you avoid the trigger folder, you could just stick with the arbitrary delay in your original method. If the delay is dependent on InDesign doing something, consider letting it do everything. You don’t necessarily need to rename or move files around to iterate through them; ID can just scavenge their data.

ah k, thanks for clarifying about the folder path. I’ll stick to the full paths in the final cut then.

The InDesign script generates a mail merge, and the deletes the data.rtf file (well, it’s a csv, but for testing…). When this script renames and moves another data.rtf, then it’ll trigger InDesign again. Basically the volume of work it needs to do is too much, so breaking it down is the best way to help it out.

I’ve tweaked this to here, but I’m having problems with the ‘else’ part, as in ‘else wait for 5 and try again’, but I don’t quite know how to tell it this. If I manually remove the files within 5, then this script continues. Just don’t know how to get it to retry…

tell application "Finder"
	repeat count (folder "1")'s files times
		
		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 5
			
		end if
	end repeat
end tell

Don’t be to hard on yourself. Marc’s example may be much shorter but from a discipline’s perspective your work flow, not the code, is way better even if it has more lines. You have more control and will actually know why the file can’t be removed. Marc’s example will just ignore the outcome of the delete and continue anyway. It’s shorter and nicer too look at but it could be permission problem, damaged file or a file that is open with read access by another application. You just don’t know.

But, hey, we’re talking AppleScript. So don’t take the code too serious, meaning that Marc’s example will do just fine. I’m talking in a more general programming discipline than AppleScript specific code. But I just wanted to let you know that your strategic is the way to go for most programming languages. Always check if the data meets the requirements of your data before processing it. It’s one of the lessons of writing safe code.

DJ’s correct; the goal of any code is functionality over style, and try hides/traps errors from being reported. I made assumptions about how things would work, and I engineered my example to purposely ignore the first deletion, because I knew it was bogus. You can, however, always comment out try blocks, to inspect events.

Since my example works a set number of times, you’d wrap the loop with another repeat, and then introduce the delay in the outer one. You can also repeat until a condition is met or by a number.

it is more understanding how the language works, and how to make it work for my needs. I am one for writing things out, but I was getting in a real repeat without an end repeat! So I’m stoked to see such a simple solution, but I appreciate the support of learning too…

Still working out the bugs tho! I’ve got the script to repeat on itself while it waits for folder 2 to be emptied, but then when the last file is moved from 1, it doesn’t end, it keeps goin. I’ve been playing with where to put the repeat command, but there aren’t many options to choose from, so at a slight loss… it’s only developed to here so far;

tell application "Finder"
	
	set the FileNumber to (number of files in folder "1")
	set FileText to "" & FileNumber & ""
	
	tell application "TextEdit"
		make new document
		
		set the text of the front document to FileText
		save document "Untitled" in file "Megatron:Users:kit:Desktop:File count.rtf"
		close saving no
		quit application "TextEdit"
	end tell
	
	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 5
				
			end if
		end repeat
	end repeat
end tell

I’m going to move on from this for the moment to have this script stick a number in a textedit file on the desktop, which then the script for smushing the PDFs will wait for this many files to drop in, before creating one file out of them. Little surprised at how little info there is on such a process, considering how much mail gets churned out! I know they have big data crunching PCs in the mail houses, but the smaller businesses don’t…

Edit - So I’ve got to this, where it creates the file and then gets on with work, but it’s still looping on the repeat, not stopping when the folder is empty/=0, and it seems to bog the finder down a bit too… System events…??