Skip to main content
Vinicius Baptista
Inspiring
December 3, 2015
Answered

app.open(); Doubts

  • December 3, 2015
  • 1 reply
  • 1128 views

Hello guys

I wrote a script to open files of a specific folder, simple. Just get some items from a folder and insert it to a listbox, but when there is no one document open in illustrator the script works, and when there is a document open not works.

And I would like open multiple files using the multiselection function of listbox, but not work, only open the first item of selection.

I think the problem is in app.open() function, maybe it's not a best way to do this.

This is the script:

var win = new Window('palette', 'Open documents');

win.alignChildren = 'fill';

var folder = Folder.selectDialog ("Select a folder to open your files.");

var list = win.add('listbox', undefined, '', {multiselect: true, numberOfColumns: 1, showHeaders: true, columnTitles: ['Path']});

list.preferredSize = [400, 200];

var btnOpen = win.add('button', undefined, 'Open selected');

var files = folder.getFiles ();

for (i = 0; i < files.length; i++){

    var populateList = list.add ('item', files);

};

btnOpen.onClick = function (){

for (i = 0; i < list.selection.length; i++){

    var openSel = app.open( new File(list.selection));

//    alert('Testing loop');

};   

};

win.show();

I'm very grateful if anyone can help me, please!

Thanks in advance, best regards,

-Vinícius

This topic has been closed for replies.
Correct answer Silly-V

EDIT: Forget everything I said!

Your code was fine!

Problem was that you are having a window of type "palette" and it doesn't use the Illustrator DOM anymore, so you have to send the function through BridgeTalk object.

Try this. Notice that because bt functions are sending a whole script as a string, the filePath variable has to be encased inside either single or double quotes within the string to be of proper syntax.

#target illustrator

function test(){

    function btFunc_openFile(filePath){

        var bt = new BridgeTalk();

        bt.target = "illustrator";

        bt.body = "function myScript(){app.open(File('" + filePath + "'))};myScript();";

        bt.send();

    };

    var win = new Window('palette', 'Open documents'); 

    win.alignChildren = 'fill'; 

    var folder = Folder.selectDialog ("Select a folder to open your files."); 

    var list = win.add('listbox', undefined, '', {multiselect: true, numberOfColumns: 1, showHeaders: true, columnTitles: ['Path']}); 

    list.preferredSize = [400, 200]; 

    var btnOpen = win.add('button', undefined, 'Open selected'); 

    var files = folder.getFiles ("*.pdf"); 

    for (var i = 0; i < files.length; i++){ 

        var populateList = list.add ('item', decodeURI(files.fsName)); 

    }; 

    btnOpen.onClick = function (){ 

        for (var i = 0; i < list.selection.length; i++){ 

            var filePath = list.selection.text;

            btFunc_openFile(filePath);

        };     

    }; 

    win.show(); 

}

test();

1 reply

Silly-V
Legend
December 3, 2015

SHouldn't it be list.items instead of list.selection?

Vinicius Baptista
Inspiring
December 4, 2015

Hi Silly, thanks for your time and collaboration,

Your suggestion doesn't work to me, look I tried to changed the btnOpen.onClick function using list.items:

btnOpen.onClick = function (){ 

for (i = 0; i < list.items.length; i++){

    if (list.items.selected){

//        alert(list.items);

    var openSel = app.open( new File(list.items)); 

    };

};     

}; 

but, failed!

I think the problem certainly is on app.open() function, if you can, please try test my code, when I hide the function app.open() and use whatever thing, the code works without a problem! Look:

btnOpen.onClick = function (){ 

for (i = 0; i < list.items.length; i++){

    if (list.items.selected){

        alert(list.items);

//    var openSel = app.open( new File(list.items)); 

    };

};     

}; 

Note: I done this script to filtering .pdf files in a list and open only the selected. But I'm bugging my brain with this simple task! Now I'm not so sure if it's possible.

Please somebody help me! lol

Thanks.

Best regards

-Vinícius

Silly-V
Silly-VCorrect answer
Legend
December 4, 2015

EDIT: Forget everything I said!

Your code was fine!

Problem was that you are having a window of type "palette" and it doesn't use the Illustrator DOM anymore, so you have to send the function through BridgeTalk object.

Try this. Notice that because bt functions are sending a whole script as a string, the filePath variable has to be encased inside either single or double quotes within the string to be of proper syntax.

#target illustrator

function test(){

    function btFunc_openFile(filePath){

        var bt = new BridgeTalk();

        bt.target = "illustrator";

        bt.body = "function myScript(){app.open(File('" + filePath + "'))};myScript();";

        bt.send();

    };

    var win = new Window('palette', 'Open documents'); 

    win.alignChildren = 'fill'; 

    var folder = Folder.selectDialog ("Select a folder to open your files."); 

    var list = win.add('listbox', undefined, '', {multiselect: true, numberOfColumns: 1, showHeaders: true, columnTitles: ['Path']}); 

    list.preferredSize = [400, 200]; 

    var btnOpen = win.add('button', undefined, 'Open selected'); 

    var files = folder.getFiles ("*.pdf"); 

    for (var i = 0; i < files.length; i++){ 

        var populateList = list.add ('item', decodeURI(files.fsName)); 

    }; 

    btnOpen.onClick = function (){ 

        for (var i = 0; i < list.selection.length; i++){ 

            var filePath = list.selection.text;

            btFunc_openFile(filePath);

        };     

    }; 

    win.show(); 

}

test();