Issue looping through an array response - arrays

I was writing an API using the kat.cr API and the IMDB api too in nodejs, I didn't use json.stringify cuz I didn't know about it at the time of writing haha XD, anyway, the problem is that when I loop through the code in 46 through 50, the response remains the same here's an example,
here is the json generated,
{
"MovieList": [{
"title": "Jurassic World",
"imdb": "tt0369610",
"poster_med": "http://ia.media-imdb.com/images/M/MV5BMTQ5MTE0MTk3Nl5BMl5BanBnXkFtZTgwMjczMzk2NTE#._V1_SX300.jpg",
"poster_big": "http://ia.media-imdb.com/images/M/MV5BMTQ5MTE0MTk3Nl5BMl5BanBnXkFtZTgwMjczMzk2NTE#._V1_SX300.jpg",
"genres": ["Action, Adventure, Sci-Fi"],
"items": [{
"torrent_magnet": "magnet:?xt=urn:btih:9D8BB2F07BC40BE4EA8EDAB7F7EB9C70A8AB2892&dn=maze+runner+the+scorch+trials+2015+hc+720p+hdrip+x264+shaanig&tr=udp%3A%2F%2Ftracker.publicbt.com%2Fannounce&tr=udp%3A%2F%2Fopen.demonii.com%3A1337",
"torrent_seeds": "1262",
"torrent_peers": "1306",
"id": "9D8BB2F07BC40BE4EA8EDAB7F7EB9C70A8AB2892"
}]
}, {
"title": "San Andreas",
"imdb": "tt2126355",
"poster_med": "http://ia.media-imdb.com/images/M/MV5BNjI4MTgyOTAxOV5BMl5BanBnXkFtZTgwMjQwOTA4NTE#._V1_SX300.jpg",
"poster_big": "http://ia.media-imdb.com/images/M/MV5BNjI4MTgyOTAxOV5BMl5BanBnXkFtZTgwMjQwOTA4NTE#._V1_SX300.jpg",
"genres": ["Action, Drama, Thriller"],
"items": [{
"torrent_magnet": "magnet:?xt=urn:btih:9D8BB2F07BC40BE4EA8EDAB7F7EB9C70A8AB2892&dn=maze+runner+the+scorch+trials+2015+hc+720p+hdrip+x264+shaanig&tr=udp%3A%2F%2Ftracker.publicbt.com%2Fannounce&tr=udp%3A%2F%2Fopen.demonii.com%3A1337",
"torrent_seeds": "1262",
"torrent_peers": "1306",
"id": "9D8BB2F07BC40BE4EA8EDAB7F7EB9C70A8AB2892"
}]
}]
}
Here is the crawler code :
var kat = require('kat-api');
var IMDb = require('imdb-scraper');
var movieTitle = require('movie-title');
var nameToImdb = require("name-to-imdb");
var movie = require('node-movie');
var fs = require('fs');
var util = require('util');
var log_file = fs.createWriteStream(__dirname + '/main.json', {
flags: 'w'
});
var log_stdout = process.stdout;
var config = '720p 2015'; //This is the line that should be changed if needed!
console.log = function(d) { //
log_file.write(util.format(d) + '\n');
log_stdout.write(util.format(d) + '\n');
};
var kat = require('kat-api');
kat.search({
query: config,
category: 'movies',
language: 'en'
}).then(function(response) {
var quotes = '"';
var startingOfJson = "{" + quotes + "MovieList" + quotes + ":" + "[";
var endingOfJson = "}";
var itemStart = quotes + "items" + quotes + ":" + "[{";
var itemEnd = "}]";
console.log(startingOfJson);
for (i = 0; i <= 20; i++) {
var titleForEverything = movieTitle(response.results[i].title);
movie(titleForEverything, function(err, data) {
console.log("{");
console.log(quotes + "title" + quotes + ":" + quotes + data.Title + quotes + ",");
console.log(quotes + "imdb" + quotes + ":" + quotes + data.imdbID + quotes + ",");
console.log(quotes + "poster_med" + quotes + ":" + quotes + data.Poster + quotes + ",");
console.log(quotes + "poster_big" + quotes + ":" + quotes + data.Poster + quotes + ",");
var genres = quotes + "genres" + quotes + ":" + "[" + quotes + data.Genre + quotes + "]" + ",";
console.log(genres);
console.log(itemStart);
console.log(quotes + "torrent_magnet" + quotes + ":" + quotes + response.results[i].magnet + quotes + ",");
console.log(quotes + "torrent_seeds" + quotes + ":" + quotes + response.results[i].seeds + quotes + ",");
console.log(quotes + "torrent_peers" + quotes + ":" + quotes + response.results[i].peers + quotes + ",");
console.log(quotes + "id" + quotes + ":" + quotes + response.results[i].hash + quotes);
console.log(itemEnd);
if (i == 20) {
console.log("}");
} else {
console.log("},")
}
});
}
}).catch(function(error) {
console.log('an error occured' + error);
});
console.log("]}");
and you can see that the magnet, seeds, hash and peers remain the same for all the results generated! How can I fix this and why does this happen? Thank you! :D

