Mail Folder Export as a Text File

Hey,

I know I’m biting off way more than I can chew with this one but I think I may be getting close anyway.
I have a folder in my Mail app that contains RSS feed emails called “CNET News”

What I’m after is a way to export just the titles of those emails to a single text file.
Here’s what I’ve got so far. Hoping for some help with if this is even possible or where my
script needs help.

Many thanks,

Carl

tell application "Finder"
	if (the folder "Temp Export Folder" exists) then set FolderLoc to (the folder "Temp Export Folder")
	if not (the folder "Temp Export Folder" exists) then
		set FolderLoc to (make new folder at (path to desktop folder) with properties {name:"Temp Export Folder"})
	end if
	set SaveLoc to FolderLoc as string
end tell

---APPLE MAIL ITEM SELECTION
tell application "Mail"
	try
		if (selection is not {}) then
			set theMessages to (Need this to be the contents of my RSS folder)
			set msgnr to 1
			repeat with thisMessage in theMessages
				set myTitle to the subject of thisMessage
								
				---CLEAN TITLE FOR FILE
				set previousDelimiter to AppleScript's text item delimiters
				set potentialName to myTitle
				set legalName to {}
				set illegalCharacters to {".", ",", "/", ":", "[", "]"}
				repeat with thisCharacter in the characters of potentialName
					set thisCharacter to thisCharacter as text
					if thisCharacter is not in illegalCharacters then
						set the end of legalName to thisCharacter
					end if
				end repeat
				
				set AppleScript's text item delimiters to ""
				
				if length of legalName is greater than 32 then
					set legalName to items 1 thru 32 of legalName as text
				else
					set legalName to legalName as text
				end if
				
				set AppleScript's text item delimiters to previousDelimiter
				set theFileName to legalName
				
				(*EXPORT TO TXT FILE *)
				set theText to myTitle
				set theFilePath to (SaveLoc & msgnr & "_" & theFileName & ".txt")
				set msgnr to msgnr + 1
				set theFileReference to open for access theFilePath with write permission
				write theText to theFileReference as «class utf8»
				close access theFileReference
			end repeat
			

Were you looking for one file with all of the names of a file for each of the names?

Works fantastic on mailboxes but can’t get it to process an RSS folder.
Is that possible?

Thanks,

Carl

Just one text file that contains just the titles of the RSS feeds.

Has the code that was posted disappeared?

Carl

I read your description too quickly and wrote something for mailboxes. I will try to get this to work for RSS too.

set TID to AppleScript's text item delimiters
set endTitles to {}

set FolderLocdo to quoted form of (POSIX path of (path to desktop) & "Temp Export Folder" as text)
do shell script "mkdir -p " & FolderLocdo

tell application "Mail"
	tell mailbox "yourMailbox" of account 1
		set theTitles to subject of it's message
	end tell
end tell

repeat with myTitle in theTitles
	set AppleScript's text item delimiters to {".", ",", "/", ":", "[", "]"}
	set theTitle to text items of myTitle
	set AppleScript's text item delimiters to {""}
	set theTitle to theTitle as text
	if (count of characters of theTitle) > 32 then set theTitle to text 1 thru 32 of theTitle
	set end of endTitles to theTitle
end repeat

set text item delimiters to return
set endTitles to endTitles as text
set AppleScript's text item delimiters to TID

do shell script "echo " & quoted form of endTitles & " > " & FolderLocdo & "/test.txt"

Thanks a bunch. From what I can gather applescript can’t access the RSS folders directly.
Maybe move the RSS files to a standard mailbox then process?

Carl

This works for an email but not with the RSS


tell application "Mail"
	set xxx to selection
	properties of (first item of xxx)
end tell

You can always try to use Yahoo’s YQL. However, I think this solution will only work for feeds going forward.
http://developer.yahoo.com/yql/console/#h=select%20title%20from%20rss%20where%20url%3D"http%3A//macscripter.net/extern.php%3Faction%3Dnew%26type%3DRSS"

Plug THE REST QUERY into a curl command.

set TID to AppleScript's text item delimiters
set {startTitles, endTitles} to {{}, {}}

set FolderLocdo to quoted form of (POSIX path of (path to desktop) & "Temp Export Folder" as text)
do shell script "mkdir -p " & FolderLocdo

