ProjectItem.moveBin() not working correctly Extends Script (Adobe) - javascript

im currently developping a script with extends script which is a javascript-similar script language, that we use to automate tasks in Adobe's Softwares (here its premiere pro)
Here is what my code is doing :
-i import files from a folder in my computer
-then i put them in a bin in my prpro project
-and for each of this file i create a bin with the file and a sequence
My problem is that the last file is not move in a bin
here is my code
root = app.project.rootItem
newFilesPath = "C:/Path"
newFilesBin = root.children[0]
app.project.importFiles(newFilesPath, true, newFilesBin, false)
app.project.save()
newFilesBin = root.children[0]
for(i=0; i<newFilesBin.children.numItems; i++){
newFile = newFilesBin.children[0]
name = newFile.name
newBin = root.createBin(name)
file.moveBin(newBin)
}
So if i put several file in the path it doesnt move the last file instead of moving every files
Thanks for help

I found the solution :
i just change the parameters of my for loop
for(i=-1; i<newFilesBin.children.numItems+1; i++)
I dont exatly know why its working but it works

Related

Running a script on all .ai files in a directory

I'm a graphic designer and a beginner at coding. So please dumb down for me :)
I have a script that I run through Illustrator. It embeds an image and removes a clipping mask on the file. I need this script to run on multiple .ai files in a folder.
The thing is, I cannot create a batch action (in Illustrator) to run the script. Our IT guy doesn't want to add the script to our Illustrator settings on multiple accounts. Is there a way to add code to my script to do this?
I need it to open all the files, run the script and save. The files can be left open on Illustrator, no need to close.
What code do I need to add to my script?
I am on a Mac not PC.
It can be done this way:
function my_script() {
// copy a full text of your script here
// or include the jsx file this way:
# include '~/Desktop/script/my_script.jsx'
}
function main() {
var folder = Folder.selectDialog('Please, select the folder');
if (!(folder instanceof Folder)) return;
var files = folder.getFiles('*.ai');
if (files.length == 0) {
alert('Folder doesn\'t content AI files');
return;
}
var i = files.length;
while (i--) {
var doc = app.open(files[i]);
my_script(); // <------- here your script is running
doc.save();
doc.close();
}
alert(files.length + ' files were processed');
}
main();
If you have subfolders with .ai files inside your chosen folder and you need to access them in the script, you may find this JS snippet useful as well:
https://gist.github.com/joonaspaakko/df2f9e31bdb365a6e5df

App Script Tool: Trying to get all files URLs in the folder

I`m trying to get output of all the files URLs form the current folder, but I only get URL of the first file from the forlder here:
function listFolderContents() {
var sheet = SpreadsheetApp.getActive().getSheetByName('Hub 2');
var foldername = sheet.getRange('D160').getValue();
var linkoutput = sheet.getRange('W160');
var folders = DriveApp.getFoldersByName(foldername);
var folder = folders.next();
var contents = folder.getFiles();
Logger.log(linkoutput);
var file;
var link;
while(contents.hasNext()) {
file = contents.next();
link = file.getUrl();
linkoutput.setValue(link);
}
};
3 more things:
I`d like to have this script looped through the whole column, so it would check each folder name in column D and output files links in the column W;
the best outcome would be if it could output all the 3 links(as each folder would have only 3 files anyways) in the range of W:Y of the same row with the foder name;
and the last if this script could run automatically without a need to open Scripts tool and running the code every time.
thanks in advance!
For this question:
and the last if this script could run automatically without a need to open Scripts tool and running the code every time. thanks in advance!
You can set a Trigger to run when the spreadsheet is opened. Or you could set time triggers, to run daily for example.
Regarding the code, It looks like your are setting the link url always to the same cell (W60).

Illustrator JS Script Run without app.activeDocument

