Repeat Loop

I’m trying to run repeat loop.

I have a bunch of files that I am automating to pdf. I want to be able to loop the process until all the files are pdf’d.

Only way I can think of doing this is to check with in the program if a window exists or until the print option is no longer usable.

Any ideas.

I’m not sure how much you know, but what you said was rather vague, so here’s a somewhat vague answer.

What you need to do for this to work is put the files into a list, then do this:


repeat with myCount from 1 to (count items of myList)
(* do something with item myCount of myList *)
end repeat

What this does is sets the variable myCount to 1, and loops the command until myCount is (count items of myList). Then, to reference the file, just use “item myCount of myList”.

Hopefully this is what you wanted. Good luck!

-SuperScripter

Hi.

Thanks for that. This makes sense. I have managed to create a list of files from a folder. What I need to do now is to open the list of files. I using this to open MS word documents at the moment. Once they are open I can run the repeat cycle on them.

what I have at the moment is


tell application "Finder"
	activate
	set the source_folder to (choose folder with prompt "Please choose a source folder" without multiple selections allowed and invisibles)
end tell

tell application "Finder"
	try
		set tFiles to files of entire contents of source_folder as alias list -- works for many
	on error -- if there is only one file in the folder "as alias list" fails.
		set tFiles to files of entire contents of source_folder as alias as list -- works for one
	end try
end tell


What I am not sure is how to make MS word open the files from the list that it outputs.

Thanks for your help.

Well I managed to sort it all out.

If any one is interested this is what I had created. Which will open a folder full of word documents from a source folder and pdf them into a destination folder. There might be an easier way to do this, but it does mean I can edit the script for almost any program.


tell application "Finder"
	activate
	set the source_folder to (choose folder with prompt "Please choose a source folder" without multiple selections allowed and invisibles)
end tell

tell application "Finder"
	activate
	set the output_folder to (choose folder with prompt "Please choose a destination folder" without multiple selections allowed and invisibles)
end tell


tell application "Finder"
	set theFiles to every file of entire contents of source_folder
	repeat with i from 1 to count of theFiles
		set thisFile to item i of theFiles as alias
		tell application "Microsoft Word" to open thisFile
	end repeat
end tell

tell application "Finder"
	try
		set myList to files of entire contents of source_folder as alias list -- works for many
	on error -- if there is only one file in the folder "as alias list" fails.
		set myList to files of entire contents of source_folder as alias as list -- works for one
	end try
end tell

tell application "Microsoft Word"
	activate
end tell

repeat with myCount from 1 to (count items of myList)
	tell application "System Events"
		tell application process "Microsoft Word"
			if (exists window 2) then
				
				keystroke "p" using command down
				
				repeat until exists window "Print"
				end repeat
				
				click menu button "PDF" of window "Print"
				
				
				repeat until exists menu item "Save as PDF." of menu 1 of menu button "PDF" of window "Print"
				end repeat
				
				click menu item "Save as PDF." of menu 1 of menu button "PDF" of window "Print"
				
				repeat until exists window "Save"
				end repeat
				
				set value of text field 1 of sheet of window "Save" to output_folder
				
				click button "Go" of sheet of window "Save"
				
				click button "Save" of window "Save"
				
				select window 2
				tell application "System Events" to keystroke "w" using command down
				
			end if
		end tell
	end tell
end repeat

If you are doing a series of operations with an application you only have to tell it once, then put in the operations, then end the tell. There is no need to activate the application, the tell block will do that – activate is usually used when you want System Events to do something to an open application window and it needs to be in front.

You didn’t have to activate Microsoft Word in that 3-line block, you got System Events to do it with tell application process…

Hi,

calling entire contents of a folder twice is very expensive,
it’s more effective to open, print and close each document one by one.


set the source_folder to (choose folder with prompt "Please choose a source folder") -- without multiple selections allowed and invisibles is default
set the output_folder to (choose folder with prompt "Please choose a destination folder")

tell application "Finder"
	set theFiles to files of entire contents of source_folder as alias list -- works always in 10.5 and higher
end tell

activate application "Microsoft Word"
repeat with thisFile in theFiles
	tell application "Microsoft Word" to open thisFile
	tell application "System Events"
		tell application process "Microsoft Word"
			keystroke "p" using command down
			
			repeat until exists window "Print"
			end repeat
			
			click menu button "PDF" of window "Print"
			
			repeat until exists menu item "Save as PDF." of menu 1 of menu button "PDF" of window "Print"
			end repeat
			
			click menu item "Save as PDF." of menu 1 of menu button "PDF" of window "Print"
			
			repeat until exists window "Save"
			end repeat
			
			set value of text field 1 of sheet of window "Save" to output_folder
			
			click button "Go" of sheet of window "Save"
			click button "Save" of window "Save"
		end tell
	end tell
	tell application "Microsoft Word" to close document 1 saving no
end repeat

Sorry Adam, that’s wrong.
As pressing keyboard shortcuts via System Events affects always the menu of the frontmost application,
you have to activate it explicitly. Just telling the process doesn’t do it.