Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Oct 18, 2012, 07:09 AM   #1
amityweb
macrumors newbie
 
Join Date: Oct 2010
Location: Wales
Applescript to move emails to Omnifocus keeps adding/duplicating the task

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 by amityweb; Oct 25, 2012 at 10:10 AM.
amityweb is offline   0 Reply With Quote
Old Oct 18, 2012, 08:41 PM   #2
Red Menace
macrumors regular
 
Join Date: May 2011
Location: Littleton, 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.
__________________
MacBook Pro / OS X Mavericks (10.9.4) / Xcode 5.1.1 / RubyMotion FTW
Red Menace is offline   0 Reply With Quote
Old Oct 18, 2012, 08:52 PM   #3
chown33
macrumors 603
 
Join Date: Aug 2009
You're asking us to guess about code you haven't posted or identified. Post your code, and post the URL of the original you started with.

The answer is "It depends". One thing it depends on is whether it's a simple variable or a property. See here:
http://www.macobserver.com/tips/appl...20020529.shtml

Google search terms: applescript properties
chown33 is offline   0 Reply With Quote
Old Oct 19, 2012, 03:42 AM   #4
amityweb
Thread Starter
macrumors newbie
 
Join Date: Oct 2010
Location: 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/Om...us.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 is offline   0 Reply With Quote
Old Oct 19, 2012, 10:07 AM   #5
amityweb
Thread Starter
macrumors newbie
 
Join Date: Oct 2010
Location: 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!
Attached Thumbnails
Click image for larger version

Name:	omnifocus.png
Views:	20
Size:	92.3 KB
ID:	370605  
amityweb is offline   0 Reply With Quote
Old Oct 19, 2012, 10:12 AM   #6
amityweb
Thread Starter
macrumors newbie
 
Join Date: Oct 2010
Location: 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.
amityweb is offline   0 Reply With Quote
Old Oct 24, 2012, 05:57 PM   #7
see.solve.
macrumors newbie
 
Join Date: Oct 2012
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.
see.solve. is offline   0 Reply With Quote
Old Oct 25, 2012, 03:15 AM   #8
amityweb
Thread Starter
macrumors newbie
 
Join Date: Oct 2010
Location: 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 is offline   0 Reply With Quote
Old Oct 25, 2012, 08:50 AM   #9
amityweb
Thread Starter
macrumors newbie
 
Join Date: Oct 2010
Location: 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 is offline   0 Reply With Quote
Old Oct 25, 2012, 10:06 AM   #10
amityweb
Thread Starter
macrumors newbie
 
Join Date: Oct 2010
Location: 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 by amityweb; Oct 25, 2012 at 10:11 AM.
amityweb is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Error when duplicating folders to shared drive in AppleScript tekboi Mac Programming 10 Mar 18, 2014 10:04 AM
Mba osx: How to "move" files in finder without it duplicating? ET iPhone Home MacBook Air 24 Feb 19, 2014 03:35 PM
Move emails from apple mail to windows outlook SteNash MacBook Pro 15 Oct 21, 2013 05:07 AM
can't move gmail emails out of inbox gregoria OS X 10.8 Mountain Lion 2 Dec 26, 2012 01:34 AM
Best way to move emails between Macs? Doc69 OS X 0 Jun 11, 2012 05:59 PM

Forum Jump

All times are GMT -5. The time now is 10:06 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC