0

I asked this question a few weeks ago and recently came back to it and solved it. I have a hidden input that looks like this:

                <input style="display:none;" class="fileDialog" type="file" nwworkingdir="C:\Users\" nwsaveas/>

This opens a save dialog at the dir given to nwworkingdir. I would like the default location to be the user's Pictures directory but I am having trouble getting the system user name dynamically with javascript. I need the username because the path is usually C:\Users\USERNAME\Pictures. I have tried constants such asssfMYPICTURESandCSIDL_PROFILE` but javascript doesn't recognize them or I am using them wrong.

The project is in node-webkit so a solution using javascript, node.js, or node-webkit APIs should work.

Community
  • 1
  • 1
Jake Sellers
  • 2,350
  • 2
  • 21
  • 40
  • 1. that opens an "open" dialog, not a "save" dialog, big difference. 2. JS has no control over which folder opens up, that's a browser preference and usually either the Downloads folder, or the most recently navigated folder. – dandavis Jul 23 '13 at 19:00
  • The `nwsaveas` tag makes it a save dialog. I solved the problem an hour ago, I shall close the topic. – Jake Sellers Jul 23 '13 at 19:01

1 Answers1

2

Solution for future readers:

Node.js function in "screen_shot.js":

exports.getUserHome = function() {
    return process.env[(process.platform == 'win32') ? 'USERPROFILE' : 'HOME'];
}

header js in index.html:

   $(document).ready(function() {
       var sc= require('screen_shot');
       $('.fileDialog').each(function(i) {
           $(this).attr('nwworkingdir', sc.getUserHome() + '\\Pictures');
       });
   });

                var sc= require('screen_shot');
            function wait(e) {
                var page = $(e).closest('[data-role="page"]');
                $('#close-popup').click();
                page.find('.fileDialog').click();
            }

            function screen_shot(fn) {
                html2canvas($('body'), {
                    onrendered: function(canvas) {
                    var img = canvas.toDataURL("image/png").split(',')[1];
                    sc.buildFile(fn, img);
                  }
                });
            }

            $('["..."]').live("pagecreate", function() {
                $(this).find('.fileDialog').change(function() {screen_shot($('.fileDialog').val() + '.png');});
            });

            $('[id^="..."]').live("pagecreate", function() {
                $(this).find('.fileDialog').change(function() {screen_shot($('.fileDialog').val() + '.png');});
            });

            $('[id^="..."]').live("pagecreate", function() {
                $(this).find('.fileDialog').change(function() {screen_shot($('.fileDialog').val() + '.png');});
            });

dialog and button:

                <input style="display:none;" class="fileDialog" type="file" nwworkingdir="" nwsaveas/>
                <a href="#" onclick="wait(this);" data-role="button" data-theme="j">Save As</a>
Jake Sellers
  • 2,350
  • 2
  • 21
  • 40