• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

create zip file via JSX

Community Beginner ,
Jan 26, 2022 Jan 26, 2022

Copy link to clipboard

Copied

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

TOPICS
Actions and scripting

Views

719

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Jan 26, 2022 Jan 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");
...

Votes

Translate

Translate
Adobe
LEGEND ,
Jan 26, 2022 Jan 26, 2022

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jan 26, 2022 Jan 26, 2022

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 26, 2022 Jan 26, 2022

Copy link to clipboard

Copied

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?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jan 26, 2022 Jan 26, 2022

Copy link to clipboard

Copied

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?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 26, 2022 Jan 26, 2022

Copy link to clipboard

Copied

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);
                }
            }
        }
    }

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jan 27, 2022 Jan 27, 2022

Copy link to clipboard

Copied

Thanks for the reply,

what I actually did was I created a .bat file from jsx and and wrote the code inside of it that zips all the assets delete the folder andthe batch file itself.

I think this can solve my issue for the time being, this is the code hopefully it can be helpful to anyone

function zipFiles(){
    var fullPath = activeDocument.path + "/application/compress.bat";
    var file = new File(fullPath);

    var batchcontent = 'powershell -Command "Compress-Archive -Path %cd%\\slices\\* -DestinationPath %cd%\\archive.zip" \n powershell Remove-Item "%cd%\\slices" \n Del compress.bat'

    file.open('w');
    file.write(
        batchcontent
    );
    file.close();
}

zipFiles()

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 27, 2022 Jan 27, 2022

Copy link to clipboard

Copied

So now you need the same for macOS 😛

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 27, 2022 Jan 27, 2022

Copy link to clipboard

Copied

zip and ditto work similarly but not exactly on macOS, ditto will work on a folder but not scattered individual files. Dude's code above zips a folder while mine zips a set of selected files regardless of location.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 02, 2022 Mar 02, 2022

Copy link to clipboard

Copied

will this also work on windows?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 02, 2022 Mar 02, 2022

Copy link to clipboard

Copied

Confirm that's a joke, not a SpamBot activity otherwise you'll get banned 😉

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 02, 2022 Mar 02, 2022

Copy link to clipboard

Copied

I am not a bot.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 02, 2022 Mar 02, 2022

Copy link to clipboard

Copied

LATEST

I asked to be sure as we have lately flood of bots over here 😉

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 02, 2022 Mar 02, 2022

Copy link to clipboard

Copied

I am not a bot. don't beleive me? throw me some 2+2 question.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines