Hello,
I've been struggling with this all day, and I've admitted defeat and saved the data to a KM Variable in the meantime until I can solve this problem.
In short - I can't find a way to download data from a Custom HTML prompt that doesn't involve using a KM variable as an intermediary. I've made a table that presents data to my from my database, and I'd rather not use a KM variable incase I need to save so much data that it might overpopulate the KM environment.
My most involved attempt revolved around trying to create a blob file from the CSV data to download, either with a URL or URI. In KM it presents an error from Mac OS's dialogue that it can't find an application like so:
"There is no application set to open the URL blob:null/26580a6e-86c7-439c-86b7-21637dfa55fb."
I'm generating this code from an 'execute shell script' that populates the HTML with data from a CSV file. Below is the javascript from the HTML that I've been trying to use to create a download:
// Define the downloadBlobAsFile function
const downloadBlobAsFile = (function closure_shell() {
const a = document.createElement(\"a\");
return function downloadBlobAsFile(blob, filename) {
const object_URL = URL.createObjectURL(blob);
a.href = object_URL;
a.download = filename;
a.click();
URL.revokeObjectURL(object_URL);
};
})();
// Function to save the spreadhseet data as a CSV
function saveChanges() {
var table = document.getElementById('csvTable');
var rows = table.getElementsByTagName('tr');
var csvContent = [];
for (var i = 1; i < rows.length; i++) {
if (rows[i].style.display !== 'none') {
var cells = rows[i].getElementsByTagName('td');
var rowData = [];
for (var j = 1; j < cells.length; j++) {
rowData.push(cells[j].textContent.replace(/,/g, ''));
}
while (rowData.length > 0 && rowData[rowData.length - 1] === '') {
rowData.pop();
}
csvContent.push(rowData.join(','));
}
}
var csvString = csvContent.join('\n');
// Create a Blob with the CSV content
var blob = new Blob([csvString], {type: \"text/csv;charset=utf-8\"});
// Use the downloadBlobAsFile function to trigger the download
downloadBlobAsFile(blob, \"IncomingCSV.csv\");
// Show instructions to the user
var instructions = document.createElement(\"p\");
instructions.textContent = \"The CSV file download has been initiated. \" +
\"Please save it to this location:\n\" +
\"CSV/IncomingCSV.csv\";
instructions.style.textAlign = \"center\";
instructions.style.marginTop = \"20px\";
document.body.appendChild(instructions);
alert('CSV file download has started. Please check your downloads and move the file to the correct location.');
}
This actually works if I run this on Safari, but I want to keep it to a HTML prompt if possible.
I thought about using window.KeyboardMaestro.ProcessAppleScript to create the file, but the escaping required to program this in javascript within html within shell was enough to make a grown man cry. I couldn't figure it out.
Any help would be greatly appreciated!