You're committing the classic error of a function inside a loop closing over the loop index i; when the function is executed, i will already have its final value. The easiest way to fix this is with for (let i.

Related

Build a dynamic string to filter a rest call

I'm trying to build a dynamic string but I'm facing a problem, so I kindly ask help to the community.
A have a string to build a rest call filter, but I'm lost with the and's
This query only works if only one filter condition is provided, but I have to provide as AND
var query;
var queryDefault = "IsLastForLevelAndParticipant eq 1";
this.state.participantFirstName
? query += "substringof('" + this.state.participantFirstName + "',Participant/FirstName)"
: queryDefault
this.state.participantLastName
? query += "substringof('" + this.state.participantLastName + "',Participant/LastName)"
: queryDefault
So let´s see.
If I start building this and only one filter is provided I'll have a plus and
var query;
var queryDefault = "IsLastForLevelAndParticipant eq 1";
this.state.participantFirstName
? query += "substringof('" + this.state.participantFirstName + "',Participant/FirstName) and "
: queryDefault
this.state.participantLastName
? query += "substringof('" + this.state.participantLastName + "',Participant/LastName)"
: queryDefault
query += " and " + queryDefault
I have 12 filters and I must know how many have values in order to provide the and clause
This is my state
//Filters Certificates
startEmission: string;
endEmission: string;
startValidity: string;
endValidity: string;
participantFirstName: string;
participantLastName: string;
paticipantCertNumber: string;
selectYesNo: string;
selectLevel: string;
Any help is a bonus.
Add all possible filters to an array, filter the falsy values and finally join all items with " and ".
var query = [
queryDefault,
this.state.participantFirstName && "substringof('" + this.state.participantFirstName + "',Participant/FirstName)"
this.state.participantLastName && "substringof('" + this.state.participantLastName + "',Participant/LastName)"
]
.filter(item => !!item)
.join(" and ");
BTW. are you using OData? the structure of the filter looks familiar. If you do, I would recommend you to use this library: https://github.com/techniq/odata-query#readme
Since I'm using datetime also, here it is the final solution
var queryDefault = "IsLastForLevelAndParticipant eq 1";
var query = [
queryDefault,
this.state.startEmission && "(Date1 ge datetime'" + moment.utc(this.state.startEmission).toISOString() + "') and (Date1 le datetime'"
+ moment.utc(this.state.endEmission).toISOString() + "')",
this.state.startValidity && "(Validto ge datetime'" + moment.utc(this.state.startValidity).toISOString() + "') and (Validto le datetime'"
+ moment.utc(this.state.endValidity).toISOString() + "')",
this.state.participantFirstName && "(substringof('" + this.state.participantFirstName + "',Participant/FirstName))",
this.state.participantLastName && "(substringof('" + this.state.participantLastName + "',Participant/LastName))",
this.state.selectYesNo && "IsPrinted eq " + this.state.selectYesNo,
this.state.selectLevel && "Level/Title eq '" + this.state.selectLevel + "'"
]
.filter(filter => !! filter)
.join(" and ");
var q = JSON.stringify(query).substring(1).slice(0, -1);
console.log(JSON.stringify(query));

fnDrawCallback how to loop through each row