I have been using Illustrators API to create a script that is able to export .ai files swatches into a JSON format.
This however is all by having to open the Illustrator file and click
File > Scripts > Run My Script.
This is something that is very tedious and was wondering if there was a way in order to take the the files location (file path) and just execute the program using something like Node which will just use my already existing code and my AI file.
Currently I have something that looks along the lines of:
var Exporter = function() {
this.swatchGroup = 'ClientColours';
this.myApp = app.activeDocument;
this.chosenSwatchGroup();
this.writeFile(); };
What I am thinking is there not a way to instead of having this.myApp = app.activeDocument; to rather have something like this.myApp = Path(../my location);?
I have been making us of this documentation Jongware and Adobe Documentation just cant seem to find the answer I am looking for in order to get closer to this sort of automation.
EDIT
Did some digging and found the application documentation and there is a 'path' property just not sure on its implementation. Documentation
Yes, you can do this by providing path. But you have to open that file and that file will be your activeDocument. For eg :
var file = File("../your location");
app.open(file);
this.myApp = app.activeDocument;
By this, the file at your location will get open in Illustrator and will be activeDocument.

Change img src with javascript

Hey guys I need some help
Im trying to change the src of an image tag but when change it using my code it adds the word file to the start of my string.
The final result is file:///C:/xampp/....
instead of just C:/xampp/....
Here is my code
for(var i = 0; i < images.length; i++) {
alert(images[i].src);
srcList.push(images[i].src);
var index = srcList[i].search("JavaBridge");
tempPath.push(srcList[i].slice(index-1, srcList[i].length));
finalPath.push("C:/xampp/tomcat/webapps"+tempPath[i]);
document.getElementById(images[i].id).src = " "+finalPath[i];
alert(images[i].src);
}
Thanks in Advance
For file manager the physical path is C:/xampp/.... but browser cannot use that directly. So, this is a default action and you cannot override it. Try to open a file with browser and see the url .. you will find file:/// appended before the path.
When you change this path to url.. the file:/// will not come.
One quick workaround is to use a server and load files using server.. You can easily setup server using apache (Xampp, Wamp) or Node (Gulp Task)

Cannot write to file in Titanium

I am having trouble writing to a file in Titanium Studio.
specifically .json file. Code is compiled through and no exception was thrown.
Here is my relevant section of code, I parse the file to var first before adding element and stringify it to be written back.
Reading works perfectly, so is adding element, it's the writing process that has issues
var file = Ti.Filesystem.getFile(Ti.Filesystem.resourcesDirectory,'data.json');
var jsontext = file.read().toString();
var jsondoc = JSON.parse(jsontext);
jsondoc['feedlist'].push({
"picloc":imagename,
"title":titlef.value,
"desc1":descf1.value,
"desc2":descf2.value,
"desc3":descf3.value
});
jsontext = JSON.stringify(jsondoc);
file.write(jsontext); // write(data,[append])
Note: I have consulted Documentation and done some of my own search, some are suggesting that "Filestream" should be used in place of normal file along with .close(), I have yet got them working but it could be pointers the solution, if anyone knows how to get it working
Thanks in advance.
EDIT: This question is flagged for duplication, initially I deemed that was 2 separate issues, one was about merely writing text to a file. Another is parsing event.media (picture) into a file.
I got it working now, The issue was that I was trying to write to file in read-only directory
Ti.Filesystem.resourcesDirectory: A read-only directory where your application resources are located
Ti.Filesystem.applicationDataDirectory: A read/write directory accessible by your app. Place your application-specific files in this directory.
The contents of this directory persist
until you remove the files or until the user uninstalls the application
Here is my code, directory is modified
var sesfile = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory,'data2.json');
var jsontext = sesfile.read().toString();
var jsondoc = JSON.parse(jsontext);
jsondoc['feedlist'].push({
"picloc":imagename,
"title":titlef.value,
"desc1":descf1.value,
"desc2":descf2.value,
"desc3":descf3.value
});
jsontext = JSON.stringify(jsondoc);
sesfile.write(jsontext,false);
If you are unable to locate data directory and simply want to load the file from there.
(In my case it does not exist in project nor will be created with Webpreview compilings)
You can do bootstrap-ish type instruction like this first
var rdfile = Ti.Filesystem.getFile(Ti.Filesystem.resourcesDirectory,'data.json');
var sesfile = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory,'data2.json');
var jsontext = rdfile.read().toString();
var jsondoc = JSON.parse(jsontext);
sesfile.write(jsontext);
hope it helps whomever makes amateur mistake like I did.

Categories