Repeat loop to check if folders exist (not working yet)

I am working on a longer script that is preparing files to send to a printer. The problem I keep running into is that my FTP browser starts uploading files before I have all the folders compressed with Stuffit. I am trying to create a way to get the upload to wait until all of my folders have been compressed.

As soon as there are no more folders in the upload_folder, the FTP upload can begin so I have come up with this repeat loop to check if folders exist. It is not working yet. If you point this script to a folder that contains any folders the result is false but I want it to display dialog “still repeating” and stay in a continuous loop. Any suggestions?

set upload_folder to alias "Macintosh HD:Users:jon:Desktop:TEST:"
set wait to true
repeat while wait is true
	tell application "Finder"
		set folderCheck to list folder upload_folder without invisibles
		if folderCheck contains (items whose kind is "folder") then
			display dialog "still repeating"
			delay 5
		else
			set wait to false
		end if
	end tell
end repeat

try this providing there is nothing else in the folder



set upload_folder to alias "Kitchen Mac:Users:johnclark:Desktop:TEST:"
set wait to true
repeat while wait is true
	tell application "Finder"
		set folderCheck to list folder upload_folder without invisibles
		if folderCheck = {} then
			display dialog "still repeating"
			delay 5
		else
			set wait to false
		end if
	end tell
end repeat

sorry wrong alias address in previous reply


set upload_folder to alias "Macintosh HD:Users:jon:Desktop:TEST:"
set wait to true
repeat while wait is true
	tell application "Finder"
		set folderCheck to list folder upload_folder without invisibles
		if folderCheck = {} then
			display dialog "still repeating"
			delay 5
		else
			set wait to false
		end if
	end tell
end repeat
set upload_folder to alias "Macintosh HD:Users:Username:Desktop:test:"
set wait to true
repeat while wait is true
	tell application "Finder"
		
		if exists (items of folder upload_folder whose (class is folder)) then
			display dialog "still repeating" buttons "Cancel" giving up after 1
			delay 5
		else
			set wait to false
		end if
	end tell
end repeat

I should have added that the way you wrote the script

set folderCheck to list folder upload_folder without invisibles

this wil list every item in upload_folder

if folderCheck = {} then

so folderCheck will never be {}

Thanks to both of you!
Mark, your version works best because my “upload_folder” will still have files when “wait” is set to false. I have to report that when inserted into the longer script it does exactly what I would like; my FTP browser does not even activate until all the folders are compressed. Perfect!

I have one more issue lingering in this script.
This part has to do with using my FTP browser. Some of the printers I work with have a specific folder for me to upload files to and others do not, so there will be times when I will need to add “with initial folder theUploadFolder” and other times where I do not.
I tried to use:

if theUploadFolder exists then
		connect to theServer with protocol theProtocol as user theUser with password thePassword with initial folder theUploadFolder
	else
		connect to theServer with protocol theProtocol as user theUser with password thePassword
	end if

And below is the script with a bit more of its context if you need it. Notice that printer “HIJ” does not have a “theUploadFolder”.
I am not sure where my problem lies. I doubt this is an issue with the specific app Cyberduck. (I think it has more to do with my novice scripting abilities.) Any suggestions welcome.

if printerName is "ABC" then
	set thePrinterEmail to "abc@email.com"
	set theServer to "01.com"
	set theUser to "abc"
	set thePassword to "01"
	set theUploadFolder to "/Jon's Folder"
else if printerName is "DEF" then
	set thePrinterEmail to "def@email.com"
	set theServer to "02.com"
	set theUser to "def"
	set thePassword to "02"
	set theUploadFolder to "/Jon's Folder"
else if printerName is "HIJ" then
	set thePrinterEmail to "HIJ@email.com"
	set theServer to "03"
	set theUser to "hij"
	set thePassword to "03"
end if
set upload_items to (every file of upload_folder whose kind is not "Folder") as alias as list
if the number of items in the upload_items is greater than 0 then
	with timeout of 600 seconds
		tell application "Cyberduck"
			activate
			set theBrowser to (make new browser)
			tell (theBrowser)
				set theProtocol to "ftp"
				set encoding to "UTF-8"
				try
					if theUploadFolder exists then
						connect to theServer with protocol theProtocol as user theUser with password thePassword with initial folder theUploadFolder
					else
						connect to theServer with protocol theProtocol as user theUser with password thePassword
					end if
				end try
				repeat with theFile in upload_items
					upload file theFile
				end repeat
				disconnect
			end tell
			quit
		end tell
	end timeout
end if

Jacques,
I am not sure if you are understanding my problem. I know that if “printerName” is “HIJ” then I have not defined a value for “theUploadFolder”. (That is sort of the point.) I am trying to use the term “exists” to note whether a pinter used an upload folder or not and then upload accordingly based on that answer.
However, your response got me thinking…
I have now set “theUploadFolder” to “” for every printer that does not have an upload folder on their FTP site so now I’m no longer using “exists” but checking to see if “theUploadFolder” is “”. Here is the working script:

if theUploadFolder is "" then
	connect to theServer with protocol theProtocol as user theUser with password thePassword
else
	connect to theServer with protocol theProtocol as user theUser with password thePassword with initial folder theUploadFolder
end if

I would still like to know if it is possible to use “exists” since I have had little luck using that term successfully. Let me know if you have any ideas and thanks for the help.