How do i loop through the enitre datset within the in datatables method?
"fnFooterCallback": function (nFoot, aData, iStart, iEnd, aiDisplay) {
$(aData).each(function (index) {
$(".it").append("<div class='mapdata' id='" + nFoot + "B' title='" + aData[index][14] + "|" + aData[index][15] + "' >");
});
},
The above works if the pagination is removed. But if iDisplayLength is set to 25 for example then it only goes through 25 records, not all 100 etc.
"fnFooterCallback": function (nFoot, aData, iStart, iEnd, aiDisplay) {
$(aData).each(function (index) {
$(".it").append("<div class='mapdata' id='" + nFoot + "B' title='" + aData[index][14] + "|" + aData[index][15] + "' data-id='true' data-id2='" + aData[index][1] + "' data-isapprove='" + aData[index][12] + "' ><div class='gTab1'><div class='info'><div class='maptitle'>" + aData[index][1] + "</div><div class='location'></div></div></div ></div >");
});
},
The above did it

Email only new rows in Google sheets

I'm editing the [Send single email with values from all new rows in a spreadsheet (Google Script / GAS). The function works as far as updating the Sheet, but the e-mail that is sent sends the info for all rows. I'd like it to only send e-mails from rows that have not yet been labeled 'email_fwd'.
function sendEmail() {
//setup function
var ActiveSheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var StartRow = 1;
var RowRange = ActiveSheet.getLastRow() - StartRow + 1;
var WholeRange = ActiveSheet.getRange(StartRow,1,RowRange,17);
var AllValues = WholeRange.getValues();
var message = "";
//iterate loop
for (i in AllValues) {
//set current row
var CurrentRow = AllValues[i];
//define column to check if sent
var EmailSent = CurrentRow[17];
//if row has been sent, then continue to next iteration
if (EmailSent == "email_fwd")
continue;
//set HTML template for information
message +=
"<p><b>E-mail: </b>" + CurrentRow[1] + "</p>" +
"<p><b>Days: </b>" + CurrentRow[2] + "</p>" +
"<p><b>First Name: </b>" + CurrentRow[3] + "</p>" +
"<p><b>Last Name: </b>" + CurrentRow[4] + "</p>" +
"<p><b>Address: </b>" + CurrentRow[5] + "</p>" +
"<p><b>City: </b>" + CurrentRow[6] + "</p>" +
"<p><b>State: </b>" + CurrentRow[7] + "</p>" +
"<p><b>Phone: </b>" + CurrentRow[8] + "</p>" +
"<p><b>Country: </b>" + CurrentRow[9] + "</p>" +
"<p><b>Phone: </b>" + CurrentRow[10] + "</p>" +
"<p><b>Place: </b>" + CurrentRow[11] + "</p>" +
"<p><b>Rank: </b>" + CurrentRow[12] + "</p>" +
"<p><b>Emergency Contact: </b>" + CurrentRow[13] + "</p>" +
"<p><b>Emergency Contact Phone: </b>" + CurrentRow[14] + "</p>" +
"<p><b>Questions & Comments: </b>" + CurrentRow[15] + "</p><br><br>";
//set the row to look at
var setRow = parseInt(i) + StartRow;
//mark row as "sent"
ActiveSheet.getRange(setRow, 17).setValue("email_fwd");
}
//define who to send grants to
var SendTo = "someaddress#yahoo.com"; // + "," + "emailaddress2#gmail.com";
//set subject line
var Subject = "RE: Registrations";
//send the actual email
MailApp.sendEmail({
to: SendTo,
cc: "",
subject: Subject,
htmlBody: message,
});
}
I think I just need to reset the StartRow = FIRST ROW THAT IS NOT FOUND WITH email_fwd but I'm not sure how to do that. Little help.
I ended up doing this from https://developers.google.com/apps-script/articles/sending_emails with the typo fix ActiveSheet for third text line from the bottom.
// This constant is written in column C for rows for which an email
// has been sent successfully.
function sendEmails2() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2; // First row of data to process
var numRows = 2; // Number of rows to process
// Fetch the range of cells A2:B3
var dataRange = sheet.getRange(startRow, 1, numRows, 3)
// Fetch values for each row in the Range.
var data = dataRange.getValues();
for (var i = 0; i < data.length; ++i) {
var row = data[i];
var emailAddress = row[0]; // First column
var message = row[1]; // Second column
var emailSent = row[2]; // Third column
if (emailSent != EMAIL_SENT) { // Prevents sending duplicates
var subject = "Sending emails from a Spreadsheet";
MailApp.sendEmail(emailAddress, subject, message);
**ActiveSheet**.getRange(startRow + i, 3).setValue(EMAIL_SENT);
// Make sure the cell is updated right away in case the script is interrupted
SpreadsheetApp.flush();
}
}
}
Please change
var EmailSent = CurrentRow[17];
to
var EmailSent = CurrentRow[16];
getRange's index start from 1. When you access JavaScript array for row, index start from 0.
Hint. You can use Logger.log(CurrentRow[17]); for debugging.
This should do it unless there were other problems.
function sendEmail()
{
var ActiveSheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var StartRow = 2;
var RowRange = ActiveSheet.getLastRow() - StartRow + 1;
var WholeRange = ActiveSheet.getRange(StartRow,1,RowRange,17);//17 is length of WholeRange so max index is 16
var AllValues = WholeRange.getValues();
var message = "";
for (var i=0;i<AllValues.length;i++)
{
var CurrentRow = AllValues[i];
var EmailSent = CurrentRow[16];
if (EmailSent!="email_fwd")
{
message +=
"<p><b>E-mail: </b>" + CurrentRow[1] + "</p>" +
"<p><b>Days: </b>" + CurrentRow[2] + "</p>" +
"<p><b>First Name: </b>" + CurrentRow[3] + "</p>" +
"<p><b>Last Name: </b>" + CurrentRow[4] + "</p>" +
"<p><b>Address: </b>" + CurrentRow[5] + "</p>" +
"<p><b>City: </b>" + CurrentRow[6] + "</p>" +
"<p><b>State: </b>" + CurrentRow[7] + "</p>" +
"<p><b>Phone: </b>" + CurrentRow[8] + "</p>" +
"<p><b>Country: </b>" + CurrentRow[9] + "</p>" +
"<p><b>Phone: </b>" + CurrentRow[10] + "</p>" +
"<p><b>Place: </b>" + CurrentRow[11] + "</p>" +
"<p><b>Rank: </b>" + CurrentRow[12] + "</p>" +
"<p><b>Emergency Contact: </b>" + CurrentRow[13] + "</p>" +
"<p><b>Emergency Contact Phone: </b>" + CurrentRow[14] + "</p>" +
"<p><b>Questions & Comments: </b>" + CurrentRow[15] + "</p><br><br>";
var setRow = i + StartRow;
ActiveSheet.getRange(setRow, 17).setValue("email_fwd");
var SendTo = "someaddress#yahoo.com";
var Subject = "RE: Registrations";
MailApp.sendEmail({to: SendTo,cc: "",subject: Subject,htmlBody: message,});
}
}
}
My guess is that your headers look like this:
The numbers on the second row should be your indexes so it's possible that they should all be reduced by one. Perhaps you can share your spreadsheet with us.

