new mail message to link in dock

I am very new to applescript. I can’t make even a script that will alert “hello”; I can’t even make a script run. I do know some actionscript, php, javascript and html. So, anyway, my question is, how do I make a link on the dock that links to a new email that comes into mail? The code should fit into a rule in the mail preferences. The code should activate when the message fits the requirements. Remember that when replying, plese reply in full; i.e. include the tags or whatever that make the script run. :confused:

Well here is a start.

This is an extract to use to test and can be wrapped in a mail rule later.
select a message and run the script.

So the task now would be how to create an alias in the dock from the path to the message file that the script finds.

tell application "Mail"
	
	set selectionMessage to selection
	
	set thisMessage to item 1 of selectionMessage
	
	set mailboxName to "/" & name of (mailbox of (thisMessage))
	set theAccountName to the account of (mailbox of (thisMessage))
	set theAccountacc to account directory of theAccountName
	set msgID to id of thisMessage
	set mailboxtype to account type of the account of (mailbox of (thisMessage))
	if mailboxtype is equal to Mac then
		set mboxtype to ".imapmbox/Messages/"
	else if mailboxtype is equal to pop then
		set mboxtype to ".mbox/Messages/"
	end if
	set URLPath to (theAccountacc & mailboxName & mboxtype & msgID & ".emlx")
	--tell application "System Events" to set theUrl to URL of alias (URLPath)
end tell

Hi.

Thanks, Mark, I have been trying to do something similar, without much success so far. With your script I’m inching closer (I hope.)

I started by trying to make URLPath an alias, and giving the alias a name. I only got the last half of the path as an alias so far:

tell application "Mail"

	set thisMessage to item 1 of (get selection)
	
	set msgID to id of thisMessage
	set msgName to msgID & ".emlx"
	tell thisMessage
		set theSubject to "_" & subject
	end tell
	set aliasName to msgID & theSubject
	
	set theAccountacc to (account directory of the account of (mailbox of (thisMessage)))
	set mailboxtype to account type of the account of (mailbox of (thisMessage))
	
	
	if mailboxtype is equal to Mac then
		set mboxtype to ".imapmbox:Messages:"
	else if mailboxtype is equal to pop then
		set mboxtype to ".mbox:Messages:"
	end if
	
	set thePath to (theAccountacc & ":" & name of (mailbox of (thisMessage)) & mboxtype & msgName)
	
end tell

--tell application "Finder"
--make new alias to file msgID at thePath with properties {name:aliasName}
--end tell


But then I noticed that mailboxes “on my Mac” weren’t seen by the script, so I started working on that:

tell application "Mail"
	
	set onMyMac to mailboxes
	repeat with myAccount in accounts
		set offMyMac to (mailboxes of myAccount)
	end repeat
	
	set selectionMessage to selection
	set thisMessage to item 1 of selectionMessage
	set mailboxName to (mailbox of (thisMessage)) as list
	
	if mailboxName is in onMyMac then
		set mboxtype to ".mbox:Messages:"
	else
		if mailboxName is in offMyMac then
			set mboxtype to ".imapmbox:Messages:"
		end if
	end if
	
end tell

Hopefully I am heading in the right direction. Any suggestions would be appreciated.

Thanks

j


tell application "Mail"
	
	set selectionMessage to selection
	
	set thisMessage to item 1 of selectionMessage
	
	set theSubject to subject of thisMessage
	
	set mailboxName to "/" & name of (mailbox of (thisMessage))
	set theAccountName to the account of (mailbox of (thisMessage))
	set theAccountacc to account directory of theAccountName
	set msgID to id of thisMessage
	set mailboxtype to account type of the account of (mailbox of (thisMessage))
	if mailboxtype is equal to Mac then
		set mboxtype to ".imapmbox/Messages/"
	else if mailboxtype is equal to pop then
		set mboxtype to ".mbox/Messages/"
	end if
	set URLPath to (theAccountacc & mailboxName & mboxtype & msgID & ".emlx")
	--tell application "System Events" to set theUrl to URL of alias (URLPath)
