How to modify this script?

I have a bunch of rtfd files
this script works perfectly to my needs:

set rtfdFile to (choose file with prompt "Choose the RTFD file.")
set destFolder to (choose folder with prompt "Choose the folder the unpack the RTF")
set packageContents to list folder rtfdFile

-- copy RTF file
set rtfSource to (rtfdFile as text) & "TXT.rtf"
if not fileExistsAtPath(rtfSource) 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
copyFile(rtfSource, destFolder)

-- copy attachments
set attFolder to (destFolder as text) & "RTF Attachments"
if not fileExistsAtPath(attFolder) then mkdir(attFolder)
repeat with i in packageContents
	if (contents of i) is not "TXT.rtf" then
		set attFile to (rtfdFile as text) & i
		copyFile(attFile, attFolder)
	end if
end repeat

however it gives me as a result the following:
TXT.rtf
and
RTF Attachments

how can I get the result wit

ORIGINALFILENAME.rtf
and
ORIGINALFILENAME Attachments?

so I can add a repeat loop

else the script asks permission to overwrite the files
or give a new name

how can I modify it?

Thanks

Hi,

try this


set rtfdFile to (choose file with prompt "Choose the RTFD file." of type "com.apple.rtfd")
set fileName to text 1 thru -6 of name of (info for rtfdFile)
set destFolder to POSIX path of (choose folder with prompt "Choose the folder the unpack the RTF")
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

Thanks very much again

I have an additional problem

All the files I want to process are in one folder

the folder contains several subfolders; each subfolders has one XXX.rtfd file in it.

How to automate the script to get:

1st I choose the folder to process

2nd create automatically a destination file as (Example) “choosen folder destination”

3rd process every file inside a folder named automatically PATHNAME_folder

which will include the

PATHNAME.TXT

and
PATHNAME_folder ATTACHMENTS

I have more than 500 files

I imagine the script could search for the XXX.rtfd file process it and move to the next within the folder if there are more or move to find the next one using the pathdata as folder name and
until all files are processed

can this be donw?

I tried some repeat function but i am not skilled enough and too old to experiment more

Thanks a lot