Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

amityweb

macrumors member
Original poster
Oct 5, 2010
40
1
Wales
I have an Applescript (modified someones) to move Mail from a certain folder into Omnifocus, then move the mail out of this folder. I set it to run every 5 minutes.

All seemed to work fine until every 5 minutes the same OF task is created, even though the original mail is not in the folder it is checking!

Weird I thought. So I thought it must have been remembering the variables the script set at the first time I run it.

So now I set the variables (the main OF task object) to nothing, and that seems to have fixed it.

If I run the script within the editor direct it did not keep doing this.

So basically I just wanted to confirm if running it using lingon or even the osascript command, means the variables are remembered or something?

The command line I run in longon is:
/usr/bin/osascript /Users/me/Library/Scripts/send-follow-up-to-omnifocus.scpt

Thanks
 
Last edited:

Red Menace

macrumors 6502a
May 29, 2011
578
226
Colorado, USA
In a regular AppleScript, all global variables are saved with the script. The variables will be cleared if/when the script is recompiled.
 

amityweb

macrumors member
Original poster
Oct 5, 2010
40
1
Wales
Sorry, here's the code. It seems to be random though... I have come to work this morning and there are not as many duplicates as before, in fact only a couple (whereas yesterday it was creating about 5 duplicates per email)... but there are a couple nevertheless. Its strange because the email has been moved and is not in the Follow Up folder anymore, so I do not understand why the same email is added again the next time its run. The script is run every 5 minutes.

Firstly thanks to https://github.com/HunterHillegas/O...er/PushFlaggedMessagesToOmniFocus.applescript for the original code.


Code:
on run
	
	tell application "Mail"

		set _msgs_to_capture to (every message of mailbox "Follow Up" of account "Gmail")
		
		repeat with eachMessage in _msgs_to_capture
			
			set theStart to missing value
			set theDue to missing value
			set theOmniTask to missing value
			set theTitle to (subject of eachMessage)
			set theNotes to the content of eachMessage
			
			set theCombinedBody to "message://%3c" & message id of eachMessage & "%3e" & return & return & theNotes
			
			tell application "OmniFocus"
				tell default document
					set newTaskProps to {name:theTitle}
					if theStart is not missing value then set newTaskProps to newTaskProps & {start date:theStart}
					if theDue is not missing value then set newTaskProps to newTaskProps & {due date:theDue}
					if theCombinedBody is not missing value then set newTaskProps to newTaskProps & {note:theCombinedBody}
					
					tell project named "Next Actions"
						set newTask to make new task with properties newTaskProps
					end tell
					
					set context of newTask to context "Work"
					
					
				end tell
			end tell
			
			
			move eachMessage to mailbox "Follow Up Temp" of account "Gmail"
			
			
			
		end repeat
		
	end tell
	
end run
 

amityweb

macrumors member
Original poster
Oct 5, 2010
40
1
Wales
Attached is a screenshot of Omnifocus. These are added every 5 minutes, even though the email is not in the Follow Up folder anymore, not since adding the first one. Very annoying!
 

Attachments

  • omnifocus.png
    omnifocus.png
    92.3 KB · Views: 112

amityweb

macrumors member
Original poster
Oct 5, 2010
40
1
Wales
Dont know if it helps but my email account is GMail over IMAP. So not sure if even though the message looks like its moved, Mail still think its in Follow Up or something.

Also, it is moved to Follow Up Temp, so not sure if the script also reads Follow Up Temp as matching Follow Up, although its not adding any other emails thats in there.

----------

I just found an issue, perhaps this is the reason...

When the mail is moved from "Follow Up" to "Follow Up Temp", it disappears from Mail app in this folder. BUT when I login to GMail, the mail still has the label "Follow Up" in addition to the new label "Follow Up Temp".

So maybe this is a Mail issue, in that even though I cant see it in the folder, it is in fact still associated with it due to it not removing the label from it. In effect Mail is not moving but seems to be copying, then hiding it from the original folder.
 

see.solve.

macrumors newbie
Oct 13, 2012
20
0
Gmail and Apple have a long, sordid history when it comes to labels. I don't use labels and don't have this issue. For those who do, if the advice given in How to make Gmail work well with Mail doesn't help, then search Google. There are other blog posts out there on the subject.
 

amityweb

macrumors member
Original poster
Oct 5, 2010
40
1
Wales
Tell me about it! It's been very difficult to get it working as one would expect with GMail. I actually started to use Postbox because it works much better than Mail with GMail. In fact it's great software and I would have kept that if it weren't for the fact they have poor Applescript support (so the above script wont work) and they do not provide any support at all. So I moved back to Mail.

I have now got a perfect setup for me, working well with GMail, EXCEPT for this Applescript issue!

But I do not know if it is a Gmail or Mail issue. I have a hunch its a Mail issue, because its as though the email is still in the new folder but hidden from my site. Because both Gmail AND Applescript think the email is in the folder still.
 

amityweb

macrumors member
Original poster
Oct 5, 2010
40
1
Wales
Ok I have an update, sure to have nearly fixed it...

When Applescript moves a message to a new folder, its as if the old folder synchronises with the IMAP server and so the new label is updated in GMail. But the old folder it WAS in does not sync. with GMail so GMail still thinks its in there. Apple Mail must have this in its DB somewhere due to AS still adding the mail. Anyway, only when I manually click on the new folder where the mail has been sent to, does that sync. with GMail and remove the old label from it, at which point the message stops being added to Omnifocus, so the message must then also be deleted from the old folder in Mail properly.

So I just need to find a way for AS to "sync" or "get new messages" from the new folder.
 

amityweb

macrumors member
Original poster
Oct 5, 2010
40
1
Wales
I think I have fixed it this time...

I used the following line at the end of my script to what I think selects the folder in Mail, thus simulating me selecting it and updating the GMail label:

Code:
set selected mailboxes of message viewer 1 to {mailbox "Archive"} of account "Gmail"

Fingers crossed!

Here's my full script which checks a folder called "Follow Up" and if any mail is in there adds it to Omnifocus then moves it to the Archive folder.

Code:
on run

	tell application "Mail"
		
		set _msgs_to_capture to (every message of mailbox "Follow Up" of account "GMail")
		
		repeat with eachMessage in _msgs_to_capture
			
			set theStart to missing value
			set theDue to missing value
			set theOmniTask to missing value
			set theTitle to (subject of eachMessage)
			set theNotes to the content of eachMessage
			
			set theCombinedBody to "message://%3c" & message id of eachMessage & "%3e" & return & return & theNotes
			
			tell application "OmniFocus"
				tell default document
					set newTaskProps to {name:theTitle}
					if theStart is not missing value then set newTaskProps to newTaskProps & {start date:theStart}
					if theDue is not missing value then set newTaskProps to newTaskProps & {due date:theDue}
					if theCombinedBody is not missing value then set newTaskProps to newTaskProps & {note:theCombinedBody}
					
					tell project named "Next Actions"
						set newTask to make new task with properties newTaskProps
					end tell
					
					set context of newTask to context "Work"
					
					
				end tell
			end tell
			
			
			move eachMessage to mailbox "Archive" of account "GMail"
			
		end repeat
		
		set selected mailboxes of message viewer 1 to {mailbox "Archive"} of account "GMail"
		
	end tell
	
end run
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.