set xxx to do shell script "curl [url=http://query.yahooapis.com/v1/public/yql?q=select%20title%20from%20rss%20where%20url%3D%22http%3A%2F%2Fmacscripter.net%2Fextern.php%3Faction%3Dnew%26type%3DRSS%22]http://query.yahooapis.com/v1/public/yql?q=select%20title%20from%20rss%20where%20url%3D%22http%3A%2F%2Fmacscripter.net%2Fextern.php%3Faction%3Dnew%26type%3DRSS%22"[/url]

set AppleScript's text item delimiters to {"<item><title>", "</title></item>"}
set xxx to text items 2 thru -2 of xxx
repeat with aTitle in xxx
	if aTitle's contents is not "" then set end of startTitles to aTitle's contents
end repeat

repeat with myTitle in startTitles
	set AppleScript's text item delimiters to {".", ",", "/", ":", "[", "]"}
	set theTitle to text items of myTitle
	set AppleScript's text item delimiters to {""}
	set theTitle to theTitle as text
	if (count of characters of theTitle) > 32 then set theTitle to text 1 thru 32 of theTitle
	set end of endTitles to theTitle
end repeat

set text item delimiters to return
set endTitles to endTitles as text
set AppleScript's text item delimiters to TID

do shell script "echo " & quoted form of endTitles & " > " & FolderLocdo & "/test.txt"


Sorry, lost me on that one. Can that script be modified for a specific feed such as the top news?

Thanks,

Carl

Yes, follow the link and enter the URL in the YOUR YQL STATEMENT window. Click test and then copy THE REST QUERY into the curl command.

I like this solution much better. Select your RSS feed before you run the script.

set TID to AppleScript's text item delimiters
set {startTitles, endTitles} to {{}, {}}

set FolderLocdo to quoted form of (POSIX path of (path to desktop) & "Temp Export Folder" as text)
do shell script "mkdir -p " & FolderLocdo

activate application "Mail"
tell application "System Events"
	tell process "Mail"
		try
			repeat with i from 1 to 10000
				set end of startTitles to (get value of text field 1 of row i of table 1 of scroll area 1 of splitter group 2 of splitter group 1 of window 1)
			end repeat
		end try
	end tell
end tell

repeat with myTitle in startTitles
	set AppleScript's text item delimiters to {".", ",", "/", ":", "[", "]"}
	set theTitle to text items of myTitle
	set AppleScript's text item delimiters to {""}
	set theTitle to theTitle as text
	if (count of characters of theTitle) > 32 then set theTitle to text 1 thru 32 of theTitle
	set end of endTitles to theTitle
end repeat

set text item delimiters to return
set endTitles to endTitles as text
set AppleScript's text item delimiters to TID

do shell script "echo " & quoted form of endTitles & " > " & FolderLocdo & "/test.txt"

Sorry, where in the script would I select a feed to enter?

Thanks,

Carl

Again, I think the last script I posted that use UI Scripting is the better solution.

However if you want to try the script that uses Yahoo’s YQL:
Go to this website http://developer.yahoo.com/yql/console/#h=select%20title%20from%20rss%20where%20url%3D"http%3A//macscripter.net/extern.php%3Faction%3Dnew%26type%3DRSS"

Enter your RSS URL where I put the placeholder MacScripter URL.

Click the test button and enter THE REST QUERY into the AppleScript on this line:

set xxx to do shell script "curl [url=http://query.yahooapis.com/v1/public/yql?q=select%20title%20from%20rss%20where%20url%3D%22http%3A%2F%2Fmacscripter.net%2Fextern.php%3Faction%3Dnew%26type%3DRSS%22]http://query.yahooapis.com/v1/public/yql?q=select%20title%20from%20rss%20where%20url%3D%22http%3A%2F%2Fmacscripter.net%2Fextern.php%3Faction%3Dnew%26type%3DRSS%22"[/url]

I got the prior script working using an NPR news feed but the text that gets saved is cutoff.

“The Man Who Painted Sport, Brigh
Hatch Wins Utah Primary; Rangel
From Our Readers: Orbitz Forgot
Nora Ephron, Filmmaker, Author,
Future Murky For Arizona’s Immig
‘Guardian’ Publishes More Allega
BCS Presidents Approve Four-Team
Dementia Complicates Romance In
Gas Prices Fall To 6-Month Low N
Romney Tries To Turn The Outsour
University Of Virginia Reinstate
Egyptian Court Overturns Militar
Sinking Under A $10,000 Monthly
Ready, Set, Sail: America’s Cup”

