Find substring in Photoshop document name - javascript

I'm very new to Javascript and trying to create a script that will run an action if it finds a particular set of characters in the open document's name. Right now I've got this which doesn't do anything, but I think it might be on the right track?
var name = activeDocument.name
var mRP = "mrp"
var hits = []
for(var i = 0 ; i < name.length ; i++){
if(name[i] === mRP[0]){
for(var j = i ; j < i + mRP.length ; j++){
if(name.substring(j, j + mRP.length)===mRP)
hits.push(name.substring(j, j + mRP.length));
}
}
if(hits[0] === "mrp"){
app.doAction ('Make Crease', 'MR P')
}
}
I'm looking for the action to run if it finds mrp in the document's title.. usually structured like BC342D_mrp_cu

You can search through the activeDocument.name string using regular expression and just do your action every time it finds "mrp" substring.
var name = activeDocument.name
var regex = /mrp/gi;
while ( regex.exec(name) )
app.doAction ('Make Crease', 'MRP');
Here is fiddle: http://jsfiddle.net/g3hnnf3o/

Related

Any alternative way of using this .charAt()?

I have a problem, when I used input[index] in the statement the reverse word didn't match the value of inputString. But this works in C#. When I try it in JavaScript is not working. I want to learn what are the other alternative way of .char() method. Can anybody solve my problem?
strReverse = "";
input = document.getElementById("inputValue").value;
i = input.length;
for(var j = i; j >= 0; j--) {
strReverse = strReverse + input.charAt(j); // i used input[index]
}
If you wish to use input[index] instead of input.charAt() then you need to address the issue that you are setting i = input.length;
So when you enter your loop for the first iteration, j will be equal to i. Meaning that you are trying to access the character at the index equal to the length of the string (when you do input[j]). However, as arrays and string indexing starts at zero in javascript (and in most languages), there is no index at i (the length of the string), but, there is an index at i-1, which will give the last character in the string. Thus, if you want to reverse your array using input[j] you need to start j in your for loop as j = i - 1.
Take a look at the snippet for a working example:
strReverse = "";
input = "level";
i = input.length;
for(var j = i-1; j >= 0; j--) {
strReverse = strReverse + input[j]; // i used input[index]
}
if(strReverse === input) {
alert(input +" is a palindrome!")
} else {
alert(input +" is not a palindrome!");
}

Why does my loop only return first value from HTML collection

Despite research and trying any number of permutations I am only getting back the first input from the loop.
Chrome dev not showing any errors.
Any help much appreciated.
function DisplayInputValues(){
//var j is the number of fields generated dynamically as required by user
var j = document.getElementById('fields').value;
var usin; //class name of input element
var i
for (i=0;i<j.length; i++){
var userinput = document.getElementsByClassName('usin')[i].value;
}
document.getElementById("showresults").innerHTML=userinput;
}
Because you override the elements in your loop.
Use this something like this:
var userinput = '';
for (i=0;i<j.length; i++){
userinput += document.getElementsByClassName('usin')[i].value;
}
document.getElementById("showresults").innerHTML=userinput;
you have to set document.getElementById("showresults").innerHTML=userinput inside the loop:
for (i=0;i<j.length; i++){
var userinput = document.getElementsByClassName('usin')[i].value;
document.getElementById("showresults").innerHTML=userinput;
}
If you'd like to display value from each of the elements with class usin, then you would need to append their value in the loop.
Like this:
function DisplayInputValues() {
//var j is the number of fields generated dynamically as required by user
var j = document.getElementById('fields').value;
var i;
var userinput = [];
for (i = 0; i < j.length; i++) {
userinput.push(
document.getElementsByClassName('usin')[i].value
);
}
document.getElementById("showresults").innerHTML = userinput.join(', ');
}
I guess j is not an array. You don't have to do j.length. Just use j.
Your value J is a string.
It 's counted as "6" or "42". And if you ask length of that, it will return 1 or 2. And you will loop that times. try this: for(var i=0;i<parseInt(j);i++){...}

get string matching then store second string and store it

have to search for classname with string matching "Routed:" then get the string of the following classname in this case "W000000" in the image below. there are over 65 of these on the page at any given time i just need to get the first one and store that value.
You could use a for loop for the elements and get the next one.
var tds = document.getElementsByClassName('ms-cellstyle ms-vb2');
var found = false;
var firstValue = '';
for (var i = 0; i < tds.length && !found; i++) {
if (tds[i].innerHTML.indexOf('Routed') >= 0 && i < tds.length - 1) {
firstValue = tds[i + 1].innerHTML;
found = true;
}
}
console.log(firstValue);

Search Box Function Not Eliminating Correct Value

