I want to add names of files of a specific folder to an JS's array, but nothing happens:
var pics = new Array();
var x;
var fs = new ActiveXObject("Scripting.FileSystemObject");
alert('x');
var fo = fs.GetFolder(Server.MapPath("C:\wamp\www\newsite\ErfanGhiasiPanel\Slider Images"));
for (x in fo.files){
pics.push(x.Name);
}
For instance, whet I insert an
alert('something')
after var fs = new ActiveXObject... or next lines, it won't appear.
What you guys think?
Thank you
Assuming JScript + Classic ASP due to the MapPath (which you dont need in your case) you need to escape the path string;
var pics = [];
var fs = new ActiveXObject("Scripting.FileSystemObject");
var fo = new Enumerator(fs.GetFolder("C:\\wamp\\www\\newsite\\ErfanGhiasiPanel\\Slider Images").Files);
for (; !fo.atEnd(); fo.moveNext()) {
pics.push(fo.item(0).Name)
}
Related
So I found this old web application (asp classic) in my workplace and been asked to modified it. What i'm trying to do is, I want to display all files from this particular folder. Instead of hardcoding each of the file name with its link, I tried as below:
<%
var fs = new ActiveXObject("Scripting.FileSystemObject");
var fo = new ActiveXObject("Scripting.FileSystemObject");
var f = new ActiveXObject("Scripting.FileSystemObject");
var theFile = new ActiveXObject("Scripting.FileSystemObject");
fo=fs.GetFolder("C:\\inetpub\\wwwroot\\edocument\\MyFiles");
f = fo.Files;
For Each theFile in f
Response.write(theFile.Name+"<br>");
Next
%>
seems like For Each loop won't work/not recognized in asp javascript but found some working example using vbscript. I also tried to access the collection directly:
Response.write(f[0].Name);
but it says ...is null or not an object
Any idea how can I access this Files Collection?
In javascript you need an Enumerator() to walk the collection, and either use for next, or while like in the example below
<% #LANGUAGE="JScript" %>
<%
var fso = Server.CreateObject("Scripting.FileSystemObject");
var fo = fso.GetFolder("C:\\inetpub\\wwwroot\\edocument\\MyFiles");
var f = new Enumerator(fo.Files);
f.moveFirst()
while (!f.atEnd()) {
Response.Write(f.item().name + "<BR>");
f.moveNext();
}
fo = null;
f = null;
fso = null;
%>
I am running javascript via Photoshop's scripts tool/option in order to batch process a folder of images. The script adds a second image layer from a single image in folder 02 to a number of images in folder 01. That works fine, however, I am trying to decrement the opacity of the layer that is added, so that the image that is added from folder 02 is increasingly more transparent for each subsequent image processed from folder 01. I'm basically creating a fade that transitions from the image in folder 02 to revealing the images processed from folder 01. The opacity change is happening as expected (decrementing opacity for each processed image); however, they seem to get processed out of order, so as I view the images in order, the opacity of that added layer jumps around. I can also see that as the newly processed images save to the processed image folder, they arrive out of order (something like 1, 3, 2, 5, 6, 4; though it might be different each time). I suspect I need to force my code to process the images one at a time in order, but I'm not sure how to go about that. Thanks in advance! I don't believe file naming is the issue since I've tried naming the images in folder 01 as both 1, 2, 3, (and so on) as well as 01, 02, 03. But that doesn't help.
Thanks in advance!
#target photoshop
// FOLDERS
var folder1 = new Folder('~/Desktop/1/');
var folder2 = new Folder('~/Desktop/2/');
var saveFolder = new Folder('~/Desktop/done/');
//
var searchMask = '*.???'; // get files named as this
var list1 = folder1.getFiles(searchMask);
var list2 = folder2.getFiles(searchMask);
var psdOptions = new PhotoshopSaveOptions();
psdOptions.layers = true;
opacityLevel = 100;
for (var i = 0; i < list1.length; i++) {
var doc = open(list1[i]);
var docName = doc.name;
placeFile('~/Desktop/2/1.jpg', 100); // for adding only one file each source file
doc.activeLayer.blendMode = BlendMode.LIGHTEN; // ## change BLEND MODE
placeFile('~/Desktop/2/2.png', 100); // for adding only one file each source file
doc.activeLayer.blendMode = BlendMode.MULTIPLY; // ## change BLEND MODE
doc.activeLayer.opacity = opacityLevel; // set layer opacity
// Decrement opacity level for each image
if (doc.activeLayer.opacity > 5) {
opacityLevel = Math.round(doc.activeLayer.opacity) - 5;
}
// SAVING
doc.saveAs(new File(saveFolder + '/' + docName.split('.')[0] + '.psd'), psdOptions);
doc.close(SaveOptions.DONOTSAVECHANGES);
};
function placeFile(file, scale) {
try {
var idPlc = charIDToTypeID("Plc ");
var desc2 = new ActionDescriptor();
var idnull = charIDToTypeID("null");
desc2.putPath(idnull, new File(file));
var idFTcs = charIDToTypeID("FTcs");
var idQCSt = charIDToTypeID("QCSt");
var idQcsa = charIDToTypeID("Qcsa");
desc2.putEnumerated(idFTcs, idQCSt, idQcsa);
var idOfst = charIDToTypeID("Ofst");
var desc3 = new ActionDescriptor();
var idHrzn = charIDToTypeID("Hrzn");
var idPxl = charIDToTypeID("#Pxl");
desc3.putUnitDouble(idHrzn, idPxl, 0.000000);
var idVrtc = charIDToTypeID("Vrtc");
var idPxl = charIDToTypeID("#Pxl");
desc3.putUnitDouble(idVrtc, idPxl, 0.000000);
var idOfst = charIDToTypeID("Ofst");
desc2.putObject(idOfst, idOfst, desc3);
var idWdth = charIDToTypeID("Wdth");
var idPrc = charIDToTypeID("#Prc");
desc2.putUnitDouble(idWdth, idPrc, scale);
var idHght = charIDToTypeID("Hght");
var idPrc = charIDToTypeID("#Prc");
desc2.putUnitDouble(idHght, idPrc, scale);
var idAntA = charIDToTypeID("AntA");
desc2.putBoolean(idAntA, true);
executeAction(idPlc, desc2, DialogModes.NO);
}
catch (e) { }
}//end function placeFile
Never count on file list functions to maintain a logical order...
Try sorting the file lists after creating them.
var list1 = folder1.getFiles(searchMask);
var list2 = folder2.getFiles(searchMask);
list1.sort();
list2.sort();
Make sure the filenames sort ascibetically. Zerofix if you use numbers.
Alternatively, you can supply Array.sort() with a sorting function to allow for a custom sorting. Read more about how that works here.
I am trying to modify original PSD and then delete the original one and only want to save as a new jpg. My code is working fine with this line:
activeDocument.close(SaveOptions.DONOTSAVECHANGES); // Close Original Image
But when I replace above line with this line:
psd.remove(); // I want to delete Original file
It giving me remove() is not a function error.
Here is the complete script. I have tired to read Photoshop JS Guide 2015 and also google this issue but I didn't find any answer.
var defaultRulerUnits = preferences.rulerUnits;
preferences.rulerUnits = Units.PIXELS;
if (documents.length >= 1){
var hres = 0;
var vres = 0;
var OldName = activeDocument.name.substring(0, activeDocument.name.indexOf('.'));
var CurrentFolder = activeDocument.path;
var psd = app.activeDocument;
hres = activeDocument.width;
vres = activeDocument.height;
activeDocument.selection.selectAll();
if (activeDocument.layers.length >1) {
activeDocument.selection.copy(true);
}
else{
if (activeDocument.layers.length =1) {
activeDocument.selection.copy(false);
}
}
psd.remove(); // I want to delete Original file
var newDoc = documents.add(hres, vres, 72, OldName, NewDocumentMode.RGB, DocumentFill.WHITE);
newDoc.paste();
jpgFile = new File(CurrentFolder + "/" + OldName+ ".jpg" );
jpgSaveOptions = new JPEGSaveOptions();
jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE;
jpgSaveOptions.matte = MatteType.NONE;
jpgSaveOptions.quality = 12;
newDoc.saveAs(jpgFile, jpgSaveOptions, true, Extension.LOWERCASE);
}
A little late to the thread, but hope this helps someone else.
The remove method expects a file location. You can accomplish removing the original file like this:
var activeDoc = app.activeDocument;
var docPath = new Folder(activeDoc.path);
var psd = new File(docPath + "/" + activeDoc.name);
...
psd.remove();
Edit: Meant to also include a link to this handy ESTK reference doc where I learned about working with the File object: http://estk.aenhancers.com/3%20-%20File%20System%20Access/file-object.html?highlight=delete
srcDoc.activeLayer.remove();
Will delete the active layer. There's no .remove() method to delete a file.
I'm new in code with Javascript and I have a problem with an array I need to create.
var tabLine = new Array();
var tabCol = new Array();
var tabComplet = new Array();
var fso, f1, ts, s, cl, ln;
var ForReading = 1;
var i = 0;
function ReadFiles() {
fso = new ActiveXObject("Scripting.FileSystemObject");
// Read the contents of the file.
ts = fso.OpenTextFile("PathToAFile\TextFile.txt", ForReading);
s = ts.ReadAll();
tabLine = s.split('\n');
cl = tabLine.length;
ts.Close();
for (i = 0; i <tabLine.length; i++) {
var tabCol = tabLine[i].split("\t");
for (j=0;j<tabCol.length;j++) {
tabComplet[i,j] = tabCol[j];
alert(tabComplet[i,j]);
}
}
alert(tabComplet[10,5]);
alert(tabComplet[3,5]);
}
ReadFiles();
The file I need to read is a text file; It have many line and each line have information separate by tabulation.
This function read the text file and convert it into an array in two dimensions.
I had some alert to check the contain of my array.
The first alert give me the result I want (it display the content of the array witch is different for each element in the array)but the two other give me the same result. I check with another alert and, in the for boucle, these two element are different.
I make more test and all happen like if my array only have the same line copy/paste.
Thanks in advance for all information that can help me.
Here is an example of file I use :
http://www.k-upload.fr/afficher-fichier-2017-05-18-1b0cfa685testimport2.txt.html
Usually there are bigger than this one but for test it's OK.
Can anyone fill in the blanks here.
I have been trying to get a script I could run to query all available users in the Global Catalog for active directory and finally managed it in VBS -looking for any particular username as below:
Const ADS_SECURE_AUTHENTICATION = 1
Set oGC = GetObject("GC:")
For Each child In oGC
Set oEntrprise = child
Exit For
Next
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.CreateTextFile("AD.txt", True)
' Setup ADO.
Set oConn = CreateObject("ADODB.Connection")
Set oComm = CreateObject("ADODB.Command")
oConn.Provider = "ADsDSOObject"
oConn.Properties("ADSI Flag") = ADS_SECURE_AUTHENTICATION
oConn.Open
oComm.ActiveConnection = oConn
' Set the search command and filter.
objFile.WriteLine(oEntrprise.ADsPath)
oComm.CommandText = "<" & oEntrprise.ADsPath & ">;(&(objectCategory=person)(objectClass=user)(givenName=aaron*));cn,distinguishedName;subTree"
' Execute the query.
Set oRS = oComm.Execute
' Print the results.
oRS.MoveFirst
While Not oRS.EOF
For Each field In oRS.Fields
objFile.WriteLine(field)
Next
objFile.WriteLine("")
oRS.MoveNext
Wend
WScript.Echo "Finished"
Im now trying to convert it to JS but I cannot replicate it.
I cannot find the golden answer for looping through GetObject("GC:"). For each doesnt seem to work like for like in this case. Is anyone aware of how to do this?
So in effect i need the JS equivelant of oEntrprise in the above script.
var oConn = WScript.CreateObject("ADODB.Connection");
var oComm = WScript.CreateObject("ADODB.Command");
var keyname = "samaccountname";
var keyvalue = "aaron";
oConn.Provider = "ADsDSOObject";
oConn.Properties("ADSI Flag") = 1;
oConn.Open;
oComm.ActiveConnection = oConn;
var objRootDSE = GetObject("GC:");
for (var i = 0; i < objRootDSE.length; i++) {
WriteToFile("Moahhh");
var oEntrprise = objRootDSE[i];
oComm.CommandText = "<" + oEntrprise.ADsPath + ">;(&(objectCategory=person)(objectClass=user)(givenName=a*));cn,distinguishedName;subTree";
var oRS = oComm.Execute;
}
function WriteToFile(sText){
var fso = new ActiveXObject("Scripting.FileSystemObject");
var FileObject = fso.OpenTextFile("C:\\builds\\LogFile.txt", 8, true,0); // 8=append, true=create if not exist, 0 = ASCII
FileObject.write(sText)
FileObject.close()
}
In JScript you need to use an Enumerator to step over the elements of a collection
var objRootDSE = GetObject('GC:');
for (var childs = new Enumerator(objRootDSE) ; !childs.atEnd(); childs.moveNext()){
var child = childs.item();
WScript.Echo( child.Name );
};
Thanks for the suggested answer - answers the impossible loop I could not solve but I have now found a way to query the Global Catalogue for users directly without a need for the loop:
var aoi = WScript.CreateObject("ADSystemInfo");
var gcBase = aoi.ForestDNSName;
var ado = WScript.CreateObject("ADODB.Connection");
ado.Provider = "ADSDSOObject";
ado.Open;
WriteToFile(aoi.ForestDNSName);
var objectList = ado.Execute("<GC://" + gcBase + ">;(&(objectCategory=person)(objectClass=user)("+keyname+"="+keyvalue+"*));cn,distinguishedName;subTree");
if(!objectList.EOF)
{
WriteToFile(objectList("distinguishedName").value);
}
function WriteToFile(sText){
var fso = new ActiveXObject("Scripting.FileSystemObject");
var FileObject = fso.OpenTextFile("C:\\LogFile.txt", 8, true,0); // 8=append, true=create if not exist, 0 = ASCII
FileObject.write(sText)
FileObject.close()
}
Never again.
Whoever finds this I will help Google get here - Global Catalog JavaScript query for Active Directory!