How to add extra column to protractor-html-screenshot-reporter like duration for each "it" block

I am trying to add an extra column to the HTML report. Like duration of each "it" block. I tried this inside jsonparser.js file.
phssr.makeHTMLPage = function(tableHtml, reporterOptions){
var styleTag = phssr.makeHardCodedStyleTag(reporterOptions);
var scrpTag = phssr.makeScriptTag();
var staticHTMLContentprefix = "<html><head><meta charset='utf-8'/>";
//Add title if it was in config setup
if (typeof (reporterOptions.docTitle) !== 'undefined' && _.isString(reporterOptions.docTitle) ){
staticHTMLContentprefix += "<title>" + reporterOptions.docTitle + "</title>";
} else {
staticHTMLContentprefix += "<title></title>";
}
staticHTMLContentprefix += styleTag + scrpTag + " </head><body>";
staticHTMLContentprefix += "<h1>" + reporterOptions.docHeader + "</h1><table class='header'>";
staticHTMLContentprefix += "<tr><th class='desc-col'>Description</th><th class='status-col'>Passed</th>";
staticHTMLContentprefix += "<th class='browser-col'>Browser</th>";
staticHTMLContentprefix += "<th class='os-col'>OS</th><th class='msg-col'>Message</th>";
staticHTMLContentprefix += "<th class='msg-col'>Duration</th>";
staticHTMLContentprefix += "<th class='img-col'>Screenshot</th></tr></table>";
var staticHTMLContentpostfix = "</body></html>";
var htmlComplete = staticHTMLContentprefix + tableHtml + staticHTMLContentpostfix;
return htmlComplete;
}
phssr.getTimestamp = function (date) {
function pad(n) { return n < 10 ? '0' + n : n; }
var currentDate = date !== undefined ? date : new Date(),
month = currentDate.getMonth() + 1,
day = currentDate.getDate();
return (currentDate.getFullYear() + "-" + pad(month) + "-" + pad(day) + " " + pad(currentDate.getHours()) + ":" + pad(currentDate.getMinutes()) + ":" + pad(currentDate.getSeconds()));
}
var sTime = new Date();
var startTime = phssr.getTimestamp(sTime);
console.log("sTime***********************",startTime);
var passCount=0, failCount=0, loopCount=0;
function generateHTML(data){
var eTime = new Date();
var endTime = phssr.getTimestamp(eTime);
console.log("eTime***********************",endTime);
var diffTime = (eTime-sTime)/1000;
console.log("dTime***********************",diffTime);
data.passed? passCount++: failCount++;
var str = '<table><tr>';
str += '<td class="desc-col">' + data.desc + '</td>';
var bgColor = data.passed? 'green': 'red';
str += '<td class="status-col" style="color:#fff;background-color: '+ bgColor+'">' + data.passed + '</td>';
str += '<td class="browser-col">' + data.browser.name+ ':' +data.browser.version + '</td>';
str += '<td class="os-col">' + data.os + '</td>';
var stackTraceInfo = data.passed? '': '<br/><a onclick="showTrace(event)" href="#trace-modal'+loopCount+'">View Stack Trace Info</a><br/> <div id="#trace-modal'+loopCount+'" class="traceinfo"><div>X' + data.trace + '</div></div>';
str += '<td class="msg-col">' + data.message+ stackTraceInfo+ '</td>';
str += '<td class="msg-col">' + diffTime + '</td>';
if(!(reporterOptions.takeScreenShotsOnlyForFailedSpecs && data.passed)) {
str += '<td class="img-col">View </td>';
}
else{
str += '<td class="img-col"></td>';
}
str += '</tr></table>';
loopCount++;
return str;
}
I can see the extra column in the report. The problem was it is not showing the correct time. In the console it is printing like this
sTime*********************** 2015-08-28 13:26:44
Using the selenium server at http://localhost:4444/wd/hub
.eTime*********************** 2015-08-28 13:26:48
dTime*********************** 3.312
.eTime*********************** 2015-08-28 13:26:52
dTime*********************** 7.984
eTime*********************** 2015-08-28 13:26:52
dTime*********************** 7.984
.
Finished in 12.123 seconds
3 tests, 3 assertions, 0 failures
eTime*********************** 2015-08-28 13:26:57
dTime*********************** 12.325
eTime*********************** 2015-08-28 13:26:57
dTime*********************** 12.325
eTime*********************** 2015-08-28 13:26:57
dTime*********************** 12.325
here, I don't know why it is executing two times. In the report it is showing 12.325 for all blocks. I don't know where I am doing wrong. please help me.
Tests:
describe('Title', function() {
it('TESTCASE-1 : Should have a title', function() {
expect(browser.getTitle()).toContain('Test');
});
it('TESTCASE-2 : Should accept a valid email address and password', function() {
element(by.id('email')).sendKeys('*********');
element(by.id('password')).sendKeys('*********');
element(by.css('.btn')).click();
expect(browser.getCurrentUrl()).toEqual('*********/app/#/home');
});
it('TESTCASE-4: Dashboard Selection.....', function(){
var menubutton = element.all(by.css('.btn')).get(0);
menubutton.click();
expect(browser.getCurrentUrl()).toEqual('*************************/students/1');
});
});

