Remove carriage return

I wrote an applescript that will allow me to email myself a task item and add it to my todo.txt file ala todo.sh. The script works great except that it adds a carriage at the end of my message (theContent). This message up my txt file because it adds a blank line. Can anyone help me figure out how to remove it?

Here’s my script:


(*
	add a mail message to todo.txt
*)
using terms from application "Mail"
	on perform mail action with messages selectedMsgs
		tell application "Mail"
			
			repeat with msg in selectedMsgs
				set theContent to content of msg
				set theSender to extract name from sender of msg
				set theDate to date received of msg
				set toText to "@mail @takeaction @" & theSender & " " & theDate & " @" & theContent
				tell application "Terminal"
					do script "/Volumes/JUMP/todo/todo.sh -d /Volumes/JUMP/todo/.todo add \"" & toText & "\""
				end tell
			end repeat
		end tell
	end perform mail action with messages
end using terms from

Where is the carriage return entered: is it in toText or is it put there by your shell script?

If it’s at the end of toText, then it’s straight-forward to remove:


set T to "Now is the
time for all
good men
to come
to the aid
of the party
" -- note final return places the quotes below the last line so the paragraphs of T look like {"Now is the", "time for all", "good men", "to come", "to the aid", "of the party", ""} -- note the blank line at the end
set newText to paragraphs 1 thru -2 of T -- grab all but the last.

Haven’t figured out btw why your’re using the terminal; can’t this be done with do shell script " etc. "?

Worked like a charm. Thanks :smiley:

btw~~ I use the terminal because I have to run a shell script after I get the correct text to put into it.

I agree with Adam that it would be worth trying the do shell script command, rather than scripting Terminal, lunac.

The following (untested) suggestion also demonstrates a method of directly removing multiple returns from the end of text (rather than coercing to a list and then back to text):

using terms from application "Mail"
	on perform mail action with messages selectedMsgs
		tell application "Mail" to repeat with msg in selectedMsgs
			set theContent to content of msg
			set theSender to extract name from sender of msg
			set theDate to date received of msg
			repeat while (count theContent's last paragraph) is 0
				set theContent to theContent's text 1 thru -2
			end repeat
			set toText to "@mail @takeaction @" & theSender & " " & theDate & " @" & theContent
			do shell script "/Volumes/JUMP/todo/todo.sh -d /Volumes/JUMP/todo/.todo add \"" & toText & "\""
		end repeat
	end perform mail action with messages
end using terms from

repeat while (count theContent's last paragraph) is 0
	set theContent to theContent's text 1 thru -2
end repeat

Nice :smiley:

OH!! I never knew you could just run scripts that way ::world opening up:: That is sooo helpful. I’ve always run it through terminal. Now I have about 15 scripts I can rewrite. And I won’t make that mistake again.

Thanks so much. :lol:

Kai – consider it tested. It works perfectly.

Thanks again to both of you.