Skip to content

Instantly share code, notes, and snippets.

@johnlindquist
Created January 3, 2024 21:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnlindquist/5181c14b2fc6bda7aeb6ec43a342c499 to your computer and use it in GitHub Desktop.
Save johnlindquist/5181c14b2fc6bda7aeb6ec43a342c499 to your computer and use it in GitHub Desktop.
// Name: Copy Latest Transcript
import "@johnlindquist/kit"
// Define the directory path
const directoryPath = path.join(process.env.HOME, "Documents", "Audio Hijack")
async function readMostRecentTxtFile(dirPath) {
try {
// Read the directory contents
const files = await readdir(dirPath, { withFileTypes: true })
// Filter out directories and non-txt files, and get the full paths
const txtFiles = files
.filter(file => file.isFile() && file.name.endsWith(".txt"))
.map(file => path.join(dirPath, file.name))
// Get the stats for each file and sort by modified time
const sortedFiles = (
await Promise.all(
txtFiles.map(async file => {
const stats = await stat(file)
return { file, mtime: stats.mtime.getTime() }
})
)
).sort((a, b) => b.mtime - a.mtime)
// Check if there are any txt files
if (sortedFiles.length === 0) {
throw new Error("No .txt files found in the directory.")
}
// Get the most recent file
const mostRecentFile = sortedFiles[0].file
// Read and return the contents of the most recent txt file
return readFile(mostRecentFile, "utf8")
} catch (error) {
console.error("An error occurred:", error)
throw error
}
}
// Use top-level await to read the most recent txt file
const content = await readMostRecentTxtFile(directoryPath)
await setSelectedText(content)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment