I'm trying to bring back to life and older script I had used that worked in the past. The script would download comics (that we have the rights to) using autohotkey and curl... Then in InDesign we would run the following from the javascript Scripts panel:
#targetengine "session"
var date, month, year, myDocument;
var curDate = new Date();
var myTemplatePath = "/c/Comic/ComicImport.indd";
var myComicsPath = "/c/Comic/Comics/";
var myTemplate = new File(myTemplatePath);
if (myTemplate.exists) {
try {
myDocument = app.open(myTemplate);
} catch (e) {
alert("Could not open template, exiting\n" + e);
exit();
}
var win = showDialog();
} else {
alert("Could not locate template at:\n" + myTemplatePath + "\nexiting");
}
function showDialog() {
var win = new Window('palette');
with(win){
win.Pnl = add('panel', undefined, 'Date / Month / Year');
win.Pnl.orientation = 'row';
with(win.Pnl) {
win.Pnl.day = add('edittext');
win.Pnl.day.text = curDate.getDate();
win.Pnl.day.preferredSize = [30,20];
win.Pnl.month = add('edittext');
win.Pnl.month.text = curDate.getMonth() + 1;
win.Pnl.month.preferredSize = [30,20];
win.Pnl.year = add('edittext');
win.Pnl.year.text = curDate.getFullYear();
win.Pnl.year.preferredSize = [50,20];
}
win.btnOk = add('button', undefined, 'Import Comic');
win.btnOk.onClick = setDate;
};
win.center();
win.show();
return win;
}
function setDate() {
date = win.Pnl.day.text;
month = win.Pnl.month.text;
year = win.Pnl.year.text;
// OK we close the window and do the import
//win.close();
importComics();
}
function importComics() {
try {
//set comic1 to "macintosh Hd:users:marshall:documents:comics:" & DYear & Dmonth & Dday & "pzjud-a.tif"
var comics = new Array();
// REPLACE with own filepaths, could be
//comics.push(new File("/c/comics/" + year + month + date + "pzjud- a.tif"));
comics.push(new File(myComicsPath + "comic1-" + year + "-" + month + "-" + date + ".tif"));
comics.push(new File(myComicsPath + "comic2-" + year + "-" + month + "-" + date + ".tif"));
comics.push(new File(myComicsPath + "comic3-" + year + "-" + month + "-" + date + ".tif"));
comics.push(new File(myComicsPath + "comic4-" + year + "-" + month + "-" + date + ".tif"));
comics.push(new File(myComicsPath + "comic5-" + year + "-" + month + "-" + date + ".tif"));
} catch (e) {
alert("Error assigning images for import, stopping script\n" + e);
exit();
}
for (i = 1; i <= comics.length; i++) {
// Script label of the rectangles/pageitems to place the graphics into
var myRect = myDocument.pageItems.item("comic" + i);
try {
myRect.place(comics[i-1]);
} catch (e) {
alert(e);
}
myRect.fit(FitOptions.CONTENT_TO_FRAME);
}
}
However as soon as I hit the Import Comic button, I get the "ReferenceError: Object is invalid" error. My directory structures look ok to me. Any ideas?
thanks!
Watch this line:
var myRect = myDocument.pageItems.item("comic" + i);
In newest ID version it is no longer calling "item.label" but "item.name"
(the one shown in Layer Panel)
If inside your doc target rectangles have "label == comic + i" you have to repeat/move this values as rectangle's name as well.
Otherwise - your code needs to create a loop through all pageItems and check particular item.label before placing image.
Related
I'm trying to create files based on current time and date inside a scheduler.
Here's what I've tried so far.
var logFileNameScheduler = schedule.scheduleJob('*/2 * * * *', function () {
let date_ob = new Date();
let date = ("0" + date_ob.getDate()).slice(-2);
let month = ("0" + (date_ob.getMonth() + 1)).slice(-2);
let year = date_ob.getFullYear();
let hours = date_ob.getHours();
let minutes = date_ob.getMinutes();
var st = 'data';
logFileName = 'Error log report at : ' + date + "-" + month + "-" + year + " " + hours + ":" + minutes + '.txt';
fs.writeFile(logFilename, st, (err) => {
if (err) console.log(err);
console.log("Successfully Written to File.");
});
});
But it creates corrupted files. I can't open them.
I've been looking for a way to display the date the page last was updated.
Now I've been searching around, and everything points to the document.lastModified function, but however I've tried to fix it, it always shows the current date.
I've tried this example:
function lastModified() {
var modiDate = new Date(document.lastModified);
var showAs = modiDate.getDate() + "-" + (modiDate.getMonth() + 1) + "-" + modiDate.getFullYear();
return showAs
}
function GetTime() {
var modiDate = new Date();
var Seconds
if (modiDate.getSeconds() < 10) {
Seconds = "0" + modiDate.getSeconds(); }
else {
Seconds = modiDate.getSeconds(); }
var modiDate = new Date();
var CurTime = modiDate.getHours() + ":" + modiDate.getMinutes() + ":" + Seconds
return CurTime }
document.write("Last updated on ");
document.write(lastModified() + " # " + GetTime());
document.write(" [D M Y 24 Hour Clock]"); document.write("");
Or a simple one like this:
<SCRIPT LANGUAGE="JavaScript">
var t = new Date(document.lastModified);
document.write("<I>Last Updated: "+document.lastModified+"</I><BR>");
document.write("<I>Last Updated: "+t+"</I><BR>");
</SCRIPT>
Is there any other way to do this?
.. Without taking a 3 years tech-class?
Press here to see the scripts live
Because you are modifying it currently. Check this out for example.
To make this work based on your requirement, checkout this link and this link
check this it will help u
Put this on the page at the bottom:
<script type="text/javascript" src="js_lus.js"></script>
Name the file whatever you want. Example: js_lus.js Make sure src=""
path is correct for all your pages.
function lastModified() {
var modiDate = new Date(document.lastModified);
var showAs = modiDate.getDate() + "-" + (modiDate.getMonth() + 1) + "-" +
modiDate.getFullYear();
return showAs
}
function GetTime() {
var modiDate = new Date();
var Seconds
if (modiDate.getSeconds() < 10) {
Seconds = "0" + modiDate.getSeconds();
} else {
Seconds = modiDate.getSeconds();
}
var modiDate = new Date();
var CurTime = modiDate.getHours() + ":" + modiDate.getMinutes() + ":" + Seconds
return CurTime
}
document.write("Last updated on ")
document.write(lastModified() + " # " + GetTime());
document.write(" [D M Y 24 Hour Clock]")
document.write("");
I'm running the following script with CasperJS and after about 1/3rd of the way through the array it starts running out of swap space and the machine becomes extremely slow. What am i doing wrong here?
searchPages is an array of 54 numbers corresponding to a URL value for a search page.
casper.each(searchPages,function(casper,index){
loadSearch(casper,index);
});
function loadSearch(casper,index){
var currentTime = new Date();
var month = currentTime.getMonth() + 2;
var day = currentTime.getDate();
var year = currentTime.getFullYear();
var dateStart = month + "/" + day + "/" + year;
month = currentTime.getMonth() + 3;
var dateEnd = month + "/" + day + "/" + year;
casper.thenOpen(url,function(){
var myfile = "data-"+year + "-" + month + "-" + day+".html";
this.evaluate(function(j) {
document.querySelector('select[name="searchParameters.localeId"]').selectedIndex = j;
},index);
this.evaluate(function(start) {
$("#leaveDate").val(start);
},dateStart);
this.evaluate(function(end) {
$("#returnDate").val(end);
},dateEnd);
this.evaluate(function() {
$("#OSB_btn").click();
});
this.waitForSelector('#destinationForPackage', function() {
if (this.exists('#destinationForPackage')){
var name = casper.evaluate(function() {
return $("#destinationForPackage option[value='" + $("#destinationForPackage").val() + "']").text()
});
if (name != "Going To"){
if (name == null){
console.log("it's null");
}else{
name = name.replace("/","_");
casper.capture('Captures/Searches/search_' + name + '.jpg');
console.log("Capturing search_" + name);
}
}
}else{
console.log("Still doesn't exist...retry");
loadSearch(casper,index);
}
},function(){
console.log("Search page timed-out.");
},20000);
});
}
And it adds about 3GB per loop.
Well turns out this is a very well-known issue with PhantomJS. 3+ years as an open bug and apparently it has something to do with QT Webkit. Nonetheless, i was able to solve it by closing each page during the loop and re-opening a new Phantom page. It's a bit of a hacky work-around, but the memory consumption is far less. However, after about 200 pages, it still has a pretty high memory usage (1GB+). So, i break up my scripts into blocks of 200 and just start the next one upon completion. Here is the finished product that completes successfully without too much memory usage. It uses less on MacOS than Windows for some reason.
casper.start(url,function(){
this.echo('continuing captures...');
}).each(searchPages,function(casper,index){
loadSearch(this,index);
});
function loadSearch(casper,index){
var currentTime = new Date();
var month = currentTime.getMonth() + 1;
var day = currentTime.getDate() + 1;
var year = currentTime.getFullYear();
var dateStart = month + "/" + day + "/" + year;
var fortnightAway = new Date(+new Date + 12096e5);
var dateEnd = fortnightAway.getMonth() + 1 + "/" + fortnightAway.getDate() + "/" + fortnightAway.getFullYear();
casper.page.close();
casper.page = require('webpage').create();
casper.thenOpen(url,function(){
var myfile = "data-"+year + "-" + month + "-" + day+".html";
this.evaluate(function(j) {
document.querySelector('select[name="searchParameters.localeId"]').selectedIndex = j;
},index);
this.evaluate(function(start) {
$("#leaveDate").val(start);
},dateStart);
this.evaluate(function(end) {
$("#returnDate").val(end);
},dateEnd);
this.evaluate(function() {
$("#OSB_btn").click();
});
this.waitForSelector('#destinationForPackage', function() {
if (this.exists('#destinationForPackage')){
var name = casper.evaluate(function() {
return $("#destinationForPackage option[value='" + $("#destinationForPackage").val() + "']").text()
});
if (name != "Going To"){
if (name == null){
console.log("it's null");
}else{
name = name.replace("/","_");
name = name.replace("/","_");
casper.capture('Captures/Searches/search_' + name + '.jpg');
console.log("Capturing search_" + name);
}
}
}else{
console.log("Search failed to load. Retrying");
loadSearch(casper,index);
}
},function(){
console.log("Search page timed-out. Retrying");
loadSearch(casper,index);
},20000);
});
}
There might be a better solution to the original issue, but for a quick fix on running out of memory, try setTimeout to make the recursive call without winding up the stack...
setTimeout(() => loadSearch(casper,index), 0);
(This idea assumes that the memory issue is the result of too much recursive depth over a long wait time).
I'm running an update on a table to set a position. I've extracted the query and manually run it on my database and works fine but when passed through connection.query() it seems to think there's a syntax error in my node.js console.
function sendShipPosition(position) {
var input = '';
if (position.moving === true) {
var currentdate = new Date();
var datetime = currentdate.getFullYear() + "-"
+ (currentdate.getMonth()+1) + "-"
+ currentdate.getDate() + " "
+ currentdate.getHours() + ":"
+ currentdate.getMinutes() + ":"
+ currentdate.getSeconds();
var input = ', moving_datetime = ' + datetime;
}
connection.query('UPDATE ships SET x_axis = :x, y_axis = :y' + input + ' WHERE ship_id = :ship_id'), {
x: parseInt(position.x),
y: parseInt(position.y),
ship_id: 1
};
}
Here is the syntax error:
Here's the input data value of 'position' variable:
{ x: '-605', y: '-257', moving: 0 }
I hope I'm not being too much of a dunce and sorry for the low quality question.
Thanks
This function will generate SQL code which is missing quotes around the datetime variable, resulting in invalid SQL code.
function sendShipPosition(position) {
var input = '';
if (position.moving === true) {
var currentdate = new Date();
var datetime = currentdate.getFullYear() + "-"
+ (currentdate.getMonth()+1) + "-"
+ currentdate.getDate() + " "
+ currentdate.getHours() + ":"
+ currentdate.getMinutes() + ":"
+ currentdate.getSeconds();
# Here!
var input = ', moving_datetime = \'' + datetime + '\''
}
connection.query('UPDATE ships SET x_axis = :x, y_axis = :y' + input + ' WHERE ship_id = :ship_id'), {
x: parseInt(position.x),
y: parseInt(position.y),
ship_id: 1
};
}
I'm fairly new to javascript and I need to rename or add to an extension of a css and .png file. The script is embedded within an ETL process. I have a variable "prd" that holds the value of the file name that the style.css and picture.png file are derived from and I also need to add a date or time stamp to the end of the extension. Basically I'm wanting to concatenate prd+style_02_06_14.png
Desired results:
Prd = sales_report
File = style.css.
Result = "sales_report_style_02_06_14.png" and "sales_report_style_02_06_14.css"
Here is my code
var sourceCssFile = outputfolder + "style.css";
var destinationCssFile = outputfolder + css_pic;
if(isFolder(destinationCssFile) == false) {
createFolder(destinationCssFile);
var testvar = "inside";
}
destinationCssFile = destinationCssFile + "/style.css";
moveFile(sourceCssFile, destinationCssFile, true);
var sourceImageFile = outputfolder + "picture.png";
var destinationImageFile = outputfolder + css_pic + "/picture.png";
moveFile(sourceImageFile, destinationImageFile, true);
var cont = loadFileContent(output);
var replaceCss = css_pic + "tt+style.css";
var replaceImg = css_pic + "tt + picture.png";
cont = cont.replace("style.css", replaceCss);
cont = cont.replace("picture.png", replaceImg);
var filename = outputfolder + new_str;
Try this:
function formatResult(prd, filename, date, extension) {
return prd + '_' + filename + '_' + formatDate(d) + '.' + extension;
}
function formatDate(d) {
var year = d.getFullYear().toString().substr(2,2);
var month = (d.getMonth()+1) + '';
if (month.length == 1) {
month = "0" + month;
}
var day = d.getDate() + '';
if (day.length == 1) {
day = "0" + day;
}
return month + '_' + day + '_' + year;
}
Demo
http://jsfiddle.net/WHpuU/5/
Nota
alert( 'style.css'.replace(/\.css$/i, '') ); // shows 'style'