OmniFocus Actions Pack for Keyboard Maestro

Using Select Sidebar Element
I'm trying to use "Select Sidebar Element" to create a project at the top of my project list.
This is the list of actions I'm using:

  • select "Projects" in the Menu "Perspective" in Omnifocus
  • Omnifocus "Deselect all sidebar Elements"
  • Omnifocus "Select "Inbox" sidebar Element"
  • select "New Project" in the Menu "File" in Omnifocus

If I don't deselect all, and I had a previously selected project in my Omnifocus window, then the "selection" shows TWO highlighted projects in the sidebar, and the New Project will be created beneath the initial selection. It's like the "Select in Sidebar" action doesn't really select, just highlights and element?
The deselection step feels like a hack. Am I doing something wrong?

Cheers

I don't think you're doing anything wrong. I think (not 100% sure) that OmniFocus changed the way selection works, and I also have to deselect everything before selecting tags or projects to avoid weird behaviour.

Also, the normal behaviour of the plugin is to add the selected element to the previously selected elements. This allows to select a list of tags. Use Deselect to deselect a specific element or, like you did, Deselect all to deselect everything.

1 Like

I tested project names with emojis. I think the problem (which is probably causing victor_c'so problem too) comes from the way Keyboard Maestro passes arguments from actions. I'll have to ask a question to Peter Lewis, only him can confirm if i'm right or not.

So I had confirmation from Peter that it's impossible to pass some characters as an action parameter (not his fault, but Apple's). Therefore I cannot directly fix the action behaviour with emojis. However, if you want to replicate the action, you can simply use the code below in an "execute Applescript" action (keep in mind that because of a bug in OmniFocus, you must have at least one existing task in your project or the script will throw an error).

Remember that everything the actions do can be done directly with AppleScript using the routines available in OmniFocusLibrary.

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
use OmniFocusLibrary : script "OmniFocusLibrary"
set theResult to OmniFocusLibrary's SetFirstSelectedTaskProjectByName("TEST đŸ˜…đŸ€“")

Here is the new version of OmniFocus Action Pack and OmniFocus Library scripts. This is a beta, I just finished coding this a few minutes ago.

There are two new actions:

  • OmniFocus Get Task Tag List
  • OmniFocus Set Task Tag List

Get Task Tag List: get the tags assigned to a task. You can get the tags by Name or by ID. Getting the tags by ID is a great workaround for tags that have special characters.

Set Task Tag List: you can set the tags by Name or by ID, again to work around the problem of special characters in tag names. You can Add, Remove, Replace (all existing tags are replaced by the specified list) or Toggle the tags. Separate the tags with commas. Please try to avoid spaces before and after the commas, I think it could cause issues. For example, this is valid: waiting for,internet,urgent (there is a space IN the tag name, it's fine, but don't do waiting for , internet , urgent (with spaces between tags). I was lazy, I should add a step to trim the tag names.

I hope this solves the needs to manipulate multiple tags, and tags with special characters. Let me know if this works for you!

OmniFocusLibrary V3.zip (483.6 KB) OmniFocus Action Pack V3.zip (1.6 MB)

Thanks for this awesome set of actions, @kornichon!

Here's an idea on how to make the "Defer to Hour" action work with multiple selected tasks:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

-- property OmniFocusLibrary : document "OmniFocusLibrary.scpt" -- use to debug library with Script Debugger
use OmniFocusLibrary : script "OmniFocusLibrary" -- use for production

tell application "Keyboard Maestro Engine"
	set theHour to (system attribute ("KMPARAM_Hour") as text)
end tell
set theHour to theHour as integer

tell application "OmniFocus"
	tell content of first document window of front document
		--Get selection
		set validSelectedItemsList to value of (selected trees where class of its value is not item and class of its value is not folder and class of its value is not tag and class of its value is not perspective)
		set totalItems to count of validSelectedItemsList
		if totalItems is 0 then
			display notification "No valid task(s) selected" with title "Error"
			return
		end if
		
		--Perform action
		set successTot to 0
		set autosave to false
		repeat with thisItem in validSelectedItemsList
			set succeeded to OmniFocusLibrary's SetTaskDeferHour(thisItem, theHour)
			if succeeded = "OK" then set successTot to successTot + 1
		end repeat
		set autosave to true
	end tell
	if successTot > 1 then
		set theResult to true
	else
		set theResult to false
	end if
	return theResult
end tell

This was adapted from this clear dates script and was hastily thrown together. But maybe the action pack could be enhanced by having support for manipulating multiple items?

Here's how it looks: https://imgur.com/yz3yCxD

(The gif shows how the current defer actions works only for one item (I press T to defer them to tomorrow), even if two are selected (again, I press T). The last time I use the enhanced version and both items get deferred to tomorrow (pressed 2).)

Not nearly as advanced as everyone else on the forum, but want to help. AppleScript will allow you to create a project at the top of any folder or project list:

	set theFolder to first flattened folder whose name is myFolder
	
	set theProject to make new project with properties {name:"Test"} at the front of projects of theFolder

Hi, I have updated the OmniFocus Action Pack so that the Defer To Hour and Due On Hour actions use the same method as the other set actions: you can specify if the action applies to the first selected task, all selected task, the last selected task or a task with a specific ID. For former users: I'm not trying to maintain backward compatibility (too much work for a side side project), so new versions of the pack may break older actions, but usually it's to add functionality.

I have also added a new really cool action: OmniFocus Do Actions. It's very useful for recurring tasks with repetitive actions, such as opening a precise url or file.

  • You can add predefined actions in the note of the task, and the action will trigger them.
  • You can define two separate sets of actions: "on start:" and "on complete:"

Example 1. Add the following text to your task's note:

on start:
https://www.linkedin.com/ open
Safari activate
on complete:
Safari quit

Triggering the action "OmniFocus Do Actions" for the On Start set of actions will:

  • open the URL www.linkedin.com
  • activate Safari (and launch it if it wasn't launched)

Triggering the action "OmniFocus Do Actions" for the On Complete set of actions will:

  • Quit Safari

There are many more possibilities, the main available commands are:

  • on start: (with the space and colon) every action below this keyword will be considered an on start action
  • on complete: (with the space and colon) every action below this keyword will be considered an On Complete action
  • activate an application, brings it to the front
  • quit: quit an application
  • run: run an application without bringing it to the front
  • shellscript: executes the shell command in parameter (the script is directly in the parameter)
  • applescript: executes the AppleScript script file (the parameter is a POSIX path to the script).
  • kmmacro: execute the kmmacro, parameter is name or UUID of macro.
  • pause: adds a pause, in seconds in the execution of actions.

More examples.

Open a specific document using a shell script:

  • "open /Users/path/to/file/document.docx" shellscript

Run a macro:

  • "C9790279-AC97-4433-AA42-3493F3939C00" kmmacro
  • "Test script" kmmacro

Run AppleScript:

  • "/path/to/script.scpt" applescript

Pause for 10 seconds:

  • 10 pause

This is very much work in progress, but it seems to work fine for simple actions. The main weakness of the current version is the line parser, which is a quick hack. To know the result of each action, I recommend that you set the result of the action to Display Results in a Window. This will show a line by line summary of the result for each action executed. Have fun!

OmniFocus Action Pack V3-1.zip (280.1 KB) OmniFocusLibrary V3-1.zip (75.0 KB)

4 Likes

@kornichon there seems to be a reference to an "AppleScriptLibrary" that these actions can't find; is there a different script that is supposed to be bundled that isn't? Or perhaps I'm just missing one that should be on my machine?

For example, for the OmniFocus Make Task/Action.scpt:

(*
	OmniFocus Make Task
*)

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

use AppleScriptLibrary : script "AppleScriptLibrary" -- use for production
-- property OmniFocusLibrary : document "OmniFocusLibrary.scpt" -- use to debug library with Script Debugger
use OmniFocusLibrary : script "OmniFocusLibrary" -- use for production

tell application "Keyboard Maestro Engine"
	set theName to (system attribute ("KMPARAM_Name") as text)
	set theNote to (system attribute ("KMPARAM_Note") as text)
	set theProject to (system attribute ("KMPARAM_Project") as text)
	set thePrimaryTag to (system attribute ("KMPARAM_Primary_Tag") as text)
	set theDuration to (system attribute ("KMPARAM_Duration"))
end tell

set theName to AppleScriptLibrary's CorrectSpecialCharacters(theName)
set theNote to AppleScriptLibrary's CorrectSpecialCharacters(theNote)
set theProject to AppleScriptLibrary's CorrectSpecialCharacters(theProject)
set thePrimaryTag to AppleScriptLibrary's CorrectSpecialCharacters(thePrimaryTag)
if theDuration is missing value then
	set nDuration to missing value
else
	set nDuration to theDuration as number
end if

return OmniFocusLibrary's MakeTask(theName, theNote, theProject, thePrimaryTag, nDuration)

I get the following error when it's run:

2020-03-18 19:06:31 /Users/juliant/Library/Application Support/Keyboard Maestro/Keyboard Maestro Actions/Omnifocus Make Task/Action.scpt: execution error: Can’t get script "AppleScriptLibrary". (-1728)

Also I hate to report that even the V3 release doesn't handle projects with emojis in the name (my apologies, I genuinely haven't tested it since you made the update in December, else I would've mentioned that earlier!). FWIW, I did mention that I experienced this issue with projects, and from your update it looks like you only accounted for tags with emojis in the name :grimacing:

Damn, here is the AppleScriptLibrary...
AppleScriptLibrary.scpt.zip (51.0 KB)

Trying to set projects by name with emojis was always going to be a buggy mess, so I now propose two methods: set the project/tag by name or by ID. To avoid problems with special characters, I recommend you use ID in the Project Format, then in the Project field you enter the ID of the project. I'm sorry, I implemented a way to get the ID of a tag, but not a way to get the ID of the project. I will work on correcting that.

1 Like

Try this version of the Action Pack, you should be able to get / set projects by ID, which is a much better solution than trying to work around the emoji/special characters problem. I have also included the latest versions of the AppleScriptLibrary.scpt and OmniFocusLibrary.scpt

Script Libraries V3-2.zip (474.5 KB)
OmniFocus Action Pack V3-2.zip (1.2 MB)

I'm trying to create a macro to create markdown links to selected OmniFocus items. I created the following macro just to get the IDs but I'm getting this error:

execution error: script "OmniFocusLibrary" of «script» doesn’t understand the “GetSelectedTasksIDsList” message

Any ideas what am I doing wrong?

One possibility is that the OmniFocusLibrary script library is not found. Make sure that it is copied in the following folder:
~/Library/Script Libraries/
~ is your home folder, for example if your user name is "toto" you should have a folder on your disk with the following path:
/Users/toto/Library/Script Libraries/

Uncompress the Script Libraries archive (you should get OmniFocusLibrary.scpt and AppleScriptLibrary.scpt) and move them to /Users/yourusername/Library/Script Libraries/

Screenshot 2020-04-02 at 10h26m47s

Then try again, let me know if it solves the problem.

Update: here is a complete solution to generate functional links to the tasks:

OmniFocus Get IDs of All Selected Tasks gets a list of the IDs of selected tasks in OmniFocus.
The regular expression adds the link element in front of each ID.
(?m) means "apply the search to each line"
^ means beginning of the line
$ means end of the line
. means any character (except newline and some other special chars), * as many characters as possible; .* effectively captures the whole line.
(.*) generates a capture group; the matching text between the parenthesis can be reused in the replace as $1

Thus replacing the search with "omnifocus:///task/$1" will generate links with the list of IDs.

Thanks, unfortunately that didn't help. I had the script files installed as indicated, I downloaded and copied them again but no luck.

Also, I was hoping to be able use a loop to get both the ID and the name of the task to construct a markdown link e.g.

`[OmniFocus Task Title](omnifocus:///task/abc123)`

Is this possible?

Hi, I think you may have an older version of the KB Maestro actions installed. Could you take the latest version of the Action Pack I uploaded a few posts ago, and copy them again to the "Keyboard Maestro Actions" folder in ~/Library/Application Support/Keyboard Maestro?

Your error message is:

execution error: script "OmniFocusLibrary" of «script» doesn’t understand the “GetSelectedTasksIDsList” message

And indeed, GetSelectedTasksIDsList does not exist, the correct function is GetSelectedTasksIDList (without "s" after ID).

Here is the answer for your second question:

Attached is the test macro I created to demonstrate how to do what you want.

99)TEST.kmmacros.zip (1.7 KB)

For some reason not all of the Action Pack items had been copied. Anyway, it's working now, here is the finished action.
Copy items as Markdown links.kmmacros (6.5 KB)

Hi guys

@kornichon: ive extracted all files as in the top post

/Users/zeltak/Library/OmniFocusLibrary.scpt

but still get the erro


2020-04-26 20:06:07 /Users/zeltak/Library/Application Support/Keyboard Maestro/Keyboard Maestro Actions/OmniFocus Get IDs of Selected Tasks/Action.scpt: execution error: Can’t get script "OmniFocusLibrary". (-1728)
 Macro “Copy items as Markdown links” cancelled (while executing OmniFocus Get IDs of All Selected Task(s)).

any clue what im doing wrong? do i need to nest the script in a sub folder?

thx

Z

Hi again

ive been trying to figure out what im doing wrong for days now with no success :slight_smile:
installed the .scrpt file here

i can see the 3rd part macros in keyboard maestro, i can load the example pallete. I can see it in omnifocus..yet nothing executes

2020-05-02 16:44:55 Execute macro “03)Forecast” from trigger Macro Set Palette
2020-05-02 16:44:56 /Users/zeltak/Library/Application Support/Keyboard Maestro/Keyboard Maestro Actions/Omnifocus Set Perspective/Action.scpt:
2020-05-02 16:44:56 /Users/zeltak/Library/Application Support/Keyboard Maestro/Keyboard Maestro Actions/Omnifocus Set Perspective/Action.scpt: Macro “03)Forecast” cancelled (while executing OmniFocus Set Perspective of front window to Forecast).
2020-05-02 16:44:56 Execute macro “03)Forecast” from trigger Macro Set Palette

would really love help as the example here are really great and as a new omnifocus would love to try them out

best

Z

1 Like

Hi zeltak, It seems that you have installed the scripts in /Users/zeltak/Library/OmniFocusLibrary.scpt

The correct path is: /Users/zeltak/Library/Script Libraries/OmniFocusLibrary.scpt

Same for the other script libraries. For more info and the latest version of the pack you can check the new page with the reference documentation I wrote: https://www.pierremorsa.com/mac-automation/omnifocus-action-pack-for-keyboard-maestro/

Let me know if this solves your problem. Apologies for not answering faster, I don't check this forum that often.