Error message "command was terminated with a non-zero result"

I have a script to export and unzip attachments from Mail. This has worked fine for over a year. Now I get for every email the above error message.

The error message comes from doing the grep:

tell application "Mail"
	set selectedMessages to selection
	try
		repeat with theMessage in selectedMessages
                        set theHeaders to all headers of theMessage
			set replyToHeaderRegex to "Reply-To:.*"
			set quotedHeaders to quoted form of theHeaders
			set Originator to do shell script "echo " & quotedHeaders & " | grep -o " & quoted form of replyToHeaderRegex
		end repeat
	on error errMsg number errNr
		display dialog "Error2: " & errMsg & " Nr.: " & errNr
	end try	
end tell

I need the reply to and not the from. What is going wrong here? I tried to goggle the message but I only found some instance where quoting was required for grep. But I already to that.

macOS High Sierra (yes, I know I have to update this month)

Move your try / on error inside the repeat loop

Scratching head… what is this supposed to solve? I usually have one email selected anyways.

It seems that the header field Reply-To has been changed to Return-Path recently.

You can consider both with this regex

set replyToHeaderRegex to "(?:Reply-To|Return-Path):.*"

The pipe (|) is the Regex OR operator and (?: represents a non-capturing group

@Fredrik71 : I only posted the relevant part of the script. If there is no reply-to then the sender of the email will be used:

if Originator = "" then
	set Originator to sender of theMessage
end if

@StefanK : no, the headers of emails have not changed. The script is for emails sent by my own application to the ticket system. The emails always have a reply-to:

From: someemail@mothsoftware.com
Subject: Error Report
X-Mailer: Moth Software
Reply-To: abc@domain.org
X-SG-EID:
=?us-ascii?Q?CePduXinO1TKWf=2FmbcRcIYklMYFRX=2F78wyWAaMVoDAWvElrBg6lXMZCwIEIVVo?=
=?us-ascii?Q?TULKFNMmQMSmJr8Uk7e7bAqIKCI0UDY7x388zEp?=

In any case a non-zero result means that an error occurred in the shell script line for example there is no match for the given regex.

@Fredrik71 The non-zero termination result doesn’t mean necessarily that “1” is returned. The integer result – which is hidden from AppleScript – indicates that an error occurred, the value represents the POSIX error code (0 is success). At the same time something else can be returned vie stdout or stderr, the two output channels of the shell. This is the actual return value of do shell script which – as you correctly stated - is always a string.

Thanks, guys. I learned quite a bit. Bad developer - I didn’t do enough testing with the script.

I did a try/on error to get the error:

set theHeaders to "test"
set replyToHeaderRegex to "Reply-To:.*"
set quotedHeaders to quoted form of theHeaders
try
	set Originator to do shell script "echo " & quotedHeaders & " | grep -o " & quoted form of replyToHeaderRegex
on error number 1
	display dialog "no grep match"
end try

But this doesn’t explain why - for the same email - the script works fine on Sonoma but not on High Sierra. Or I just give up on my beloved and creaky High Sierra and update the computer to Crapolina or even BS.

There was a report at Late Nite Software’s forum about Apple changing some spaces for displaying dates to a different Unicode character. They might have done the same ‘clever’ thing to the dash in Reply-to.

Yes, that is a new “feature” on Sonoma. But not for the raw data of emails on High Sierra.

Sometimes it’s better to do the searches in AppleScript rather then send it out to the command line. (due to overhead)

try this. I timed it in script geek and it was faster (go figure)

tell application "Mail"
	set selectedMessages to selection
	set replyToHeaderRegex to "Reply-To:" --"Reply-To:.*"
	try
		repeat with theMessage in selectedMessages
			set theHeaders to all headers of theMessage
			set Originator to my getHeader(theHeaders, replyToHeaderRegex)
		end repeat
	on error errMsg number errNr
		display alert "Error2: " & errMsg & " Nr.: " & errNr
	end try
end tell

on getHeader(header, findText)
	local headerLines, foundtext
	set headerLines to paragraphs of header
	set foundtext to {}
	repeat with aline in headerLines
		set aline to contents of aline
		if aline starts with findText then set end of foundtext to aline
	end repeat
	return foundtext as text
end getHeader

Thanks! Text processing in AppleScript is so very clunky.

I finally found out why the original script failed. For testing my software I had changed the language of the computer from English to German. The header keywords were translated with an Antworten an instead of a Reply-To. Because I do emails every day I didn’t think to check the header. Feeling stupid.

At least I learned a lot!