how do i make this script work in applescript studio

this script works in applescript but not in applescript studio why is that

tell application "Mail"
	check for new mail
	set x to ""
	set numOfMsgs to unread count of inbox
	if numOfMsgs is not 0 then
		set messagesToGet to (messages of inbox whose read status is false)
	end if
end tell

repeat with i from 1 to number of items in messagesToGet
	tell application "Mail"
		set thisMessage to item i of messagesToGet
		set nme to (sender of thisMessage) as string
		set ttl to (subject of thisMessage) as string
		set msg to (content of thisMessage) as string
	end tell
	set mfile to my replace_chars((nme & "_" & ttl), "\"", "")
	set mfile to my replace_chars(mfile, ":", "")
	set mfile to my replace_chars(mfile, ">", "")
	set mfile to my replace_chars(mfile, "<", "")
	
	say ttl & ". from " & nme & ". " & msg saving to mfile & ".aiff"
end repeat

on replace_chars(this_text, search_string, replacement_string)
	set AppleScript's text item delimiters to the search_string
	set the item_list to every text item of this_text
	set AppleScript's text item delimiters to the replacement_string
	set this_text to the item_list as string
	set AppleScript's text item delimiters to ""
	return this_text
end replace_chars

Is that the whole script?

this is my applescript studio script

-- Mail To Audio File.applescript
-- Mail To Audio File

on clicked theObject
	tell application "Mail"
		check for new mail
		set x to ""
		set numOfMsgs to unread count of inbox
		if numOfMsgs is not 0 then
			set messagesToGet to (messages of inbox whose read status is false)
		end if
	end tell
	
	repeat with i from 1 to number of items of messagesToGet
		tell application "Mail"
			set thisMessage to item i of messagesToGet
			set nme to (sender of thisMessage) as string
			set ttl to (subject of thisMessage) as string
			set msg to (content of thisMessage) as string
		end tell
		set mfile to my replace_chars((nme & "_" & ttl), "\"", "")
		set mfile to my replace_chars(mfile, ":", "")
		set mfile to my replace_chars(mfile, ">", "")
		set mfile to my replace_chars(mfile, "<", "")
		
		say ttl & ". from " & nme & ". " & msg saving to mfile & ".aiff"
	end repeat
end clicked

on replace_chars(this_text, search_string, replacement_string)
	set AppleScript's text item delimiters to the search_string
	set the item_list to every text item of this_text
	set AppleScript's text item delimiters to the replacement_string
	set this_text to the item_list as string
	set AppleScript's text item delimiters to ""
	return this_text
end replace_chars

this is the script in applescript

tell application "Mail"
	check for new mail
	set x to ""
	set numOfMsgs to unread count of inbox
	if numOfMsgs is not 0 then
		set messagesToGet to (messages of inbox whose read status is false)
	end if
end tell

repeat with i from 1 to number of items in messagesToGet
	tell application "Mail"
		set thisMessage to item i of messagesToGet
		set nme to (sender of thisMessage) as string
		set ttl to (subject of thisMessage) as string
		set msg to (content of thisMessage) as string
	end tell
	set mfile to my replace_chars((nme & "_" & ttl), "\"", "")
	set mfile to my replace_chars(mfile, ":", "")
	set mfile to my replace_chars(mfile, ">", "")
	set mfile to my replace_chars(mfile, "<", "")
	
	say ttl & ". from " & nme & ". " & msg saving to mfile & ".aiff"
end repeat

on replace_chars(this_text, search_string, replacement_string)
	set AppleScript's text item delimiters to the search_string
	set the item_list to every text item of this_text
	set AppleScript's text item delimiters to the replacement_string
	set this_text to the item_list as string
	set AppleScript's text item delimiters to ""
	return this_text
end replace_chars

i recive this error with applescript studio

sth like this should work:

on clicked theObject
	tell application "Mail"
		check for new mail
		set dest to POSIX path of (choose folder)
		set numOfMsgs to unread count of inbox
		if numOfMsgs is not 0 then
			set messagesToGet to (messages of inbox whose read status is false)
		end if
		repeat with i from 1 to number of items of messagesToGet
			set thisMessage to item i of messagesToGet
			set nme to (sender of thisMessage) as string
			set ttl to (subject of thisMessage) as string
			set msg to (content of thisMessage) as string
			set mfile to my replace_chars((nme & "_" & ttl), "\"", "")
			set mfile to my replace_chars(mfile, ":", "")
			set mfile to my replace_chars(mfile, ">", "")
			set mfile to my replace_chars(mfile, "<", "")
			my saymail(ttl, nme, msg, mfile, dest)
		end repeat
	end tell
end clicked

on replace_chars(this_text, search_string, replacement_string)
	set AppleScript's text item delimiters to the search_string
	set the item_list to every text item of this_text
	set AppleScript's text item delimiters to the replacement_string
	set this_text to the item_list as string
	set AppleScript's text item delimiters to ""
	return this_text
end replace_chars

on saymail(ttl, nme, msg, mfile, dest)
	say ttl & ". from " & nme & ". " & msg saving to ((dest & mfile & ".aiff") as POSIX file)
end saymail

:wink:
D.

Hi,

if no unread messages are detected, the repeat loop is executed anyway.
Maybe AppleScript Studio behaves different.
Try this

tell application "Mail"
	check for new mail
	set numOfMsgs to unread count of inbox
	set messagesToGet to (get messages of inbox whose read status is false)
end tell

-- if numOfMsgs is not 0 then (not needed)
repeat with thisMessage in messagesToGet
	tell application "Mail" to tell thisMessage to set {nme, ttl, msg} to {sender, subject, content}
	set mfile to my replace_chars((nme & "_" & ttl), "\"", "")
	set mfile to my replace_chars(mfile, ":", "")
	set mfile to my replace_chars(mfile, ">", "")
	set mfile to my replace_chars(mfile, "<", "")
	
	say ttl & ". from " & nme & ". " & msg saving to mfile & ".aiff"
end repeat
-- end if

on replace_chars(this_text, search_string, replacement_string)
	set AppleScript's text item delimiters to the search_string
	set the item_list to every text item of this_text
	set AppleScript's text item delimiters to the replacement_string
	set this_text to the item_list as string
	set AppleScript's text item delimiters to ""
	return this_text
end replace_chars

Note: if the list messagesToGet in the repeat loop is empty, the loop will be skipped

thanks i was able to get the script to work form your script