end tell
set URLPath to POSIX file URLPath

tell application "Finder"
	set theAlias to make new alias file at desktop to URLPath
end tell

I just read about that - as opposed to POSIX path, the mistake that led me astray - in another thread. I knew there must be an easier way. Back to work.

Thanks a lot,

j

I got my script to make an alias with the message subject and ID in its name. It works for mailboxes stored on the computer, too - I don’t have subfolders, so didn’t take them into account. I also lack a pop account.

j

--http://bbs.applescript.net/viewtopic.php?id=17538
tell application "Mail"
	
	set thisMessage to item 1 of (get selection)
	set mailboxName to (mailbox of (thisMessage)) as list
	set msgID to id of thisMessage
	set theFile to (msgID as string) & ".emlx"
	
	tell thisMessage
		set aliasName to subject & "-" & msgID & ".emlx"
	end tell
	
	set onMyMac to mailboxes
	repeat with myAccount in accounts
		set offMyMac to (mailboxes of myAccount)
	end repeat
	
	if mailboxName is in onMyMac then
		set mboxtype to ".mbox:Messages:"
		set beginAlias to "Macintosh HD:Users:mylogin:Library:Mail:Mailboxes"
	else
		if mailboxName is in offMyMac then
			set mboxtype to ".imapmbox:Messages:"
			set beginAlias to "Macintosh HD:Users:mylogin:Library:Mail:Mac-mymailname"
		end if
	end if
	
	set endAlias to (":" & name of (mailbox of (thisMessage)) & mboxtype & msgID & ".emlx")
	set aliasPath to beginAlias & endAlias
	
end tell

tell application "Finder"
	set theAlias to make new alias file at desktop to aliasPath with properties {name:aliasName}
end tell

Thank you everyone! I appreciate your ideas! Now, there’s just two things left to do:

  1. Instead of “selected message” I need the message in which fits the rules set in Mail (see link below)

Picture

  1. Put the alias into the dock

So far, all ideas have worked like clock work!

P.S. How do you turn the [img] tag on?

Give this a go:

property theSender : "me@example.com"

tell application "Mail"
	set theMailbox to first mailbox of first account whose name is "INBOX"
	set theMessage to first message of theMailbox whose sender contains theSender and read status is false
	try
		open theMessage
		activate
	on error
		beep
		return
	end try
end tell

Is that like what you want?

Oh, and try this:

display dialog "Hello!" buttons "OK" default button 1

Ok using a bit off this thread http://bbs.applescript.net/viewtopic.php?id=13548

I can get it into the Dock. with the subject as its name.
Its using a alias thats created in the /Users/username/Library/Caches/TemporaryItems folder. that may be a problem when the /Users/username/Library/Caches/TemporaryItems folder is cleared. The dock link will not find the original.

As to getting this working in a Mail rule. Thats not that hard really, but this is the best way of testing first.

tell application "Mail"
	
	set selectionMessage to selection
	
	set thisMessage to item 1 of selectionMessage
	
	set theSubject to subject of thisMessage
	
	set mailboxName to "/" & name of (mailbox of (thisMessage))
	set theAccountName to the account of (mailbox of (thisMessage))
	set theAccountacc to account directory of theAccountName
	set msgID to id of thisMessage
	set mailboxtype to account type of the account of (mailbox of (thisMessage))
	if mailboxtype is equal to Mac then
		set mboxtype to ".imapmbox/Messages/"
	else if mailboxtype is equal to pop then
		set mboxtype to ".mbox/Messages/"
	end if
	set URLPath to (theAccountacc & mailboxName & mboxtype & msgID & ".emlx")
	--tell application "System Events" to set theUrl to URL of alias (URLPath)
end tell
set URLPath to POSIX file URLPath as list

tell application "Finder"
	set the_temp to path to temporary items folder from user domain
	set theAlias to make new alias file at the_temp to URLPath with properties {name:theSubject}