Have not tested this.


if printerName is "ABC" then
	set thePrinterEmail to "abc@email.com"
	set theServer to "01.com"
	set theUser to "abc"
	set thePassword to "01"
	set theUploadFolder to "/Jon's Folder"
	set check to "yes"  --<--Note
else if printerName is "DEF" then
	set thePrinterEmail to "def@email.com"
	set theServer to "02.com"
	set theUser to "def"
	set thePassword to "02"
	set theUploadFolder to "/Jon's Folder"
	set check to "yes" --<--Note here
else if printerName is "HIJ" then
	set thePrinterEmail to "HIJ@email.com"
	set theServer to "03"
	set theUser to "hij"
	set thePassword to "03"
	set check to "no" --<--Note here
end if

set upload_items to (every file of upload_folder whose kind is not "Folder") as alias as list
if the number of items in the upload_items is greater than 0 then
	with timeout of 600 seconds
		tell application "Cyberduck"
			activate
			set theBrowser to (make new browser)
			tell (theBrowser)
				set theProtocol to "ftp"
				set encoding to "UTF-8"
				try
					if check is equal to "yes" then --<--Note here
						
						connect to theServer with protocol theProtocol as user theUser with password thePassword with initial folder theUploadFolder
					else
						connect to theServer with protocol theProtocol as user theUser with password thePassword
					end if
				end try
				repeat with theFile in upload_items
					upload file theFile
				end repeat
				disconnect
			end tell
			quit
		end tell
	end timeout
end if

Mark,
Thanks for the suggestion; setting up the variable “check” works. I guess I could also set “check” to true or false instead of using a string value such as “yes” or “no”.

Everything now seems to be working great for the second half of this AppleScript but I still have a couple glitches on the front half. Maybe someone out there can give me a suggestion or two.

PROBLEM:
In the script below both printer “ABC” and “GHI” work correctly but “DEF Inc.” does not. I know it is because I am changing the title to DEFInc but I think I should be able to do this . Any suggestions?

on open {input_folder}
	display dialog "Which printer would you like to upload this job to?" buttons {"ABC", "DEF Inc.", "GHI"}
	set pushButton to button returned of result
	if pushButton is "DEF Inc." then
		set printerName to "DEFInc"
	else
		set printerName to pushButton
	end if
	set newFolderLocation to alias "Macintosh HD:Users:jon:Desktop:TEST:"
	set designerName to "Jon"
	get current date
	copy the result to today
	copy today as string to dateString
	if word 2 of dateString is "January" then set monthName to "01"
	if word 2 of dateString is "February" then set monthName to "02"
	if word 2 of dateString is "March" then set monthName to "03"
	if word 2 of dateString is "April" then set monthName to "04"
	if word 2 of dateString is "May" then set monthName to "05"
	if word 2 of dateString is "June" then set monthName to "06"
	if word 2 of dateString is "July" then set monthName to "07"
	if word 2 of dateString is "August" then set monthName to "08"
	if word 2 of dateString is "September" then set monthName to "09"
	if word 2 of dateString is "October" then set monthName to "10"
	if word 2 of dateString is "November" then set monthName to "11"
	if word 2 of dateString is "December" then set monthName to "12"
	if word 3 of dateString is "1" then set dayName to "01"
	if word 3 of dateString is "2" then set dayName to "02"
	if word 3 of dateString is "3" then set dayName to "03"
	if word 3 of dateString is "4" then set dayName to "04"
	if word 3 of dateString is "5" then set dayName to "05"
	if word 3 of dateString is "6" then set dayName to "06"
	if word 3 of dateString is "7" then set dayName to "07"
	if word 3 of dateString is "8" then set dayName to "08"
	if word 3 of dateString is "9" then
		set dayName to "09"
	else
		set dayName to word 3 of dateString
	end if
	set shortDate to monthName & dayName
	set newFolderName to shortDate & "_" & designerName & "_" & printerName
	tell application "Finder" -- everything passed to the Finder here creates the new folder
		try
			make new folder with properties {name:newFolderName} at newFolderLocation
		on error
			set x to 2
			set newFolderName to shortDate & "_" & designerName & "_" & printerName & x
			try
				make new folder with properties {name:newFolderName} at newFolderLocation
			on error
				set folderOK to false
				repeat while folderOK is false
					set x to x + 1
					set newFolderName to shortDate & "_" & designerName & "_" & printerName & x
					try
						make new folder with properties {name:newFolderName} at newFolderLocation
						set folderPath to newFolderLocation as string
						if folder (folderPath & newFolderName) exists then set folderOK to true
					end try
				end repeat
			end try
		end try
		set folderPath to newFolderLocation as string
		move contents of input_folder to (folderPath & newFolderName)
		try
			set input_files to (every file of entire contents of input_folder whose kind is "Adobe Illustrator Document") as alias list
		on error -- happens if there is only one
			set input_files to (every item of entire contents of input_folder whose kind is "Adobe Illustrator Document") as alias as list
		end try
	end tell
	tell application "Adobe Illustrator"
		activate
		set printToAcrobat to {class:print options, printer name:"Adobe PDF 7.0"}
		repeat with currentFile in input_files
			open currentFile
			print current document options printToAcrobat
			close current document saving no
		end repeat
	end tell
end open