// Name: Screenshot Tool
// Description: Capture screen area, save, and share
// Author: johnlindquist
import '@johnlindquist/kit'// Define available screen capture areas
const captureAreas = ["full", "window", "selection"] as const;
type CaptureArea = typeof captureAreas[number];
// Prompt user to select capture area
const area = await arg<CaptureArea>("Select area", captureAreas);
// Prompt user to enter filename, using timestamp as default
const filename = await arg("Enter filename", `${Date.now()}.png`);
const savePath = kenvPath("screenshots", filename);
// Ensure the screenshots directory exists
await ensureDir(kenvPath("screenshots"));
// Execute screen capture command based on selected area
let captureCommand = "";
switch (area) {
case "selection":
captureCommand = `screencapture -i ${savePath}`;
break;
case "window":
captureCommand = `screencapture -w ${savePath}`;
break;
case "full":
captureCommand = `screencapture -x ${savePath}`;
break;
default:
throw new Error(`Invalid capture area: ${area}`);
}
try {
await exec(captureCommand);
} catch (error:any) {
console.error(`Failed to capture screenshot: ${error.message}`);
process.exit(1);
}
// Prompt user to share the screenshot
const share = await arg("Share Screenshot?", ["yes", "no"]);
// Copy path and notify if sharing is enabled
if (share === "yes") {
await copy(savePath);
await notify(`Copied path of ${filename}`);
}
// Reveal the saved screenshot in Finder
await revealFile(savePath);