Copy link to clipboard
Copied
I'm trying to get file downloading with NodeJS working in my CEP extension for Premiere Pro CC 2015. I have the following script:
var http = require('http');
var fs = require('fs');
var file = fs.createWriteStream("file.jpg");
var request = http.get("http://i3.ytimg.com/vi/J---aiyznGQ/mqdefault.jpg", function(response) {
response.pipe(file);
});
The script works perfectly well if I run it on it's own. For example, if I put it in a file called test.js, and run node test.js in terminal, it successfully saves the image to the same directory the script is in.
When I include it in my CEP extension, however, nothing happens. The script executes without errors, but no file is downloaded anywhere. Even if I try:
try {
response.pipe(file);
alert("File downloaded");
} catch(e) {
alert(e);
}
The "File downloaded" alert pops up. So the lack-of-download doesn't seem to be from an error in the code, but perhaps in the way CEP works itself? I'm at a loss. Any suggestions?
Thanks!
For anyone interested, I came up with a solution.
Instead of saving to the directory of the extension, you can save a downloaded file to the path of the project, using app.project.path. Here's my commented code:
In hostscript.jsx
...function returnPath() {
var projectPath = app.project.path;
// split the path into an array
var parsed = projectPath.split("/");
// remove the last element (which is the name of the Premiere project file)
parsed.pop();
// rejoin the array, gi
Copy link to clipboard
Copied
Moved to right forum, you were in the Business Catalyst development forum.
Copy link to clipboard
Copied
Thanks
Copy link to clipboard
Copied
Update:
Looks like it might be a permissions issue. I altered the createWriteStream path to write to a file on my desktop, and the download worked from the extension.
To clarify, this extension will be used to download audio files from a website our company hosts, and import them directly into Premiere Pro. Therefore, the files need to be downloaded somewhere consistent on the client machine. That's why I'm trying to download to the actual extension folder itself.
If this isn't possible, can anyone suggest a way to reliably download the file to a consistent location on the client machine, so that I can then immediately import it into Premiere?
Thanks.
Copy link to clipboard
Copied
Copy link to clipboard
Copied
For anyone interested, I came up with a solution.
Instead of saving to the directory of the extension, you can save a downloaded file to the path of the project, using app.project.path. Here's my commented code:
In hostscript.jsx
function returnPath() {
var projectPath = app.project.path;
// split the path into an array
var parsed = projectPath.split("/");
// remove the last element (which is the name of the Premiere project file)
parsed.pop();
// rejoin the array, giving the parent directory of the Premiere project file
var joined = parsed.join("/");
return(joined);
}
In main.js
function downloadAndImport(url, songName) {
// call the evalScript we made in the jsx file
csInterface.evalScript('returnPath()', function(result) {
var https = require('https');
var fs = require('fs');
var mkdirp = require('mkdirp');
// create a Downloads directory in the project path if it doesn't exist already
var downloadDirectory = result + '/Downloads';
mkdirp(downloadDirectory, function(err){
if (err) { // handle error }
});
var fullPath = downloadDirectory + "/" + songName;
var file = fs.createWriteStream(fullPath);
var request = https.get(url, function(response) {
response.pipe(file);
// ensure file is complete before importing
response.on('end', function() {
csInterface.evalScript("app.project.importFiles(['" + fullPath + "'])");
});
});
});
}
This works for my needs. Hopefully it will help someone else!
Copy link to clipboard
Copied
Thx, I already find solution.
Copy link to clipboard
Copied
Hello danf33957308,
I am trying to download an image file from internet via index.html file from my illustrator extension ,
tried to use your piece of code also but nothing happens,
Could you help or give your working set so that i could proceed.
thnaks
Copy link to clipboard
Copied
Amazing, Thank you for coming back and sharing your solution. This worked like a charm. saved me.
Copy link to clipboard
Copied
Curious if anything here needs to be changed for PC. for some reason it seems as if my .jsx file is not being read on my PC test computer at all. anyone have an idea?