Skip to content

Move selected text to a note within a directory #169

Answered by johnlindquist
classicrob asked this question in Q&A
Move selected text to a note within a directory #169
Apr 21, 2021 · 2 answers · 4 replies

I'm curious - how straightforward would it be to create a global sort of "move block" function with script kit? I'm imagining highlighting a text selection, pressing a keyboard shortcut, cutting that selection, searching through files within my notes directory to find the specific file I want to add it to, and then appending it to the bottom. Ideally, at the point of insertion, I decide whether to leave a copy of the text in the original location or whether it is just fully moved to the new location.

Thank you!

Here's what I came up with. I couldn't decide between this solution (listing all the files twice, once for each action) or spitting it into a arg("Select note") and a arg("Select command"). But hopefully this should get you started with whatever you decide.

Install testing-cut-to-file

//Shortcut: opt x

//I haven't decided on the final api for a keyboard lib, use this for now:
let command = async key =>
  await applescript(`
    tell application "System Events"
      keystroke "${key}" using command down
    end tell`)
//-----------------------------------------------------------------------
let NOTES_PATH = await env("NOTES_PATH", {
  placeholder: "Set env for NOTES_PATH",
})

let { note,

Replies

2 suggested answers
·
4 replies

Great idea! 💡

I'm heading out for my 2nd vaccine shot, but I'll put together a script demo for you this afternoon

0 replies

Here's what I came up with. I couldn't decide between this solution (listing all the files twice, once for each action) or spitting it into a arg("Select note") and a arg("Select command"). But hopefully this should get you started with whatever you decide.

Install testing-cut-to-file

//Shortcut: opt x

//I haven't decided on the final api for a keyboard lib, use this for now:
let command = async key =>
  await applescript(`
    tell application "System Events"
      keystroke "${key}" using command down
    end tell`)
//-----------------------------------------------------------------------
let NOTES_PATH = await env("NOTES_PATH", {
  placeholder: "Set env for NOTES_PATH",
})

let { note, key } = await arg("Select note", async () => {
  let notes = await readdir(NOTES_PATH)

  return notes.flatMap(note => {
    let copyTo = {
      name: note,
      description: `copy`,
      value: {
        note: path.join(NOTES_PATH, note),
        key: `c`,
      },
    }

    let cutTo = {
      name: note,
      description: `cut`,
      value: {
        note: path.join(NOTES_PATH, note),
        key: `x`,
      },
    }

    return [copyTo, cutTo]
  })
})

//Hide so the keyboard shortcut can focus on the app under the prompt.
// This will also be an interal API in the future
send("HIDE_APP")
await command(key)

await appendFile(note, await paste())

edit(note)
4 replies
@classicrob

Wowowow this is amazing, thank you!

@classicrob

Updated request: How could I make it search through files in subdirectories too? My notes directory has a few subdirectories.

Also, congratulations on the vaccine :)

@classicrob

Okay, so I tried updating the code to use a different readdir https://www.npmjs.com/package/readdir-enhanced#deep and just copied the "Crawl Subdirectories" example but its not working... I think I might be missing something.
image

Answer selected by johnlindquist
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants