// Author: johnlindquist
// Name: Batch Image Renamer
// Description: Rename images in a directory with Faker-generated names
// Author: johnlindquist
import '@johnlindquist/kit'
import { faker } from '@faker-js/faker'
// Prompt user to select a directory containing images
const imageDir = await path({
onlyDirs: true,
placeholder: 'Select a directory with images to rename',
})
// Read files from the selected directory
const imageFiles = await readdir(imageDir)
.then(files => files.filter(file => /\.(jpe?g|png|gif)$/i.test(file))) // Filter for common image types
.catch(error => {
console.error('Error reading directory:', error)
exit()
})
if (!imageFiles.length) {
await div(md(`# No images found in ${imageDir}`))
exit()
}
// Generate fake image names using faker.js
const fakeImageNames = imageFiles.map(() => faker.word.words(3).replace(/\s/g, '-') + '-' + faker.string.alphanumeric(8))
// Batch rename images with fake names
for (let i = 0; i < imageFiles.length; i++) {
const oldImagePath = path.join(imageDir, imageFiles[i])
const ext = path.extname(imageFiles[i])
const newFileName = fakeImageNames[i] + ext
const newImagePath = path.join(imageDir, newFileName)
await rename(oldImagePath, newImagePath)
}
// Notify user about successful renaming
await notify({
title: 'Images Renamed!',
body: `Renamed ${imageFiles.length} images in ${imageDir} with fake names.`,
icon: 'face-smile',
})
// Optionally reveal the directory in Finder for user convenience
await revealFile(imageDir)