Help with Moblogs. Sprint, and a simple applescript

I have a sprintpcs telephone.

I have a blosxom blog at timswebpage.com.

When you send photos from the sprint phone, it just sends a preview of the image with a link to see the full sized image.

To create a blog with blosxom, you simply have to create a .txt file and save it in the appropriate spot on the server…

I want to make an applescript that will run in mail whenever i send a photo to my email address, from my phone, that will cut and paste the contents of the email into a text file, and save it in the appropriate spot on my blosxom server.

I’m fairly certain that this will be an easy task…but I’ve never done anything with applescript, so I was hoping someone could point me in the right direction.

the only thing that seems like it could be tricky, would be saving the .txt file with a different filename for each picture so that the old moblog posts don’t get written over.

any help would be greatly appreciated.

Tim

Don’t all jump in at once. =)

Be extremely specific about the process you wish to automate, step by step. There are lots of gaps in your description which need to be filled before someone can give you a good start on this.

Ok, here is the process.

I will create an account called something like moblog@timswebpage.com.

I want to create a rule in apples’ mail program that will run a script on all incoming mail to moblog@timswebpage.com.

The script will need to cut and paste the text from the emails from my telephone and save them in a .txt file.

the name of the .txt file needs to be different for each .txt file that is created.

after the .txt file is created, the script will need to upload it to a specific directory on my webserver.

once its uploaded, it automatically shows up on my blog.

i hope that makes sense.

Much better, yes.

First, I would download an example Mail rule script from Apple’s website or somewhere and use it as a starting point.

Next, your uploaded text file, as you said, must have a unique file name. The easiest way to do this, I think, is to get a date and timestamp to use for the text file name. Here’s a subroutine I use all the time.

on get_datetime_stamp()
	-- Date/Time routine based considerably on one written by Kevin Talbert. Thanks, Kev.
	set todaysDate to (current date)
	set timeString to time string of todaysDate
	set AppleScript's text item delimiters to ":"
	set a to every text item in timeString
	set condTime to ((item 1 of a) & (item 2 of a) & (text 1 through -4 of (item 3 of a)))
	set {m, d, y} to {month, day, year} of todaysDate
	set monthList to {January, February, March, April, May, June, ¬
		July, August, September, October, November, December}
	repeat with i from 1 to 12
		if m = (item i of monthList) then
			set monthString to text -2 thru -1 of ("0" & i)
			exit repeat
		end if
	end repeat
	set dayString to text -2 thru -1 of ("0" & d)
	set yearString to text -2 thru -1 of (y as string)
	set datetime_stamp to monthString & "/" & dayString & "/" & yearString & "-" & condTime
	return datetime_stamp
end get_datetime_stamp

Finally, you should be able to do the web server upload using standard ‘curl’ commands via AppleScript’s ‘do shell script’ command.

This should get you started.

Here is what I have so far.
It’s funny to me that hours of figuring made me end up with only about 10 lines of code.

im at a point where i cant go further because i cant figure out how to get the script to copy the message contents. with the way its written now, it selects all of my messages and copies them…which causes mail to crash=)

any help is greatly appreciated.

ive been using UI browser. its very helpful. are there any other programs that are better for this sort of thing?

here is the code.

tell application "Mail" to activate
-- may not need to activate application

tell application "System Events"
	
	tell application process "Mail"
		
		click menu item "Select All" of menu "Edit" of menu bar item "Edit" of menu bar 1
		click menu item "Copy" of menu "Edit" of menu bar item "Edit" of menu bar 1
	end tell
end tell

ok, i have the code that will use applescript to upload into the correct directory.

it is implemented by attaching it as a folder action script.
it works like a charm.

here it is:


on adding folder items to this_folder after receiving these_items
	
	-- transform alias to posix path that Transmit can use
	set these_items_string to these_items as string
	set the_file to the POSIX path of file these_items_string
	
	-- list categories in yer blog
	-- line break for display purposes
	-- i only have a few sub-cateogories so i don't mind typing them each time
	display dialog ("Post to sub-category? Choices are:
		different
		blosxom
		blog
		categories
		no (for root level)") default answer "no"
	set category to text returned of the result as string
	
	-- set path on server
	if (category is equal to "no") then
		set remote_path to "mainwebsite_html/blosxom/moblog"
	else
		set remote_path to "mainwebsite_html/blosxom/moblog" & category as string
	end if
	
	-- enter password
	-- again line breaks for display
	display dialog ("Connecting to " & remote_path & "?

Enter password:") default answer ""
	set my_password to text returned of the result as string
	-- Transmit happens	
	tell application "Transmit"
		activate
		make new document at before front document
		tell document 1
			if (connect to "[url=ftp://ftp.timswebpage.com]ftp.timswebpage.com[/url]" as user "nope@timswebpage.com" with password "nope") then
				if (set their stuff to remote_path) then
					upload item the_file
				else
					display dialog ("could not set their stuff") buttons {"OK"}
				end if
			else
				display dialog ("could not connect to server") buttons {"OK"}
			end if
		end tell
		close document 1
	end tell
	
end adding folder items to


now, i just need to modify the above to to get it to stop asking me questions, and automatically upload all files to the correct directory. anyone know how to do that? i need it to work without me having to sit here clicking buttons.

once i get this part figured out, i can use the ‘mail script 2.2 archive to plain text’ script to save each email as a text file in the correct folder on my machine.

the main thing im going to have problems with is filtering sprint’s stupid emails to only include my photos and messages, and not their lame ads.

any help with the two above problems would be greatly appreciated.

I just tried the “archive” script that comes with mail scripts 2.2, and i realized that it too requires user input to function…

someone sent me this earlier, and i just need to figure out how to get it to save the messages as a .txt file.

using terms from application "Mail"
	on perform mail action with messages theMessages for rule theRule
		set messageList to {}
		
		tell application "Mail"
			repeat with eachMessage in theMessages
				(* source is the raw Rfc822 formatted message *)
				copy source of eachMessage to end of messageList
			end repeat
			(* at this point you have a 'messageList' of
 messages to do something with (there's no reason you can't process them
 singly
 but my application does some heavyweight stuff
 here so I batch as many as I can so as not to hang the mail client *)
		end tell
	end perform mail action with messages
end using terms from


I hate to beat this into the ground…but i figured out how to hack that script so that it doesnt require any input…

so now all i need is to figure out how to save all emails to a certain address into a .txt file by attaching a script.

someone sent me this recently…but im not sure how to implement it. it looks like it would work with a few minor adjustments.

i would leave my mail program open, and it would check the mail every 5 minutes, so there wouldn’t be any real need to worry about batch processing.

using terms from application "Mail"
	on perform mail action with messages theMessages for rule theRule
		set messageList to {}
		
		tell application "Mail"
			repeat with eachMessage in theMessages
				(* source is the raw Rfc822 formatted message *)
				copy source of eachMessage to end of messageList
			end repeat
			(* at this point you have a 'messageList' of
 messages to do something with (there's no reason you can't process them
 singly
 but my application does some heavyweight stuff
 here so I batch as many as I can so as not to hang the mail client *)
		end tell
	end perform mail action with messages
end using terms from


I’m almost finished with my script.

This script runs in Apple’s mail program whenever mail is sent to a certain mail address.

this script copies the subject and message of an email to the clipboard.
it opens text edit and pastes it in the correct format.

now…i need to implement the script that you gave me above that will save the file with a unique filename.

how do I do that?

if anyone wanted to finish the script for me, i’d be thrilled. i used a program called “UI Browser” to help me write the script…so im sure it could be cleaned up a bit too.

any help is greatly appreciated.

Here is what I have so far:

using terms from application "Mail"
	on perform mail action with messages theMessages for rule theRule
		tell application "Mail"
			
			set theContent to content of eachMessage
			set theSubject to subject of eachMessage
			set Moblog to theContent & theSubject
			set the clipboard to Moblog
			
		end tell
	end perform mail action with messages
end using terms from


tell application "TextEdit" to activate
-- may not need to activate application

tell application "System Events"
	if (system attribute "sysv") < 4144 or UI elements enabled then
		tell application process "TextEdit"
			click menu item "New" of menu "File" of menu bar item "File" of menu bar 1
			click menu item "Paste" of menu "Edit" of menu bar item "Edit" of menu bar 1
			
		end tell
	else
		tell application "System Preferences"
			activate
			set current pane to pane "com.apple.preference.universalaccess"
			beep
			display dialog "GUI Scripting is not enabled." & return & return & "Check "Enable access for assistive devices" in the Universal Access preference pane (authentication is required), then run this script again." with icon stop buttons {"OK"} default button "OK"
		end tell
	end if
end tell

i thought it was working…but now, it seems like something has gone awry with copying the messages to the clipboard…

let me know if anyone sees any problems in the code. im so confused.