Vojta Holik

Vojta Holik

// Menu: Tinify
// Description: Compress selected images with Tinify
// Author: Vojta Holik
// Twitter: @vjthlk
let tinify = await npm("tinify");
let fs = await import("fs");
let selectedFiles = await getSelectedFile();
tinify.key = env.TINIFY_API_KEY;
let filePaths = selectedFiles.split("\n");
for (let filePath of filePaths) {
let directory = path.dirname(filePath);
let extension = path.extname(filePath);
let originalFileName = path.basename(filePath);
let suffix = "-optimized";
let newFileName = originalFileName.replace(extension, suffix + extension);
fs.readFile(filePath, (err, sourceData) => {
if (err) throw err;
tinify.fromBuffer(sourceData).toBuffer((err, resultData) => {
if (err) throw err;
fs.writeFile(`${directory}/` + newFileName, resultData, (err) => {
if (err) throw err;
});
});
});
}
# ...
TINIFY_API_KEY=your_key
<details> <summary>You can change file names and directory to best suite your workflow. Since I often work with <code>@2x</code> images I adjusted the script to correctly suffix that part.</summary>
let isHD = originalFileName.includes("@2x");
let newFileName = isHD
? originalFileName
.replace("@2x", "")
.replace(extension, `${suffix}@2x${extension}`)
: originalFileName.replace(extension, `${suffix}${extension}`);
</details>

<img src="https://p-ZmFjNlQ.b3.n0.cdn.getcloudapp.com/items/mXur6N2W/50e6f3d6-8b3d-4b24-a4c6-3ee047a9613e.gif?v=f5fe168354cb0d1e34dcbf405c7891ac" width="700" />
// Menu: Slugify file name
// Description: Slugify selected files
// Author: Vojta Holik
// Twitter: @vjthlk
let slugify = await npm("slugify")
let selectedFiles = await getSelectedFile()
const filePaths = selectedFiles.split("\n")
for (let filePath of filePaths) {
let originalFileName = path.basename(filePath)
let newFileName = slugify(originalFileName, {
lower: true,
})
let newFilePath = path.join(
path.dirname(filePath),
newFileName
)
cp(filePath, newFilePath)
}