Skip to content

Commit 0d668c5

Browse files
Merge pull request #16 from thaodangspace/fix-jxa-todo-creation
Fix ToDo creation in JXA automation
2 parents d225fc1 + 2ca3e48 commit 0d668c5

1 file changed

Lines changed: 13 additions & 9 deletions

File tree

‎things/automation.js‎

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -747,6 +747,16 @@ function deleteRelation(object, property) {
747747
relation.delete();
748748
}
749749

750+
// Things' JXA `make({ new: "to do" })` throws "Can't make class" on some
751+
// macOS/Things builds (the two-word "to do" class does not resolve), so build
752+
// the ToDo object and push it into a container instead -- the form Things
753+
// reliably accepts. `container` is a project or one of app.lists (e.g. Inbox).
754+
function newTodo(app, container, title) {
755+
var task = app.ToDo({ name: title });
756+
container.toDos.push(task);
757+
return task;
758+
}
759+
750760
function addTodo(app, request) {
751761
if (request.completed && request.canceled) throw new Error("completed and canceled cannot both be true");
752762
if ((request.list || request.list_id) && (request.project || request.project_id)) {
@@ -756,10 +766,9 @@ function addTodo(app, request) {
756766
var task;
757767
if (request.project || request.project_id) {
758768
project = resolveProjectTarget(app, request.project_id, request.project);
759-
task = app.ToDo({ name: request.title });
760-
project.toDos.push(task);
769+
task = newTodo(app, project, request.title);
761770
} else {
762-
task = app.make({ new: "to do", withProperties: { name: request.title } });
771+
task = newTodo(app, resolveListTarget(app, "", "Inbox"), request.title);
763772
}
764773
applyCommonFields(app, task, request);
765774
if (!project) applyListDestination(app, task, request);
@@ -775,12 +784,7 @@ function addProject(app, request) {
775784
if (request.area || request.area_id) project.area = resolveAreaTarget(app, request.area_id, request.area);
776785
var children = request.to_dos || [];
777786
for (var i = 0; i < children.length; i++) {
778-
var child = app.make({ new: "to do", withProperties: { name: children[i] } });
779-
try {
780-
child.project = project;
781-
} catch (error) {
782-
app.move(child, { to: project });
783-
}
787+
newTodo(app, project, children[i]);
784788
}
785789
if (request.reveal) project.show();
786790
return actionResult("add-project", project);

0 commit comments

Comments
 (0)