Download and delete files of a certain type

Hi all,
I have a task in front of me that I just cannot seem to get to work the way I want.
The program I am working on needs to connect to a remote FTP server, look for .txt files, download them and then delete them off the server.

I am currently trying to use Transmit to complete this task, but since the file name is always different, the only common denominator is the file type (txt).

I have completely run my resources dry, googled everywhere, etc.

I contacted Transmit and a tech suggested this:

I am still quite a noob but I’ve made it this far but I don’t understand how to make this loop as he suggested.

Please help a noob!

-chris

Model: Powerbook G4
AppleScript: Current
Browser: Safari 522.12.1
Operating System: Mac OS X (10.4)

chris:

Using the list remote folder command will return a list of the names of all the items in that chosen folder. These will be names only, not actual file references, so you would loop through them like this:

tell application "Transmit"
	set a to list remote folder TheFolderYouWant --This variable needs to be the name of the folder on the server
	repeat with a_filename in a
		if ((characters -4 thru -1 of a_filename) as string) = ".txt" then --This should only select the filenames that end with .txt
			download (TheFolderYouWant & a_filename) --This should build reference to download the .txt file
		end if
	end repeat
end tell

Mind you, I have Transmit, but have never scripted it (Bad on me), but according to the dictionary, this should at least download the files. It may be advantageous to try this out first before going on to the deletion portion.

Thanks! This will get me started and if I have any more questions, I’ll be back!

Thanks again,
Chris

Ok, I had to do an overhaul but I finally got it working!
Since I have seen so many requests, here you go. I did not do this alone. Support from Panic Software helped me.

This is close to my exact code. Hopefully I edited it correctly for a generic download then delete.

This is based on me having a push button with a AS name of “transfer”.


on clicked theObject
	
	if name of theObject = "transfer" then
		
		tell application "Transmit"

			-- define the list of acceptable file extensions
			set extensionList to {"txt"}
			
			-- create a new window for this script to run in
			set theDoc to (make new document at end)
			
			tell current session of theDoc
				
				-- connect
				if (connect to favorite with name "NAME OF FAVORITE") then
										
					-- get a list for the current folder
					set folderList to list remote folder
					
					-- loop through all items in the list
					repeat with i from 1 to (count items in folderList)
						-- select the next item in the list
						set currentItem to (item i of folderList)
						
						-- split the current item's name into name and extension elements
						set AppleScript's text item delimiters to "."
						set nameAndExtension to (text items of currentItem)
						
						-- if the item's extension is one in the list, download the item
						repeat with j from 1 to (count items in extensionList)
							if ((item 2 of nameAndExtension) = (item j of extensionList)) then
								download item currentItem with resume mode replace
								delete remote item currentItem
						                exit repeat
							end if
							
						end repeat
					end repeat
				else
					display dialog ("Could not connect to server.")
				end if
			end tell
		end tell
	end if
	
end clicked
on awake from nib theObject
end awake from nib

Model: Powerbook G4
AppleScript: Current
Browser: Safari 522.12.1
Operating System: Mac OS X (10.4)