Change Omni Automation script to "silent run"

Hello,

I want to “rebuild” the Omni Automation Script called “Replace Note Text with clipboard” to a silent run, meaning I do not get a confirmation anymore to replace (yes/ cancel). This way, I will be able to use it in an iOS Shortcut in Automation on time.

I tried a lot, but I do not see how to accomplish this. Is there somebody in the community who can help? This is the Omni Automation Script:

*{
	"type": "action",
	"targets": ["omnifocus"],
	"author": "Otto Automator",
	"identifier": "com.omni-automation.of.replace-note-text-with-clipboard",
	"version": "1.2",
	"description": "Will replace the text content of the note of the selected project or task with the text content of the clipboard.",
	"label": "Replace Note Text with Clipboard",
	"shortLabel": "Replace Note Text",
	"paletteLabel": "Replace Note Text",
	"image": "arrow.right.doc.on.clipboard"
}*/
(() => {
	const action = new PlugIn.Action(function(selection, sender){
		try {
			if (selection.tasks.length === 1){
				var item = selection.tasks[0]
			} else if(selection.projects.length === 1){
				var item = selection.projects[0]
			} else {
				throw {"name":"Selection Error", "message":"Please select a single project or task."}
			}
			var alertTitle = "CONFIRMATION"
			var alertMessage = "Replace note text with text from the clipboard?"
			var alert = new Alert(alertTitle, alertMessage)
			alert.addOption("Replace")
			alert.addOption("Cancel")
			alert.show(buttonIndex => {
				if (buttonIndex === 0){
					if(app.userVersion.atLeast(new Version("4"))){
						// show note
						tree = document.windows[0].content
						node = tree.nodeForObject(item)
						node.expandNote(true)
					}
					// replace text
					console.log(item)
					item.note = Pasteboard.general.string
				}
			})
		}
		catch(err){
			new Alert(err.name, err.message).show()
		}
	});

	action.validate = function(selection, sender){
		return (
			Pasteboard.general.hasStrings &&
			selection.databaseObjects.length === 1
		)
	};
	
	return action;
})();
```/*{
	"type": "action",
	"targets": ["omnifocus"],
	"author": "Otto Automator",
	"identifier": "com.omni-automation.of.replace-note-text-with-clipboard",
	"version": "1.2",
	"description": "Will replace the text content of the note of the selected project or task with the text content of the clipboard.",
	"label": "Replace Note Text with Clipboard",
	"shortLabel": "Replace Note Text",
	"paletteLabel": "Replace Note Text",
	"image": "arrow.right.doc.on.clipboard"
}*/
(() => {
	const action = new PlugIn.Action(function(selection, sender){
		try {
			if (selection.tasks.length === 1){
				var item = selection.tasks[0]
			} else if(selection.projects.length === 1){
				var item = selection.projects[0]
			} else {
				throw {"name":"Selection Error", "message":"Please select a single project or task."}
			}
			var alertTitle = "CONFIRMATION"
			var alertMessage = "Replace note text with text from the clipboard?"
			var alert = new Alert(alertTitle, alertMessage)
			alert.addOption("Replace")
			alert.addOption("Cancel")
			alert.show(buttonIndex => {
				if (buttonIndex === 0){
					if(app.userVersion.atLeast(new Version("4"))){
						// show note
						tree = document.windows[0].content
						node = tree.nodeForObject(item)
						node.expandNote(true)
					}
					// replace text
					console.log(item)
					item.note = Pasteboard.general.string
				}
			})
		}
		catch(err){
			new Alert(err.name, err.message).show()
		}
	});

	action.validate = function(selection, sender){
		return (
			Pasteboard.general.hasStrings &&
			selection.databaseObjects.length === 1
		)
	};
	
	return action;
})();

@bkruisdijk, I think the inclusion of that safety measure is a good idea. If you have a specific use case where you wouldn’t want that, send me a direct message and I will help you.

1 Like

Please test with this one…
ReplaceNoteText.omnifocusjs (1.3 KB)

1 Like

What a great and helpful community this is! Really appreciated.

@Logan , this is working, and I now see what I was doing wrong. Great thanks!

@unlocked2412 , thanks for your warning! I am aware of the risk. I am a little bit thinking about a too-complex Automation. What I am trying to do is the following:

  1. I have an already working iOS Shortcut that plans my OmniFocus items (My Do list) to the free slots in my agenda. It will be scheduled in my calendar with the name “00. Hyperschedule” with a link back to the OF item. I am already very happy with this, but trying to automate further;
  2. I have 11 calendars that represent my roles, and in OF, I have the same 11 folders that represent roles I aspire;
  3. So by copying the project of an OF Item in the note field, I can see in the Calendar item the OF project. That is were I need the “Replace Note text” for. Silent, without warning, because I want to schedule this iOS Shortcut at certain times in the night; (Work in progress, but to get an idea, here is the iOS Shortcut)
  4. I still need to figure out how to move the specific Calendar events, representing the Do task in OF, to the correct Calendar (Role) by finding the OF folder (Role) of that project.