OmniFocus add document

Lost me, I’m afraid. I do embed the file in the OmniFocus action.

I see, I think.

For my purposes though I’m archiving a copy of the file elsewhere via a Hazel rule and am having OmniFocus embed the file so that I can have it on my various satellite devices also running OmniFocus. I agree that if all I was doing was working locally with the projects then not embedding the file would be by far the better choice.

As it stands now, before the file ever gets to the to OmniFocus folder it is filed away permanently. I then have a perspective setup which is linked to a recurring routine whereby I actually delete the completed actions that have the embedded files in them so that I don’t bog down my OF database.

Quick question, I hate to bother you again after all the effort you’ve put in, but is there any way that this could be modified to run as a stand alone script? I’m having a lot of trouble again with Folder Actions just not working and am hoping to incorporate the script into a Hazel rule.

Many many thanks.

I surmise, you’d like to select one file in the frontmost finder window, and then run the script, or would you prefer to choose a file through a dialog box? :slight_smile:

I am sorry that folder actions doesn’t work for you. You can increase the priorites of them, by using the renice command, to make them more responsive, but that is nothing I am doing per post here, so you’d have to read up on both renice an launch agents, to make that hack work.

Not sure about the answer to your question. I’m planning on having Hazel take the file from a folder and run the script once Hazel detects a file, or files, added to the folder. I know that Hazel always expects “end” rather than “property” to being the script if that helps at all.

