Action

Get a Dynalist Item

Posted by sorashima, Last update almost 3 years ago

UPDATES

almost 3 years ago

Added the URL of the description page.

クリップボードのDynalistアイテムリンクを元に、そのアイテムを読み込む。

https://sorashima.hatenablog.com/entry/EditDynalistItemOnDrafts

(sorry. in Japanese.)

Steps

  • script

    // CredentialにDynalistのSecret Tokenを保存
    const credential = Credential.create("Dynalist", "Dynalist")
    credential.addPasswordField("token", "Secret Token")
    credential.authorize()
    
    
    // クリップボードの読み込み
    console.log(`app.getClipboard:${app.getClipboard()}`)
    const ids = /https:\/\/dynalist\.io\/d\/(.+)#z=(.+)/.exec(app.getClipboard())
    
    if (ids !== null) {
      // クリップボードにDynalistのアイテムリンクがコピーされているようなら
      console.log(`ids[1]:${ids[1]}`)
      // ドキュメントのコンテンツの読み込み
      const http = HTTP.create()
      const response = http.request({
        "url": "https://dynalist.io/api/v1/doc/read",
        "encoding": "json",
        "method": "POST",
        "data": {
          "token": credential.getValue("token"),
          "file_id": ids[1]
        }
      })
    
    
      if (response.success) {
        // HTTPのリクエストが正常に完了したら
        const rText = JSON.parse(response.responseText)
    
        if (rText._code == 'Ok') {
          // レスポンスの _code が Ok (リクエストが成功)の場合
          //app.displaySuccessMessage(rText._code)
          console.log(JSON.stringify(rText))
    
    
          const nodesA = rText.nodes
          const itemO = nodesA.find(n => n.id == ids[2])
          
          const content = itemO.content
          const note = itemO.note
          const checked = itemO.checked 
          const checkbox = itemO.checkbox 
          const heading = itemO.heading 
          const color = itemO.color 
    
    
          const dc = `${(content) ? content : ""}
    ---
    file_id: ${ids[1]}
    node_id: ${ids[2]}
    checked: ${(checked) ? checked : ""}
    checkbox: ${(checkbox) ? checkbox : ""}
    heading: ${(heading) ? heading : ""}
    color: ${(color) ? color : ""}
    ---
    ${(note) ? note : ""}`
          const d = new Draft()
          d.content = dc
          d.update()
      
          const uuid = d.uuid
          editor.load(Draft.find(uuid))
    
    
    
        } else {
          // レスポンス の _code が Ok でない場合、errorの情報を表示する
          alert(rText._code + '\n' + rText._msg)
          context.fail(rText._msg)
        }
    
      } else {
        // HTTPのリクエストが正常に完了しなかったら
        console.log(response.statusCode)
        console.log(response.error);
        alert(response.statusCode + '\n' + response.error)
        context.fail(response.error)
    
      }
      
    } else {
      // クリップボードにDynalistのアイテムリンクがコピーされていないようなら
      alert("Dynalistのアイテムリンクがクリップボードにコピーされていません")
      context.fail("Dynalistのアイテムリンクがクリップボードにコピーされていません")
    }
    

Options

  • After Success Default
    Notification Info
    Log Level Info
Items available in the Drafts Directory are uploaded by community members. Use appropriate caution reviewing downloaded items before use.