Skip to main content
New Participant
January 26, 2022
Answered

create zip file via JSX

  • January 26, 2022
  • 2 replies
  • 1462 views

hello everyone!

I am planning to create a zip file containing a list of images generated from a PSD using "JSX"
is there a way to zip all exported images via Javascript

Thanks

Correct answer Lumigraphics

I haven't written the Mac function yet, ditto is the best approach but there are some issues with it, I may have to use OSAScript/AppleScript and the System Archive Utility instead. But this works for Windows at least. Apache-licensed if you feel like forking it.

 

/*
Utility Pack Scripts created by David M. Converse ©2018-21

This script calls system functions to create a zip archive
Windows-only at present

Last modifed 6/7/2021

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#target bridge
if(BridgeTalk.appName == 'bridge'){
    if(Folder.fs == 'Windows'){ //Windows-only for now
        //create menu
        var archCommand = MenuElement.create('command', 'Create Zip Archive...', 'at the end of Tools');
        var archCommandc = MenuElement.create('command', 'Create Zip Archive...', 'after Thumbnail/Open', this.menuID); //add to Contextual menu

        archCommand.onSelect = function(){
            zipArchive();
            }
        
        archCommandc.onSelect = function(){
            zipArchive();
            }
        
        function zipArchive(){
            try{
                var sels = app.document.selections;
                if(sels.length == 0){
                    return;
                    }
                if(Folder.fs == 'Windows'){
                    var inputName = '\'' + sels[0].spec.fsName + '\'';
                    for(var i = 1; i < sels.length; i++){
                        inputName = inputName + ' , ' + '\'' + sels[i].spec.fsName + '\'';
                        }
                    inputName =  inputName.replace(/\//g, '\\');
                    var archFile = new File('~/Desktop/archive.zip').saveDlg('Create New ZIP Archive', '*.zip'); //create text file
                    var archName = '\'' + archFile.fsName + '\'';
                    archName = archName.replace(/\//g, '\\');
                    app.synchronousMode = true;
                    var archivestr = 'CreateObject("Wscript.Shell").Run "powershell -NoLogo -NoProfile -Command Compress-Archive -LiteralPath '+ inputName +' -DestinationPath ' + archName + '", 0, True';
                    var vbstempFile = new File(Folder.temp + '/vbstemp.vbs');
                    vbstempFile.open ('w'); //save vbs file
                    vbstempFile.write(archivestr);
                    vbstempFile.close();
                    File(vbstempFile).execute(); //execute vbs file
                    $.sleep(1500);
                    File(vbstempFile).remove();
                    app.synchronousMode = false;
                    }
                else{
                    alert('Mac implementation not yet available. Please stay tuned for updates.');
                    return;
                    }
                }
            catch(e){
                alert(e + e.line);
                }
            }
        }
    }

 

2 replies

Brainiac
January 26, 2022

You could end up with a HUGE zip file. It is possible, I have a Bridge script in development that zips a set of files via command line.

Are you on Windows or Mac?

New Participant
January 26, 2022

Thanks for the reply

I will have to use both it depends on the user machine
I am trying to execute this on a windows machine

var cmd = 'powershell -Command "Compress-Archive -Path %cd%\application\slices* -DestinationPath %cd%\application\zip2.zip"';
app.system(cmd); 

I don't know if it works or I can run a batch that does the job for me

any advice?

LumigraphicsCorrect answer
Brainiac
January 26, 2022

I haven't written the Mac function yet, ditto is the best approach but there are some issues with it, I may have to use OSAScript/AppleScript and the System Archive Utility instead. But this works for Windows at least. Apache-licensed if you feel like forking it.

 

/*
Utility Pack Scripts created by David M. Converse ©2018-21

This script calls system functions to create a zip archive
Windows-only at present

Last modifed 6/7/2021

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#target bridge
if(BridgeTalk.appName == 'bridge'){
    if(Folder.fs == 'Windows'){ //Windows-only for now
        //create menu
        var archCommand = MenuElement.create('command', 'Create Zip Archive...', 'at the end of Tools');
        var archCommandc = MenuElement.create('command', 'Create Zip Archive...', 'after Thumbnail/Open', this.menuID); //add to Contextual menu

        archCommand.onSelect = function(){
            zipArchive();
            }
        
        archCommandc.onSelect = function(){
            zipArchive();
            }
        
        function zipArchive(){
            try{
                var sels = app.document.selections;
                if(sels.length == 0){
                    return;
                    }
                if(Folder.fs == 'Windows'){
                    var inputName = '\'' + sels[0].spec.fsName + '\'';
                    for(var i = 1; i < sels.length; i++){
                        inputName = inputName + ' , ' + '\'' + sels[i].spec.fsName + '\'';
                        }
                    inputName =  inputName.replace(/\//g, '\\');
                    var archFile = new File('~/Desktop/archive.zip').saveDlg('Create New ZIP Archive', '*.zip'); //create text file
                    var archName = '\'' + archFile.fsName + '\'';
                    archName = archName.replace(/\//g, '\\');
                    app.synchronousMode = true;
                    var archivestr = 'CreateObject("Wscript.Shell").Run "powershell -NoLogo -NoProfile -Command Compress-Archive -LiteralPath '+ inputName +' -DestinationPath ' + archName + '", 0, True';
                    var vbstempFile = new File(Folder.temp + '/vbstemp.vbs');
                    vbstempFile.open ('w'); //save vbs file
                    vbstempFile.write(archivestr);
                    vbstempFile.close();
                    File(vbstempFile).execute(); //execute vbs file
                    $.sleep(1500);
                    File(vbstempFile).remove();
                    app.synchronousMode = false;
                    }
                else{
                    alert('Mac implementation not yet available. Please stay tuned for updates.');
                    return;
                    }
                }
            catch(e){
                alert(e + e.line);
                }
            }
        }
    }

 

Kukurykus
Brainiac
January 26, 2022

Play with methods for decompression and then try to do what you need perhaps yourself: Unzip a file

New Participant
January 26, 2022

Thanks for the reply,
This put me on the right track