Script to export layer coordinates to excel

I have found a script that export my layers coordinates form photoshop CS5 to XML
I hope somebody here can help me to edit that script to record coordinates to xls file?
Also if is possible to have each coordinates on separate row will be great.
Below is script I want to modify to do what I need.
//
// This script exports extended layer.bounds information to [psd_file_name].xml
// by pattesdours
//
function docCheck() {
// ensure that there is at least one document open
if (!documents.length) {
alert('There are no documents open.');
return; // quit
}
}
docCheck();
var originalRulerUnits = preferences.rulerUnits;
preferences.rulerUnits = Units.PIXELS;
var docRef = activeDocument;
var docWidth = docRef.width.value;
var docHeight = docRef.height.value;
var mySourceFilePath = activeDocument.fullName.path + "/";
// Code to get layer index / descriptor
//
cTID = function(s) { return app.charIDToTypeID(s); };
sTID = function(s) { return app.stringIDToTypeID(s); };
function getLayerDescriptor (doc, layer) {
var ref = new ActionReference();
ref.putEnumerated(cTID("Lyr "), cTID("Ordn"), cTID("Trgt"));
return executeActionGet(ref)
};
function getLayerID(doc, layer) {
var d = getLayerDescriptor(doc, layer);
return d.getInteger(cTID('LyrI'));
};
var stackorder = 0;
// function from Xbytor to traverse all layers
traverseLayers = function(doc, ftn, reverse) {
function _traverse(doc, layers, ftn, reverse) {
var ok = true;
for (var i = 1; i <= layers.length && ok != false; i++) {
var index = (reverse == true) ? layers.length-i : i - 1;
var layer = layers[index];
if (layer.typename == "LayerSet") {
ok = _traverse(doc, layer.layers, ftn, reverse);
} else {
stackorder = stackorder + 1;
ok = ftn(doc, layer, stackorder);
}
}
return ok;
};
return _traverse(doc, doc.layers, ftn, reverse);
};
// create a string to hold the data
var str ="";
// class using a contructor
function cLayer(doc, layer) {
//this.layerID = Stdlib.getLayerID(doc, layer);
this.layerID = getLayerID(doc, layer);
//alert("layer ID: " + this.layerID);
this.layerWidth = layer.bounds[2].value - layer.bounds[0].value;
this.layerHeight = layer.bounds[3].value - layer.bounds[1].value;
// these return object coordinates relative to canvas
this.upperLeftX = layer.bounds[0].value;
this.upperLeftY = layer.bounds[1].value;
this.upperCenterX = this.layerWidth / 2 + layer.bounds[0].value;
this.upperCenterY = layer.bounds[1].value;
this.upperRightX = layer.bounds[2].value;
this.upperRightY = layer.bounds[1].value;
this.middleLeftX = layer.bounds[0].value;
this.middleLeftY = this.layerHeight / 2 + layer.bounds[1].value;
this.middleCenterX = this.layerWidth / 2 + layer.bounds[0].value;
this.middleCenterY = this.layerHeight / 2 + layer.bounds[1].value;
this.middleRightX = layer.bounds[2].value;
this.middleRightY = this.layerHeight / 2 + layer.bounds[1].value;
this.lowerLeftX = layer.bounds[0].value;
this.lowerLeftY = layer.bounds[3].value;
this.lowerCenterX = this.layerWidth / 2 + layer.bounds[0].value;
this.lowerCenterY = layer.bounds[3].value;
this.lowerRightX = layer.bounds[2].value;
this.lowerRightY = layer.bounds[3].value;
// I'm adding these for easier editing of flash symbol transformation point (outputs a 'x, y' format)
// because I like to assign shortcut keys that use the numeric pad keyboard, like such:
// 7 8 9
// 4 5 6
// 1 2 3
//
this.leftBottom = this.lowerLeftX + ", " + this.lowerLeftY;
this.bottomCenter = this.lowerCenterX + ", " + this.lowerCenterY;
this.rightBottom = this.lowerRightX + ", " + this.lowerRightY;
this.leftCenter = this.middleLeftX + ", " + this.middleLeftY;
this.center = this.middleCenterX + ", " + this.middleCenterY;
this.rightCenter = this.middleRightX + ", " + this.middleRightY;
this.leftTop = this.upperLeftX + ", " + this.upperLeftY;
this.topCenter = this.upperCenterX + ", " + this.upperCenterY;
this.rightTop = this.upperRightX + ", " + this.upperRightY;
// these return object coordinates relative to layer bounds
this.relUpperLeftX = layer.bounds[1].value - layer.bounds[1].value;
this.relUpperLeftY = layer.bounds[0].value - layer.bounds[0].value;
this.relUpperCenterX = this.layerWidth / 2;
this.relUpperCenterY = layer.bounds[0].value - layer.bounds[0].value;
this.relUpperRightX = this.layerWidth;
this.relUpperRightY = layer.bounds[0].value - layer.bounds[0].value;
this.relMiddleLeftX = layer.bounds[1].value - layer.bounds[1].value;
this.relMiddleLeftY = this.layerHeight / 2;
this.relMiddleCenterX = this.layerWidth / 2;
this.relMiddleCenterY = this.layerHeight / 2;
this.relMiddleRightX = this.layerWidth;
this.relMiddleRightY = this.layerHeight / 2;
this.relLowerLeftX = layer.bounds[1].value - layer.bounds[1].value;
this.relLowerLeftY = this.layerHeight;
this.relLowerCenterX = this.layerWidth / 2;
this.relLowerCenterY = this.layerHeight / 2;
this.relLowerRightY = this.layerHeight;
this.relLowerRightX = this.layerWidth;
this.relLowerRightY = this.layerHeight;
return this;
}
// add header line
//str = "<psd filename=\"" + docRef.name + "\" path=\"" + mySourceFilePath + "\" width=\"" + docWidth + "\" height=\"" + docHeight + "\">\n";
// now a function to collect the data
function exportBounds(doc, layer, i) {
var isVisible = layer.visible;
var layerData = cLayer(doc, layer);
// if(isVisible){
// Layer object main coordinates relative to its active pixels
var str2 = leftTop // this is the
// + "\" layerwidth=\"" + layerData.layerWidth
// + "\" layerheight=\"" + layerData.layerHeight
// + "\" transformpoint=\"" + "center" + "\">" // hard-coding 'center' as the default transformation point
+"\" \"" + layer.name + ".png" + "</layer>\n" // I have to put some content here otherwise sometimes tags are ignored
str += str2.toString();
};
//};
// call X's function using the one above
traverseLayers(app.activeDocument, exportBounds, true);
// Use this to export XML file to same directory where PSD file is located
var mySourceFilePath = activeDocument.fullName.path + "/";
// create a reference to a file for output
var csvFile = new File(mySourceFilePath.toString().match(/([^\.]+)/)[1] + app.activeDocument.name.match(/([^\.]+)/)[1] + ".xls");
// open the file, write the data, then close the file
csvFile.open('w');
csvFile.writeln(str + "</psd>");
csvFile.close();
preferences.rulerUnits = originalRulerUnits;
// Confirm that operation has completed
alert("Operation Complete!" + "\n" + "Layer coordinates were successfully exported to:" + "\n" + "\n" + mySourceFilePath.toString().match(/([^\.]+)/)[1] + app.activeDocument.name.match(/([^\.]+)/)[1] + ".xml");
Change
var str2 = leftTop // this is the
// + "\" layerwidth=\"" + layerData.layerWidth
// + "\" layerheight=\"" + layerData.layerHeight
// + "\" transformpoint=\"" + "center" + "\">" // hard-coding 'center' as the default transformation point
+"\" \"" + layer.name + ".png" + "</layer>\n" // I have to put some content here otherwise sometimes tags are ignored
str += str2.toString();
to
var str2 = leftTop + ","+ layer.name + "\n"
str += str2.toString();
and
var csvFile = new File(mySourceFilePath.toString().match(/([^\.]+)/)[1] + app.activeDocument.name.match(/([^\.]+)/)[1] + ".xls");
to
var csvFile = new File(mySourceFilePath.toString().match(/([^\.]+)/)[1] + app.activeDocument.name.match(/([^\.]+)/)[1] + ".csv");
This works great for me!

Resources