Created
January 3, 2024 21:28
-
-
Save johnlindquist/5181c14b2fc6bda7aeb6ec43a342c499 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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