I wrote the following macro to rename macros in a specified folder but when I try to store the name of the folder to work on I get the error:
[error]: Not allowed to write to ‘!FOLDER’
Here is the code I used:
{
“Name”: “RemoveCasePrefix”,
“CreationDate”: “2025-4-16”,
“Commands”: [
{
“Command”: “store”,
“Target”: “A_Favorite”,
“Value”: “!FOLDER”,
“Description”: “Set the folder to the A Group of Top Favorites folder”
},
{
“Command”: “executeScript_Sandbox”,
“Target”: “var fs = require(‘fs’); var folder = ${!FOLDER}; var files = fs.readdirSync(folder).filter(f => f.endsWith(‘.json’)); files;”,
“Value”: “fileList”,
“Description”: “Get a list of all macro files (.json)”
},
{
“Command”: “forEach”,
“Target”: “fileList”,
“Value”: “fileName”,
“Description”: “Loop through each macro file”
},
{
“Command”: “store”,
“Target”: “${fileName}”,
“Value”: “currentFileName”,
“Description”: “Store the current file name”
},
{
“Command”: “executeScript_Sandbox”,
“Target”: “var fileName = ${currentFileName}; var newFileName = fileName.startsWith(‘Case.’) ? fileName.substring(5) : fileName; newFileName;”,
“Value”: “newFileName”,
“Description”: “Check if the filename starts with ‘Case.’, if yes then remove the prefix, else retain the old name.”
},
{
“Command”: “if”,
“Target”: “${currentFileName} != ${newFileName}”,
“Value”: “”,
“Description”: “Check if new file name is different from old name”
},
{
“Command”: “executeScript_Sandbox”,
“Target”: “var fs = require(‘fs’); var oldPath = ${!FOLDER} + ‘/’ + ${currentFileName}; var newPath = ${!FOLDER} + ‘/’ + ${newFileName}; fs.renameSync(oldPath, newPath);”,
“Value”: “”,
“Description”: “Rename the file”
},
{
“Command”: “echo”,
“Target”: “Renamed ${currentFileName} to ${newFileName}”,
“Value”: “”,
“Description”: “Log the rename”
},
{
“Command”: “else”,
“Target”: “”,
“Value”: “”,
“Description”: “”
},
{
“Command”: “echo”,
“Target”: “Skipping ${currentFileName}”,
“Value”: “”,
“Description”: “Log the skip”
},
{
“Command”: “end”,
“Target”: “”,
“Value”: “”,
“Description”: “”
},
{
“Command”: “end”,
“Target”: “”,
“Value”: “”,
“Description”: “End for each loop”
}
]
}
|