I am not sure what you mean, I’ll make a script for you that lets you choose a file from a folder, (the folder being static), and then pass it over to another script, that takes the file as a parameter, (so that you can use the same convention, (hopefully) from Hazel.

Either tonight, or tomorrow, but not right now. :slight_smile:

I’m having fever today, I’ll do it asap as soon as the fever have sunk. Sorry :expressionless:

Hello. There should be a lot to remove here, but I am not sure how Hazel runs these things, I know for sure that you won’t have any other option than to embed the file into Omnifocus, so I have removed that, I am unsure about the quick entry window however.

I hope you can try it from here, and we’ll modify some as we go along. But I am not the right man for Hazel, as I have never used it.

The last part of the script:

tell AddToOmniFocus to run {aFileList}

Shows how it is to be invoked, if it is embedded in a Hazel script.

If it is to be loaded into a Hazel-AppleScript, then you’d better have a run script (“hfs path” as alias ) with parameters {theList}

The list. Right now, the script fires for a list of aliases, this can be changed so that it fires for individual files, I am not sure what you need.



script AddToOmniFocus
	property pUseQuickEntry : false (* if true, Quick Entry window used and left open
if false, actions added directly to Inbox *)
	property pDefaultProject : "Incoming"
	property pDefaultContext : "MacFolderTest"
	
	on run {added_items}
		try
			repeat with i from 1 to number of items in added_items
				set this_item to item i of added_items
				tell application "Finder"
					set file_name to (name of this_item)
					my logit("item file_name" & file_name, "toOmnifocus")
					set file_cmt to (comment of this_item)
					set comment of this_item to "whatever you like"
					set file_cmt to (comment of this_item)
					my logit("item file_cmt" & file_cmt, "toOmnifocus")
				end tell
				tell application "OmniFocus"
					if (pUseQuickEntry) then
						set theContext to (first flattened context where its name = pDefaultContext as rich text)
						set theProject to (first flattened project where its name = pDefaultProject)
						tell quick entry
							open
							tell theProject
								set NewTask to make new task with properties {name:file_name, context:theContext}
								tell the note of NewTask to make new file attachment with properties {file name:this_item, embedded:true}
							end tell
							activate
						end tell
					else
						tell front document
							set theContext to (first flattened context where its name = pDefaultContext as rich text)
							set theProject to (first flattened project where its name = pDefaultProject)
							tell theProject
								set NewTask to make new task with properties {name:file_name, context:theContext}
								tell the note of NewTask to make new file attachment with properties {file name:this_item, embedded:true}
							end tell
						end tell
					end if
					
				end tell
				# tell application "Finder" to set comment of this_item to file_cmt
				tell application "Finder"
					set file_cmt to (comment of this_item)
					my logit("item file_cmt" & file_cmt, "toOmnifocus")
				end tell
				
			end repeat
		on error e number n
			my logit("Error: " & e & " " & n, "toOmnifocus")
		end try
	end run
	
	to logit(log_string, log_file)
		do shell script ¬
			"echo `date '+%Y-%m-%d %T: '`\"" & log_string & ¬
			"\" >> $HOME/Library/Logs/" & log_file & ".log"
	end logit
end script

tell AddToOmniFocus to run {aFileList}

Edit

I think you will find what you need here: Create OmniFocus Tasks with AppleScript ” MacSparky

Hmm. So complicated… Many many thanks for sticking with me on this.

The following error popped up in Hazel’s error logs when trying to run the script:

NSLocalizedDescription = “The variable aFileList is not defined.”;
NSLocalizedFailureReason = “The variable aFileList is not defined.”;
OSAScriptErrorAppAddressKey = “<NSAppleEventDescriptor: null()>”;
OSAScriptErrorBriefMessageKey = “The variable aFileList is not defined.”;
OSAScriptErrorMessageKey = “The variable aFileList is not defined.”;
OSAScriptErrorNumberKey = “-2753”;
OSAScriptErrorOffendingObjectKey = “<NSAppleEventDescriptor: ‘utxt’("aFileList")>”;
OSAScriptErrorRangeKey = “NSRange: {0, 0}”;

The script compiles in Hazel just fine, but doesn’t embed the file or create an action, etc… If this error is a result of me not finishing the script, I’m afraid I’ll need a little more direction on that front. Sorry, you’re dealing with a nearly incompetent person when it comes to scripting.

On the MacSparky script, yes this was the original inspiration for the request. That script works perfectly except for it not embedding the file into the OmniFocus action. If there was a way to just modify it to embed the file that would work great!

Though really I’m at a point now with the original Folder Action working most of the time, and a new mac on order I’m thinking I can just live with the original, version 7 or so, script you put together. I’m hoping the issue I’m having with Folder Actions sometimes working and sometimes not will be remedied with a fresh install on a new machine.

Many thanks again. Your resilience is impressive.

Buy SSD!

I have never had a problem with a folder action not firing off! Unless I have like 3-4 levels with folder actions or more. As a matter of fact, I have had folder actions to keep Finder windows neatly organized! :slight_smile:

I don’t know why your hazel script doesn’t work, I’d rather address that with the guys behind Hazel.

An alternative solution, would be to have Hazel call a shell script, that contains an OSA script, that does the stuff with OmniFocus.

I must say, that I am opposing the usage of Hazel to accomplish this, this is something that Apple should have made working properly by their folder actions.

You can find the process by peeking into launchd, and reading up, then you can renice the Folder actions process so that it runs with a higher priority.

Apple really gives it all to the GUI, so you won’t really notice any lag if you change the priority of the launchd process that runs the folder actions for the better.

There are some reading involved, and no guarantees.

Edit:

I am sure Hazel is a great program, and the ability to sort things out with rules, like mail, is just marvellous. But having to use Hazel, in order to make something simple, that could have been done with a simple Folder Action, is clearly overkill.

When I ran many Folder Actions I reniced the process, and it worked, without any feeling of lagging. But I do have an SSD disk. Another way to circumvent this, is to make a ram disk, and install the folder action onto the ram disk whenever you boot, That should also help a little for the responsiveness of the Folder Action.

About Folder Actions:

As long as the operation on the items aren’t time critical, one can write the Folder Actions so that they guarantee, that the action is done the next time the folder action is fired.

There are however no guarantee if the folder action totally stops working of course, but it is easy to develop a scheme that sees to that any missed items are processed.

There are issues here, that are not investigated at this time, such as concurrency, reentrancy and so on.

The scheme is to write out a file list somewhere with the contents of the folder each time the folder action has completed its mission.

When the folder action starts, the contents of the folder is checked against that file list, and any items not in the list should be processed, together with the recieved items.

This scheme would involve lock files and such, to avoid the re-entrancy problems, and a script executed at login to remove any such lock files that wasn’t released before the machine was shut down.

This scheme may not be reliable enough to run a nuclear facility, but should be thrusted to work in 99.99% of the occurences, as long as the action on the dropped items aren’t time critical.

Fortunately I’m not currently running a nuclear facility! This sounds complicated but workable. Nothing intensley time sensitive at all.

Is there a script that would allow for the contents of the file to be checked to see if a folder action has been run against it?

I suppose this is what you’re basically describing??

You’ll just have to look and see if something is or has happened here, as it is going to be on my leisure.

I didn’t make any promises, and we are very close to christmas! :slight_smile:

There is one thing I’d like you to do, and that is to inspect your logfiles, for dylib conflicts escpecially. Maybe use Onyx that is free, and clean up everything that can be cleaned, except for your browser history, that you want to keep.

If you do this, and maybe remove stuff that may create conflicts, if you can find what it is, and what Items you don’t need, then maybe your folder actions may trigger correctly.

It may also, so odd it may sound, help to clean the hardware caches.