I am trying to make a simple website where the user types input into a search box, and every time a key is press, their input is compared against the first row of a 2 dimensional array which checks for character matches. If the character they input doesn't match anything, I want it to remove that specific bucket of the array. I have attempted to write basic code for this I thought would work, and have it up at the demo site linked. (Sorry I am just using a free host and havn't optimized the equation table at all so bear with it)
http://fakefakebuzz.0fees.net/
As you can see, the function is not eliminating the appropriate table rows. For example, typing "A" should not eliminate the "Average Current Equation" row because the first letter of that is A, which means matches should not = 0.
I have been looking through this code all morning, and cannot find where I went wrong. I also want to stick to vanilla js.
Any help?
Thanks so much.
I just debugged your code, and the function you use is narrowTable. first remove onkeypress from body node
<body onload="printTable()" onkeypress="narrowTable()">
and add onkeyup instead to you input, like this:
<input type="search" name="equationSearch" id="equationSearch"
placeholder="Equation Search" autofocus="" onkeyup="narrowTable()">
because when you use onkeypress the key value hasn't been added to the input box and your input value has no value in your function, which is:
function narrowTable() {
var newTableContent = "";
var matches = 0;
var input = document.getElementById("equationSearch").value;
//input has no value
for (var i = 0; i < tableData.length; i++) {
for (var j = 0; j < tableData[i][0].length; j++) {
if (input == tableData[i][0].charAt(j)) {
matches++;
}
}
if (matches == 0) {
tableData.splice(i, 1);
}
matches = 0;
}
for (var i = 0; i < tableData.length; i++) {
newTableContent += "<tr><td>" + tableData[i][0] + "</td><td>" + tableData[i][1] + "</td></tr>";
}
document.getElementById("table").innerHTML = newTableContent;
}
the other problem your code has is after printing your table, your tableData variable has changed because you have removed some of indexes. you should reset the tableData to its original value or you can do:
function narrowTable() {
//create a copy of your original array and use currenttableData instead
var currenttableData = tableData.slice();
var newTableContent = "";
var matches = 0;
//your code
}
the other problem here is the way you search for your input value:
for (var j = 0; j < tableData[i][0].length; j++) {
if (input == tableData[i][0].charAt(j)) {
matches++;
}
}
if (matches == 0) {
tableData.splice(i, 1);
}
you can easily do this, instead:
if(tableData[i][0].search("input") == -1){
tableData.splice(i, 1);
}
First, to check if a string is a substring of another string, you can use indexOf. It will return -1 if the string is not found in the other string.
Second, you shouldn't alter the array while you are still looping through it, unless you make sure to alter the counter variable (i in this case) appropriately.
var dataToRemove = [],
i;
for (i=0; i<tableData.length; i++) {
if(tableData[i][0].indexOf(input) == -1) {
// add the index to the to-be-removed array
dataToRemove.push(i);
}
// remove them in reverse order, so the indices don't get shifted as the array gets smaller
for(i = dataToRemove.length - 1; i >= 0; i--) {
tableData.splice(i, 1);
}
dataToRemove = [];
for (i=0; i<tableData.length; i++) {
newTableContent += "<tr><td>" + tableData[i][0] + "</td><td>" + tableData[i][1] + "</td></tr>";
}
I haven't tested this code, but it should at least give you a better idea of how to make this work.

Google Apps Script: How to fix this for loop to insert paragraphs of one google document to another?

So I'm arriving at last stops of my Apps journey (there'll be several others =).
The code bellow is a function that aims to do this: iterates through the paragraphs of one google text document and, when it finds in text some sinal, some paragraph (such as "Introduction", "Part 1 - Background" or "Part 2 - Biography") whose content is igual a theses_type, it inserts all theses_type paragraphs into the first document, after that sinal or theses_type keyword.
So, I'm trying to do this with the function "importTheses" (thanks #Serge insas and others for previous help!). But I'm having trouble after the line for( var k = 0; k < thesesParagraphs-1; ++k ). Even when I got through the log the boolean True, I can't get the paragraphs inserted. I also can't get the log of this line: Logger.log("thesesDoc.getText() = " + thesesElement.getText() );. So, any help or hint will be very appreciated.
function importTheses(targetDocId, thesesId, thesesType) { // just a name, I used it to analyse docs
var targetDoc = DocumentApp.openById(targetDocId);
var targetDocParagraphs = targetDoc.getParagraphs();
var targetDocElements = targetDoc.getNumChildren();
var thesesDoc = DocumentApp.openById(thesesId);
var thesesParagraphs = thesesDoc.getParagraphs();
var thesesElements = thesesDoc.getNumChildren();
Logger.log("targetDocId = " + targetDocId);
Logger.log("thesesId = " + thesesId);
Logger.log("thesesType = " + thesesType);
var elTargetDoc=[];
var elTheses=[];
for (var j = 0; j < targetDocElements; ++j ) {
var targetDocElement = targetDoc.getChild(j);
Logger.log("targetDoc.getChild(j) = " + targetDocElement);// to see targetDoc's content
elTargetDoc[j]=targetDocElement.getText();
if(elTargetDoc[j] == thesesType){
Logger.log("elTargetDoc[j]== " + elTargetDoc[j]);
Logger.log("thesesType " + thesesType);
Logger.log("if(elTargetDoc[j]== thesesType)" + (elTargetDoc[j]== thesesType) );
for( var k = 0; k < thesesParagraphs-1; ++k ) {
var thesesElement = thesesDoc.getChild(k);
Logger.log("thesesDoc.getChild(k) " + thesesDoc.getChild(k));
Logger.log("thesesDoc.getText() = " + thesesElement.getText() );
elTheses[k] = thesesElement.getText();
targetDoc.insertParagraph(j, elTheses[k]);
}
}
}
}
for( var k = 0; k < thesesParagraphs-1; ++k ) { .. }
In this line of code, you're intention is to loop using k, over integer values starting at 0. The loop should run at least once, as long as thesesParagraphs is 2 or more... and is a number.
That second condition is your problem. Earlier in the function, you had this:
var thesesParagraphs = thesesDoc.getParagraphs();
... so thesesParagraphs is an Array of Paragraph objects, not a number. You are probably interested in the count of paragraphs:
for( var k = 0; k < thesesParagraphs.length-1; ++k ) { .. }
^^^^^^^
Or perhaps thesesElements was what you intended to use to bound your loop, since it's a number.

Categories

Resources