-
1. Re: Dynamic If Statment
dgolberg Mar 23, 2012 12:48 AM (in response to dgolberg)Seems I've come up with a stumper. I'm not even sure if this is doable, but if anyone has any ideas or knows if it's doable or not; let me know. The script basically just needs to check for files whose tags match the first file's tags and open them in Photoshop so we can compare and get rid of any duplicates. The biggest obstacle being the random number of tag categories per file category.
-
2. Re: Dynamic If Statment
Michael L Hale Mar 23, 2012 10:00 AM (in response to dgolberg)I think you have way too many conditions to check with just one if statement. My guess is that you will need multiple if statements and it may also help to have a switch statement.
It's hard to make a suggestion to the logic flow you will need because I an not clear what data those arrays hold.
-
3. Re: Dynamic If Statment
dgolberg Mar 23, 2012 12:28 PM (in response to Michael L Hale)Hey Michael, thanks for the reply. The array labelled "baseTag" holds the data for a single row in a .csv file (for simplicity, we'll say it's the very first row). The array labelled "tag" holds the data for all the remaining rows of the .csv file. So for example, the .csv might look something like below when opened with a spreadsheet program:
File Name Tag1 Tag2 Tag3 FileName1.format wood dark strong FileName2.format wood light strong FileName3.format wood medium light FileName4.format wood dark strong FileName1.format is the file we're doing the check on using the tag columns to the right, and if it finds a match using those tags, it opens the file listed in column 1. So the script would need to make sure all tags match the first row's tags (in this example, only FileName4.format would match FileName1.format's tags). However, there may be instances where I wish to exclude one of the columns from the check (for example, only check for matches of tag 1 and 3). So this would require that the if statement's check only look for matches of the tag1 and tag3 columns while ignoring Tag2 (so now FileName2.format would also match). Essentially, instead of being:
if(baseTag[1] == tag[1] && baseTag[2] == tag[2] && baseTag[3] == tag[3]) { open(tag[0]); }
it would then be:
if(baseTag[1] == tag[1] && baseTag[3] == tag[3]) { open(tag[0]); }
if I decide to have it exclude the tag2 column.
The issue I'm having is making this change to the if statement be dynamic (without a huge mess of inefficient code). Your mention of a switch statement sounds interesting. I'm not the greatest programmer yet (pretty much just the really basic stuff) so I hadn't heard about this statement yet; but I'll certainly be looking it up now. Anyway, hope the info helps clarify it a little better.
-
4. Re: Dynamic If Statment
Michael L Hale Mar 23, 2012 2:01 PM (in response to dgolberg)1 person found this helpfuloK, you can do this in one if statement but I think it is hard to read and understand.
var tag1 = true; var tag2 = true; var tag3 = false; var baseTag = ['test.jpg','wood','dark','strong']; var tag = ['test.jpg','wood','dark','light']; if((!tag1 || baseTag[1] == tag[1]) && (!tag2 || baseTag[2] == tag[2] ) && ( !tag3 || baseTag[3] == tag[3] ) /*etc*/) { alert('load file'); }
You have have if statement that uses the AND operator. In order for that to test true all the expressions need to test true. So for each expression you create another set of expression that uses the OR operator. With the OR operator if either are true it test true.
So if you want baseTag[1] == tag[1] to test true if tag1 is false( skip this tag ) you create an expression that will test true if tag1 == false. !tag1 || baseTag[1] == tag[1] You then wrap with () so it makes one expression as part of your AND comparisons
-
5. Re: Dynamic If Statment
Paul Riggott Mar 23, 2012 3:11 PM (in response to dgolberg)Would something like this work?
var w = new Window('dialog','tag test'); w.cb1 = w.add('checkbox',undefined,'Use Tag 1'); w.cb2 = w.add('checkbox',undefined,'Use Tag 2'); w.cb3 = w.add('checkbox',undefined,'Use Tag 3'); w.bu1 = w.add('button',undefined,'Cancel'); w.bu2 = w.add('button',undefined,'Process'); w.bu2.onClick=function(){ w.close(1); var csvFile = File.openDialog("Open Comma-delimited File","comma-delimited(*.csv):*.csv;"); csvFile.open('r'); var Data = csvFile.read(); csvFile.close(); Data = Data.split('\n'); Data.shift(); //remove header line //alert(Data[0]); var maintag = Data.shift().split(','); var string1 =''; if(w.cb1.value) string1 += maintag[1].toString().replace(/^\s+|\s+$/g,''); if(w.cb2.value) string1 += maintag[2].toString().replace(/^\s+|\s+$/g,''); if(w.cb3.value) string1 += maintag[3].toString().replace(/^\s+|\s+$/g,''); //alert(string1); while(Data.length>1){ var sectag = Data.shift().split(','); var string2 =''; if(w.cb1.value) string2 += sectag[1].toString().replace(/^\s+|\s+$/g,''); if(w.cb2.value) string2 += sectag[2].toString().replace(/^\s+|\s+$/g,''); if(w.cb3.value) string2 += sectag[3].toString().replace(/^\s+|\s+$/g,''); if(string1 == string2) $.writeln(sectag[0].toString()); }//end while } w.show(); /**************CSV file.***************************** File Name,Tag1,Tag2,Tag3 FileName1.format,wood ,dark,strong FileName2.format,wood,light, strong FileName3.format,wood,medium, light FileName4.format,wood, dark , strong *****************************************************/
-
6. Re: Dynamic If Statment
dgolberg Mar 23, 2012 5:16 PM (in response to Paul Riggott)Hmm, both methods look very promising. I'll give them a try and see how it goes. Thanks guys!
-
7. Re: Dynamic If Statment
dgolberg Apr 2, 2012 12:11 PM (in response to dgolberg)Well, it was a tough call, but both methods worked quite well. Michael's method was a little easier to implement, while Paul's was a little bit more dynamic. As a result, I decide to give the "Correct" answer credit to Paul, though both are technically correct (if I could give it to 2 people, I would!). Thanks again for the help guys! It is very much appreciated!
Edit: The thing that makes Pauls more dynamic is the ability to incorporate it with a for loop so that the length can be determined by an array's length, where as the other method is easier to implement, but limited to the scope of the defined if statement.