What am I doing wrong in this repeat loop?

I tried to copmbine two scripts (both working for single files, so a whole folder could be processed as the files are many.


set inputfolder to (choose folder)
tell application "Finder" to set htmlFiles to files of entire contents of inputfolder
tell application "Finder" to set sel to files of entire contents of inputfolder
set destFolder to (choose folder)
repeat with oneFile in sel
	set rtfdFile to oneFile
	set fileName to name of (info for rtfdFile)
	set packageContents to list folder rtfdFile
	
	-- copy RTF file
	if "TXT.rtf" is not in packageContents then
		display alert "No Valid RTFD" message "The chosen file is not a valid RTF file with attachmetns. The 'TXT.rtf'-file was not found."
		return
	end if
	set rtfSource to POSIX path of rtfdFile & "TXT.rtf"
	do shell script "/usr/bin/ditto " & quoted form of rtfSource & space & quoted form of (destFolder & fileName & ".txt")
	-- copy attachments
	set attFolder to destFolder & fileName & " Attachments/"
	repeat with i in packageContents
		if (contents of i) is not "TXT.rtf" then
			do shell script "/usr/bin/ditto " & quoted form of (POSIX path of rtfdFile & i) & space & quoted form of (attFolder & i)
		end if
	end repeat
end repeat

however I always get this error:
Finder got an error: File document file Abc 48 Guangzhou, Guangdong, China.rtfd of folder China of folder Desktop of folder DanwaN of folder Users of startup disk wasn’t found.

How can I fix this repeat loop

Model: Mac Pro, Quad-Core Intel Xeon
Browser: Safari 533.19.4
Operating System: Mac OS X (10.6)

hi
I don’t know if this will help you,this works with excel files in a folder(over a 1000)

repeat with i from 1 to count fileList
	set thisFile to item i of fileList as text
--do something with each file

bills

Maybe this will help… I use it every now and then to process hundreds of files. Note that I try to keep the source folder clean of everything except the files I want to process (i.e. no folders).


tell application "Finder"
	set input_folder to (choose folder)
	set file_list to list folder input_folder without invisibles
	set file_count to count items in file_list
	
	set dest_folder to choose folder
	
	repeat with i from 1 to file_count
		-- do whatever you need
	end repeat
end tell

You got your specifiers mixed up, and one line needs to be redirected.

This line returns Finder references:
tell application “Finder” to set sel to files of entire contents of inputfolder.

This line needs an alias:
set fileName to name of (info for rtfdFile)

This line needs to address the Finder AND must use an alias:
set packageContents to list folder rtfdFile

This works (note safety filter when listing content of input folder):

set inputfolder to (choose folder)

tell application "Finder" to set sel to files of entire contents of inputfolder whose name extension is "rtfd"

repeat with rtfdFile in sel
	set fileName to name of (info for (rtfdFile as alias))
	tell application "Finder" to set packageContents to list folder rtfdFile as alias
end repeat

When getting a reference from Finder an RTFD is ‘document file’, which you cannot list as folder.
When using a reference to an RTFD in alias form it ends with a colon, so ‘list folder’ works.
Run the snippet from the Editor, and look in the Events pane to see this in action.

With the filter (whose…) you actually no longer need to check each file - you have only files of the desired type in the list.
Should be much quicker, too.

Actually, ‘list folder’, like ‘info for’ is a StandardAdditions command, so the Finder ‘tell’ isn’t needed. The Finder’s involved in the coercion, however ” even without the ‘tell’ ” is it owns the reference being coerced. This coercion’s done twice as often as necessary if it’s done individually for each command.

Both ‘info for’ and ‘list folder’ are now deprecated and may cease to work in a future AppleScript version. Since danwan’s using shell scripts and POSIX paths to dissect his RTFD files, it would make sense to use a shell script to get the paths in the first place.

set inputfolder to quoted form of text 1 thru -2 of (POSIX path of (choose folder))
set destFolder to POSIX path of (choose folder)

-- Get the POSIX paths of all the RTFD bundles and their contents.
-- Each bundle (.rtfd) path in the result is followed by its associated attachment paths.
set rtfdPaths to paragraphs of (do shell script ("/usr/bin/find -f " & inputfolder & " \\( -path '*.rtfd*' \\)"))

set astid to AppleScript's text item delimiters
-- Work through the paths in order.
repeat with i from 1 to (count rtfdPaths)
	set thisPath to item i of rtfdPaths
	if (thisPath ends with ".rtfd") then -- This is a bundle path.
		-- Test for the existence of a "TXT.rtf".
		set txtPath to thisPath & "/TXT.rtf"
		set validRTFD to (rtfdPaths contains txtPath)
		if (validRTFD) then -- TXT.rtf exists
			-- Get the name of the bundle.
			set AppleScript's text item delimiters to "/"
			set fileName to text item -1 of thisPath
			-- Set the TIDs to the bundle path to extract the attachment names from their paths.
			set AppleScript's text item delimiters to thisPath & "/"
		else -- No TXT.rtf file.
			display alert "No Valid RTFD" message "The file " & thisPath & " is not a valid RTF file with attachments. The 'TXT.rtf'-file was not found."
		end if
	else if (validRTFD) then -- This is an attachment path in a valid bundle.
		if (thisPath is txtPath) then
			-- Duplicate the TXT.rtf file, with new name, to the destination folder.
			do shell script ("/usr/bin/ditto " & quoted form of txtPath & space & quoted form of (destFolder & fileName & ".txt"))
		else
			-- Duplicate any other attachments to a specially created folder within the destination folder.
			set attachmentName to text item -1 of thisPath
			do shell script ("/usr/bin/ditto " & quoted form of thisPath & space & quoted form of (destFolder & fileName & " Attachments/" & attachmentName))
		end if
	end if
end repeat
set AppleScript's text item delimiters to astid