Any fix for that?

Thanks a bunch,

Carl

I thought you wanted a 32-character limit for the titles.

delete or comment out this line:

if (count of characters of theTitle) > 32 then set theTitle to text 1 thru 32 of theTitle

Just experimenting i changed the 32 to 64 and got:

The Man Who Painted Sport, Bright and Beautiful
Hatch Wins Utah Primary; Rangel Triumphs In NY
From Our Readers: Orbitz Forgot Linux
Nora Ephron, Filmmaker, Author, Dies
Future Murky For Arizona’s Immigration Law
‘Guardian’ Publishes More Allegations Of Collusion In Mexican Pr
BCS Presidents Approve Four-Team College Football Playoff</title
Dementia Complicates Romance In Nursing Homes
Gas Prices Fall To 6-Month Low Nationally; Under $3 In South Car
Romney Tries To Turn The Outsourcing Table On Obama</ite
University Of Virginia Reinstates President, After Public Outcry
Egyptian Court Overturns Military’s Power To Arrest Civilians</t
Sinking Under A $10,000 Monthly Mortgage Payment
Ready, Set, Sail: America’s Cup Back In Rhode Island</it

Still an issue of course. Assuming it’s fixable, is there a way to add a period and a space to the
end of each title? I’m going to have my home automation app read the file
and without the periods it will be one long sentence.

Thanks,

Carl

The UI script does not have that problem. Also, those illegal characters are not included in the Titles of the news. If you want to include them use this:

set TID to AppleScript's text item delimiters
set startTitles to {}

set FolderLocdo to quoted form of (POSIX path of (path to desktop) & "Temp Export Folder" as text)
do shell script "mkdir -p " & FolderLocdo

activate application "Mail"
tell application "System Events"
	tell process "Mail"
		try
			repeat with i from 1 to 10000
				set end of startTitles to (get value of text field 1 of row i of table 1 of scroll area 1 of splitter group 2 of splitter group 1 of window 1)
			end repeat
		end try
	end tell
end tell

set text item delimiters to "." & return
set startTitles to startTitles as text
set AppleScript's text item delimiters to TID

do shell script "echo " & quoted form of startTitles & " > " & FolderLocdo & "/test.txt"

I don’t understand how to use your last script that uses the Mail app. How does it process a specific RSS folder?

I commented the line out you suggested on the other one but am getting the in the saved file:

Sinking Under A $10,000 Monthly Mortgage Payment
Ready, Set, Sail: America’s Cup Back In Rhode Island

Am liking the idea of not using the Mail app.
Thanks,

Carl

You must select an item in the RSS folder before you run it.

For the other script, this should do it:

set TID to AppleScript's text item delimiters
set endTitles to {}

set FolderLocdo to quoted form of (POSIX path of (path to desktop) & "Temp Export Folder" as text)
do shell script "mkdir -p " & FolderLocdo

set xxx to do shell script "curl [url=http://query.yahooapis.com/v1/public/yql?q=select%20title%20from%20rss%20where%20url%3D%22http%3A%2F%2Fmacscripter.net%2Fextern.php%3Faction%3Dnew%26type%3DRSS%22]http://query.yahooapis.com/v1/public/yql?q=select%20title%20from%20rss%20where%20url%3D%22http%3A%2F%2Fmacscripter.net%2Fextern.php%3Faction%3Dnew%26type%3DRSS%22"[/url]

set AppleScript's text item delimiters to {"<item><title>", "</title></item>"}
set xxx to text items 2 thru -2 of xxx
repeat with aTitle in xxx
	if aTitle's contents is not "" then set end of endTitles to aTitle's contents
end repeat

set text item delimiters to "." & return
set endTitles to endTitles as text
set AppleScript's text item delimiters to TID

do shell script "echo " & quoted form of endTitles & " > " & FolderLocdo & "/test.txt"

Well, for some reason it still returns the .

From Our Readers: Orbitz Forgot Linux.
Nora Ephron, Filmmaker, Author, Dies

Obviously, I’m an Applescript noob, but I’m not seeing where the
issue is with that.

Assuming we can lose the is it possible to add a period
and a space to the end of each title?

Thanks,

Carl