I have the script partially working.
It saves all the open psd as jpg into into a separate directory and it close some of the open files not all.
The directory has five files. The script saves only three files,
What am I doing wrong?
#target photoshop
if (app.documents.length > 0) {
//flatten the active document
app.activeDocument.flatten();
//jpeg options
var myJPEGOptions = new JPEGSaveOptions();
myJPEGOptions.embedColorProfile = true;
myJPEGOptions.formatOptions = FormatOptions.STANDARDBASELINE;
myJPEGOptions.matte = MatteType.WHITE;
myJPEGOptions.quality = 12;
myJPEGOptions.scans = 3;
// get documents;
var docs = app.documents;
for (var m = 0; m < app.documents.length; m++) {
app.activeDocument = docs[m];
try {
//save file to folder
var myFile = new File(("~/Desktop/forum-test") + "/" + activeDocument.name);
app.activeDocument.saveAs(myFile, myJPEGOptions, true);
//close the document
activeDocument.close(SaveOptions.DONOTSAVECHANGES);
}
catch (e) {
alert ("Error the script did not execute");
}
}
}
The app.documents collection is dynamic so when you close a document this collection is changed accordingly.
Because you are closing your document inside a for loop where you compare an incrementing index against app.documents.length you are not processing all files (as app.documents.length decreases by one each time the loop is processed).
Try a while loop instead:
while (app.documents.length){
// Save and close the active document here.
}
I think this two lines are wrong:
//save file to folder
var myFile = new File(("~/Desktop/forum-test") + "/" + activeDocument.name);
and
//close the document
activeDocument.close(SaveOptions.DONOTSAVECHANGES);
Shouldn't you be using app.activeDocument instead of activeDocument? What is activeDocument anyway?
Related
I know that there are many questions and answers pertaining this, but i have tried but am still unable to do so. Please help.
Here is my js code:
$(document).ready(clicklistener); // once ready, run clicklistener function
function clicklistener() {
$(".image-thumbnail").click(popup); // run popup function when ".image-thumbnail" is clicked}
function popup() {
var srcname = $(this).attr("src"); // get source image filename
var newfile = srcname.replace("small", "large"); // replace with large img
if ($(this).parent().children("span").children("img").hasClass("image-popup")) { // traverse the elements to check if the image popup already exists
$(this).parent().children("span").remove(); // remove if it already exists
} else {
var printimg = "<span><img class=\"image-popup\"src=\"" + newfile + "\"></img></span>";
$(this).after(printimg); // create img if it doesn't exist
}}
Doesn't look like you have an event defined for closing it (unless you click on the popup). SO you need to write one:
$(document).ready(function(){
$(".image-thumbnail").click(function(){
// allow for body.click to first hide any current images
setTimeout(function(){popup($(this))}, 600)); // run popup function when ".image-thumbnail" is clicked
});
function popup($ele) {
var srcname = $ele.attr("src"); // get source image filename
var newfile = srcname.replace("small", "large"); // replace with large img
if ($ele.parent().children("span").children("img").hasClass("image-popup")) { // traverse the elements to check if the image popup already exists
$ele.parent().children("span").remove(); // remove if it already exists
} else {
var printimg = "<span><img class=\"image-popup\"src=\"" + newfile + "\"></img></span>";
$ele.after(printimg); // create img if it doesn't exist
}
}
$(window).click(function(){
$('.image-popup').remove()
});
})
I'm using JsPDF to generate a number of PDF files and then download them. With Chrome and Edge it's correctly generating and downloading all of them, but with Firefox is downloading only the first one.
This is my code:
<script>
$( document ).ready(function() {
let doc = new jsPDF();
doc.text(20, 20, 'hello');
for(a = 0; a < 6; a++){
// doc.output('dataurlnewwindow'); < NOT WORKING ON RECENT BROWERS.
// doc.output('datauri');
// doc.addHTML($('#content'), 1, 1, function () {
// var blob = doc.output("blob");
// window.open(URL.createObjectURL(blob));
// });
doc.save('file_number_' + 'a' + '.pdf'); < DOWNLOADS ONLY THE FIRST FILE IN FIREFOX.
}
});
</script>
How could I be able to download all the generated files? It would be OK even if I could open them in a new window, as long as I'm able to specify the filename of the PDF.
Thanks in advance.
Edit: as indicated by pytness, it appears Firefox is blocking the process.
I just tried to delay the clicks in this way:
function clickAll(){
let waiter = 0;
$('.pdf_line').each(
function(){
if ($(this).find('input').prop('checked')){
//console.log('click! waiter = ' + waiter);
$(this).find('.print_document').delay(4000*waiter).click();
waiter++;
};
}
)
}
But after the first download Firefox halts the whole process.
It looks like you’re saving the file with the same name every time in your loop, so it’s just saving over it each time. Try changing the ‘a’ in your doc.save to a
I am trying to do an XML import automatically from a startup script when a document is loaded. I am successful in getting most of the content to populate, but images are being ignored. Everything works, including images, when I do a manual 'Import XML' through the UI, or through a manual script.
Below is my manual script:
var myDocument = app.activeDocument;
var xmlFile = File('/c/Full/Path/To/data.xml');
myDocument.importXML(xmlFile);
But the goal is to do it on startup. Below is my startup script:
#targetengine "session"
app.addEventListener('afterOpen', function(myEvent) {
if (myEvent.target.constructor.name !== 'Document') {
return;
}
var myDocument = myEvent.target;
var xmlFile = File('/c/Full/Path/To/data.xml');
myDocument.importXML(xmlFile);
});
Below is the XML tag for the image:
<Image href="file:///C:/Full/Path/To/Image/02.png" />
I'm wondering if there is an issue with the 'afterOpen' event callback, and that's the reason why it works manually using the same method, but not in the startup script.
I was able to get around the issue by avoiding the event listener altogether.
main();
function main () {
// create a path for a file object
var curFile = File('/c/Path/To/file.indd');
var xmlFile = File('/c/Path/To/data.xml');
// close app if files don't exist
if (!curFile.exists || !xmlFile.exists) {
app.quit(SaveOptions.NO);
}
// open the file
var curDoc = app.open(curFile);
// import the xml
curDoc.importXML(xmlFile);
// create a new file object
var pdfFile = new File(curFile.parent + '/' + curFile.name + '.pdf');
// export to pdf
curDoc.exportFile(ExportFormat.PDF_TYPE, pdfFile);
// close app
app.quit(SaveOptions.NO);
}
I have made a photo gallery in my website using the following:
/*Begin Photo Gallery Code*/
var images = ['g1.jpg', 'g2.jpg', 'g3.jpg', 'g4.jpg'];
function loadImage(src) {
$('#pic').fadeOut('slow', function() {
$(this).html('<img src="' + src + '" />').fadeIn('slow');
});
}
function goNext() {
var next = $('#gallery>img.current').next();
if(next.length == 0)
next = $('#gallery>img:first');
$('#gallery>img').removeClass('current');
next.addClass('current');
loadImage(next.attr('src'));
}
$(function() {
for(var i = 0; i < images.length; i++) {
$('#gallery').append('<img src="images/gallery/' + images[i] + '" />');
}
$('#gallery>img').click(function() {
$('#gallery>img').removeClass('current');
loadImage($(this).attr('src'));
$(this).addClass('current');
});
loadImage('images/gallery/' + images[0]);
$('#gallery>img:first').addClass('current');
setInterval(goNext, 4000);
});
It loads one picture at a time from a set of four pictures. Also I have four html files, each of them being relevant to one of the pictures. I want to use JavaScript/JQuery/AJAX to load the relevant html file's content along with the shown picture. Does anyone have an idea how I can do this?
Should I put the ajax files (4 html files) into a JavaScript array or something?
var ajaxPages=['ajax1.html','ajax2.html','ajax3.html','ajax4.html'];
Thanks in advance.
Unless the HTML files supposed to change somehow during their displaying, should either output them via your server-side code in hidden divs with the request (would be the correct way of doing it) or use AJAX to save them in a variable or create hidden divs.
First you need two arrays like this:
var ajaxPages=['ajax1.html','ajax2.html','ajax3.html','ajax4.html'];//File Names
var divPages=['div1','div2','div3','div4'];//Div ids in order
For the AJAX part you should use something like:
var getHtml = function(filename,divid){
$.post('html/'+filename, function(data) {
//The first argument is your file location
//Second one is the callback, data is the string retrieved
$('#'+divid).html(data);
});
}
$.each(ajaxPages,function(index,value){
getHtml(value,divPages[index]);
});
That should do it... Do tell me if you require further explanation.
EDIT:
var ajaxPages=['ajax1.html','ajax2.html','ajax3.html','ajax4.html'];
var divId="yourdivid";
var textArray=new Array();
var currentImg=0;
var getHtml = function(filename){
$.post('html/'+filename, function(data) {
textArray.push(data);//Save data inside the array textArray
});
}
$.each(ajaxPages,function(index,value){
getHtml(value,divPages[index]);
});
Then your goNext() method:
function goNext() {
var next = $('#gallery>img.current').next();
if(next.length == 0){
next = $('#gallery>img:first');
currentImg=0;
}else{
currentImg++;
}
$('#gallery>img').removeClass('current');
next.addClass('current');
loadImage(next.attr('src'));
$('#'+divId).html(textArray[currentImg]);//Adds text to div based on current picture
}
That should be working fine!
Using javascript I'm trying to load images not on the current page (from an array of image links) and if they are large enough, add them to an array. I'm executing this after the current page has loaded, though a bookmarklet, or firebug console. Using FF.
The closest I've come is the below, but this does not seem to work, and I'm guessing this is because the size test is running before the images have loaded. Apparently my attempt to fix this with 'onload' does not work. How can I fix this up, or is there a better/simpler way to accomplish this task?
(function() {
//create array for the images
var imageArray = new Array();
function loadIfBig(x){
if (x.height > 299 && x.width > 299 && (x.height > 399 || x.width > 399)) {
imageArray.push(x);
//dispImage = '<img src=' + x.src + '><br>';
//document.write('<center>' + dispImage + '</center>');
}
return true;
}
//given an array of imageLinks:
for (x in imageLinks){
//create an image form link and add to array if big enough
im = document.createElement('img');
im.src = imageLinks[x];
im.onload = loadIfBig(im);
}
//view results:
for (x in imageArray){
disp_image = '<img src='+imageArray[x].src+'><br>';
document.write('<center>'+disp_image+'</center>');
}
})();
Update:
Thanks! sure you're right and I'm missing something stupid here, but I made the change you suggested, and then pulled writing the elements into loadIfBig, but it doesn't seem to be executing that function. current code below, including a couple sample input links:
(function() {
var imageArray = new Array();
var imageLinks = ['http://1.bp.blogspot.com/_mfMRTBDpgkM/SwCwu1RPphI/AAAAAAAAJpw/v9YInFBW84I/s1600/800px-San_Francisco_in_ruin_edit2.jpg','http://2.bp.blogspot.com/_mfMRTBDpgkM/SwCwulSZ_yI/AAAAAAAAJpo/NsRcJdpz4Dk/s1600/San_Francisco_PC_59.jpg']
function loadIfBig(x){
if (x.height > 299 && x.width > 299 && (x.height > 399 || x.width > 399)) {
imageArray.push(x);
dispImage = '<img src=' + x.src + '><br>';
document.write('<center>' + dispImage + '</center>');
}
return true;
}
processImages = function(){
for (x in imageLinks) {
//create an image from link and add to array if big enough
im = document.createElement('img');
im.src = imageLinks[x];
im.onload = function(){
loadIfBig(this);
}
}
}
})();
Your fix doesn't work because you're actually executing it immediately.
im.onload = loadIfBig(im) is actually running the function.
What you can do is something like:
im.onload = function() {
loadIfBig(this);
}
Another problem is the fact that you're running through the array of big images before the onload callbacks have actually executed. That needs to be stuck in a different function and called once all the onloads are done.