end tell
set theAlias to theAlias as alias
set theAlias to theAlias as list
repeat with i from 1 to the count of theAlias
	set this_file to POSIX path of (item i of theAlias)
	
	set this_file to do shell script "echo " & quoted form of this_file & "|sed 's/\\ /\\\\ /g'"
	do shell script "defaults write com.apple.dock persistent-others -array-add '<dict><key>tile-data</key><dict><key>file-data</key><dict><key>_CFURLString</key><string>" & quoted form of this_file & "</string><key>_CFURLStringType</key><integer>0</integer></dict></dict></dict>'"
end repeat


tell application "Dock"
	quit
	repeat
		try
			activate
			exit repeat
		end try
	end repeat
end tell


Hi Mark.

I just tried your latest script. It stopped at this line

set theAlias to make new alias file at the_temp to URLPath with properties {name:theSubject}

with this error

The event log says

This happened intermittently with the previous version of your script (post #4,) but that one is working again after a fresh download. I was originally going to post a question about that (after spendind a couple of hours trying to figure it out on my own.)

My script (post #6) was working as described, but now it just works for mboxes on my mac, not for imapmboxes - same Finder error as above. The event log for running it with mboxes looks identical to the one for imapboxes, except for the “Finder got an error: The operation could not be completed.”

I’m baffled, what am I missing?

Thanks for all you help.

j

yer It was throwing me after I posted it.
It seems to be mail.app and the way it registers the mail messages.
If I quit and re launch mail. the app will work again.

I was working on one that would not need the alias file, but although I am very close.
It works on a copy of the plist but when you use it on the real one,
It seems the strings in the com.dock.plist for the name of the file. get over written at some point to reflect the real name of the file.
Grrrr…

here is what I have so far.

Back up you plist first before you run this.

tell application "Mail"
	
	set selectionMessage to selection
	
	set thisMessage to item 1 of selectionMessage
	
	set theSubject to subject of thisMessage
	
	set mailboxName to "/" & name of (mailbox of (thisMessage))
	set theAccountName to the account of (mailbox of (thisMessage))
	set theAccountacc to account directory of theAccountName
	set msgID to id of thisMessage
	set mailboxtype to account type of the account of (mailbox of (thisMessage))
	if mailboxtype is equal to Mac then
		set mboxtype to ".imapmbox/Messages/"
	else if mailboxtype is equal to pop then
		set mboxtype to ".mbox/Messages/"
	end if
	set URLPath to (theAccountacc & mailboxName & mboxtype & msgID & ".emlx")
	--tell application "System Events" to set theUrl to URL of alias (URLPath)
end tell
set URLPath to POSIX file URLPath as list

--tell application "Finder"
--set the_temp to path to temporary items folder from user domain
--set theAlias to make new alias file at the_temp to URLPath with properties {name:theSubject}
--end tell
--set theAlias to theAlias as alias
--set theAlias to theAlias as list
repeat with i from 1 to the count of URLPath
	set this_file to POSIX path of (item i of URLPath)
	set theSubject to do shell script "echo " & quoted form of theSubject & "|sed 's/\\ /\\\\ /g'"
	set this_file to do shell script "echo " & quoted form of this_file & "|sed 's/\\ /\\\\ /g'"
	do shell script "defaults write  com.apple.dock persistent-others -array-add '<dict><key>tile-data</key><dict><key>file-label</key><string>" & quoted form of theSubject & "</string><key>file-data</key><dict><key>_CFURLString</key><string>" & quoted form of this_file & "</string><key>_CFURLStringType</key><integer>0</integer></dict></dict></dict>'"
	
end repeat

Thanks Jacque - you always seem to save the day for me.

I was testing on the most recent e-mail, which ended up being something like “Reply to topic: ‘new mail message to link in dock’”

I retested my script and as long as I don’t select a message with “:” in it’s name it works fine - I’ll have to learn how to replace characters next. I read about in a similar thread but forgot about it until now:

And thanks, Mark, you are teaching me a lot as well.

j

I resolved the subject name problem, but I had also been thinking about how to make the script work for all kinds of accounts and mailboxes. I also experimented with duplication (as you’ll see by the coomented lines of my latest effort) just so I could do that if I ever wanted. I’ll leave the dock issue to the more capable Mark.


tell application "Mail"
	
	set thisMessage to item 1 of (get selection)
	set msgID to id of thisMessage as string
	set theFile to msgID & ".emlx"
	tell thisMessage
		set aliasName to subject & "-" & msgID & ".emlx"
		--set duplicateName to subject & "-" & msgID & "copy.emlx"
	end tell
	
	set TID to AppleScript's text item delimiters
	set AppleScript's text item delimiters to ":"
	set newName to (text items of aliasName)
	--set newName to (text items of duplicateName)
	set AppleScript's text item delimiters to "-"
	set aliasName to newName as string
	--set duplicateName to newName as string
	set AppleScript's text item delimiters to TID
	
end tell

set thePath to (do shell script " /usr/bin/find ~/Library/Mail -name " & theFile & " -print")
set thePath to POSIX file thePath

tell application "Finder"
	set theAlias to make new alias file at desktop to thePath with properties {name:aliasName}
	
	--duplicate thePath to alias "Macintosh HD:Users:J:Desktop:" without replacing
	--set name of file theFile of desktop to duplicateName
end tell

Thanks everyone, but, the new message isn’t going to be “selected”.

I don’t know if I’m being clear enough.

In mail, you can go to preferences and make rules. For one of the “perform the following actions” options, you can sellect to run an applescript. I want that actionscript to take the message that activated the rule to the dock.


tell application "Mail"
	
	set selectionMessage to selection
	
	set thisMessage to item 1 of selectionMessage
	
	set theSubject to subject of thisMessage
	
	set mailboxName to "/" & name of (mailbox of (thisMessage))
	set theAccountName to the account of (mailbox of (thisMessage))
	set theAccountacc to account directory of theAccountName
	set msgID to id of thisMessage
	set mailboxtype to account type of the account of (mailbox of (thisMessage))
	if mailboxtype is equal to Mac then
		set mboxtype to ".imapmbox/Messages/"
	else if mailboxtype is equal to pop then
		set mboxtype to ".mbox/Messages/"
	end if
	set URLPath to (theAccountacc & mailboxName & mboxtype & msgID & ".emlx")
end tell

tell application "Finder"
	
	set item_path to URLPath
	--try
	set item_path to POSIX path of item_path
	--end try
	--try
	tell application "Dock" to quit
	--end try
	do shell script "defaults write com.apple.dock persistent-others -array-add '<dict><key>tile-data</key><dict><key>file-data</key><dict><key>_CFURLString</key><string>" & item_path & "</string><key>_CFURLStringType</key><integer>0</integer></dict></dict></dict>'"
	
	
end tell

I changed the dock script a little, and this takes the selected message and puts it in the dock. Three things though:

-As said above, I need the message that activales the rule rather than the selected message

-All of the minimised windows open when the script runs!

-When the script runs, Dashboard quits! That is absolutely uacceptable, as for all of my open widgets take up to 30 seconds to load!

Are these issues fixable?

Yes you are, but as I keep saying. Setting that up comes later, Doing the select message is just to test the script.
Lets worry about that when we thing we have something worth wrapping in a Mail Rule.

Not sure But your right thats a Pain. and would need to be addressed
Something like
get minimised of windows
re launch Dock
apply minimised to windows whose minimised was true.

Hi.

Wow, thanks for that.

DashboardStarter (http://www.bronsonbeta.com/ for anybody who’s interested) quickly opens then closes Dashboard at login to preload widgets. Couldn’t AppleScript do that? Add something like:


tell application "Dashboard" to launch
--then use "System Events" to keystroke the click to close it again

j