obsidian-projects icon indicating copy to clipboard operation
obsidian-projects copied to clipboard

How to "create new project" by code

Open zhangkaihua88 opened this issue 1 year ago • 2 comments

What would you like to be added?

I would like to ask how to quickly create a project through code instead of a UI interface

like this app.plugins.plugins["obsidian-projects"].createProject()

Why is this needed?

Due to my many projects, I want to automate the creation

zhangkaihua88 avatar Jul 07 '24 10:07 zhangkaihua88

I implemented it by adding the following code in main.js

  addNewProject(data){
    data.id = v4_default()
    data.views.forEach(view => {
        view.id = v4_default();  // Setting the 'type' property to 2
      });
    settings.addProject(data);
    // this.activateView(data.id);
    return data.id
  }

Calling through JavaScript

a = {
  "fieldConfig": {},
  "defaultName": "",
  "templates": [],
  "excludedNotes": [],
  "isDefault": false,
  "dataSource": {
    "kind": "folder",
    "config": {
      "path": "14_Quant/因子学习",
      "recursive": true
    }
  },
  "newNotesFolder": "",
  "views": [
    {
      "config": {},
      "filter": {
        "conjunction": "and",
        "conditions": []
      },
      "colors": {
        "conditions": []
      },
      "sort": {
        "criteria": []
      },
      "name": "表格",
      "type": "table"
    }
  ],
  "name": "Quant_test"
};

app.plugins.getPlugin('obsidian-projects').addNewProject(a)

zhangkaihua88 avatar Jul 08 '24 02:07 zhangkaihua88

I don't know why this isn't being thumbs-up-ed more. I know this is a relatively new issue, but the use case for programmatically creating projects is great.

Especially, the ability to define a fieldConfig and have the fields be exposed and in the proper order instead of manually adding, dragging and dropping the fields that we wish to see on each project. Even when using Templater to define the new note template, projects doesn't automatically set the fields to the template.

Currently, I'm using the QuickAdd plugin to create project folders through code and input prompts, and lastly a call to the create project modal, where I need to fill in the details kinda stinks as a workflow. Being able to reuse variables set by the input prompts would be amazing.

AttackOnTyler avatar Oct 30 '24 15:10 AttackOnTyler