Shoutbox

[HELP] Browsing to save a file - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Messenger Plus! for Live Messenger (/forumdisplay.php?fid=4)
+---- Forum: Scripting (/forumdisplay.php?fid=39)
+----- Thread: [HELP] Browsing to save a file (/showthread.php?tid=83834)

[HELP] Browsing to save a file by SmokingCookie on 05-20-2008 at 02:56 PM

Hi,

I want to save a file, using Windows' "Save File"-dialog and return the selected path, filename and extension, but I have no idea how to.. The MSDN descriptions don't make any sense to me..

Could anybody please help me?

Thanks in advance..


RE: [HELP] Browsing to save a file by Matti on 05-20-2008 at 03:48 PM

Have a look at matty's reply to Browse For File.

You'll probably need to modify some things, as I don't think you want the dialog to open at script start-up for example, but I think you'll be able to figure that out. Also, because that code was written for an Open File dialog, you'll have to make some modifications to get a Save File dialog, for example you'll have to call GetSaveFileName instead of GetOpenFileName. You'll also have to change the flags in the structure, as some flags only work with Open File dialogs and others are interesting to use for a Save File dialog. :)

Experiment with it. It's the only way to actually learn how to do it. ;)


RE: [HELP] Browsing to save a file by SmokingCookie on 05-20-2008 at 04:04 PM

Well, thank you :D

I have taken a quick look and don't really understand it (because it's a quick look).. I'll take a good hard look at it later this evening ;)

Again thank you for the quick reply and the information :D

EDIT::

I have inserted the code and changed everything with "Open" to "Save", but it won't work.. It returns a 0 as path...


RE: [HELP] Browsing to save a file by matty on 05-20-2008 at 07:41 PM

Do some research just editing code isn't going to help you learn.

Post your code so we can give you an idea where you went wrong.


RE: [HELP] Browsing to save a file by SmokingCookie on 05-20-2008 at 07:44 PM

I don't understand those flags etc., but here's the code anyway:

code:
function BrowseFile(filter,title,file) { // these ARE specified
    var SaveFileName = Interop.Allocate(88);
    var filter;
    var s_filter;
    var file;
    var s_file;
    var s_title;
    var ret;
    var tdata;
    with(SaveFileName) {
        WriteDWORD(0,Size); // lStructSize
        WriteDWORD(4,0); // hwndOwner
        WriteDWORD(8,0); // hInstance
        filter;
        s_filter = Interop.Allocate(2 * (filter.length + 1));
        WriteMultiStringW(s_filter, filter);
        WriteDWORD(12,s_filter.DataPtr); // lpstrFilter
        WriteDWORD(16,0); // lpstrCustomFilter
        WriteDWORD(20,0); // nMaxCustomFilter
        WriteDWORD(24,1); // nFilterIndex
        file + Space(256);
        s_file = Interop.Allocate(2 * (file.length + 1));
        s_file.WriteString(0,file);
        WriteDWORD(28,s_file.DataPtr); // lpstrFile
        WriteDWORD(32,file.length); // nMaxFile
        WriteDWORD(36,0); // lpstrFileTitle
        WriteDWORD(40,0); // nMaxFileTitle
        WriteDWORD(44,0); // lpstrInitialDir
        title;
        s_title = Interop.Allocate(2 * (title.length + 1));
        s_title.WriteString(0,title);
        WriteDWORD(48,s_title.DataPtr); // lpstrTitle
        WriteDWORD(52,OFN_ENABLESIZING | OFN_EXPLORER | OFN_HIDEREADONLY | OFN_LONGNAMES | OFN_PATHMUSTEXIST/* | OFN_FILEMUSTEXIST*/); // flags
        WriteWORD(56,0); // nFileOffset
        WriteWORD(58,0); // nFileExtension
        WriteDWORD(60,0); // lpstrDefExt
        WriteDWORD(64,0); // lCustData
        WriteDWORD(68,0); // lpfnHook
        WriteDWORD(72,0); // lpTemplateName
        WriteDWORD(76,0); // pvReserved
        WriteDWORD(80,0); // dwReserved
        WriteDWORD(84,0); // FlagsEx
    }
    with(SaveFileName) {
        Ret = Interop.Call("comdlg32.dll","GetSaveFileNameW",SaveFileName);
        if(ret == 0) {
            return "__none";
        } else {
            return Ret;
        }
    }
}

function WriteMultiStringW(datablock,string) {
    var pos = 0;
    datablock.WriteString(0,string);
    pos = string.indexOf("|",pos);
    while(pos != -1) {
        datablock.WriteWORD(2 * pos, 0);
        pos = string.indexOf("|",pos + 1);
    }
}

function Space(number) {
    var i;
    var s = "";
    for(i = 0; i < number; i++) {
        s += " ";
    }
    return s;
}


RE: [HELP] Browsing to save a file by roflmao456 on 05-20-2008 at 07:52 PM

maybe this could work (xp only?):

code:
function SaveFile(){
var SFD = new ActiveXObject("SAFRCFileDlg.FileSave");
SFD.FileName = "*.*";
SFD.FileType = "*.*";
if(SFD.OpenFileSaveDlg()){
return SFD.FileName;
}
return false;
}


run this JS script in wscript.exe. since it uses the jscript scripting engine, it works ;)
RE: [HELP] Browsing to save a file by Spunky on 05-20-2008 at 09:43 PM

quote:
Originally posted by roflmao456
maybe this could work (xp only?):
code:
function SaveFile(){
var SFD = new ActiveXObject("SAFRCFileDlg.FileSave");
SFD.FileName = "*.*";
SFD.FileType = "*.*";
if(SFD.OpenFileSaveDlg()){
return SFD.FileName;
}
return false;
}


run this JS script in wscript.exe. since it uses the jscript scripting engine, it works ;)

Doesn't work on vista in WScript here
RE: [HELP] Browsing to save a file by matty on 05-21-2008 at 12:29 AM

quote:
Originally posted by SmokingCookie
I don't understand those flags etc., but here's the code anyway:
Looking at the MSDN documentation for a structure is always a wise idea: http://msdn.microsoft.com/en-us/library/ms646839(VS.85).aspx

code:
function BrowseFile(filter, title, file) {
    var _SAVEFILENAME = Interop.Allocate(88);
    var s_filter;
    var s_file;
    var s_title;
    with(_SAVEFILENAME) {
        /*Define the size of the structure; this is done because there are multiple sizes of the structure */
        WriteDWORD(0, Size);
       
        /*Use this portion to force the Dialog to show ontop of another window */
        WriteDWORD(4, 0);
       
        /*Define the file filter */
        s_filter = Interop.Allocate(2 * filter.length + 1);
        WriteMultiStringW(s_filter, filter);
        WriteDWORD(12, s_filter.DataPtr);
       
        /* Set the current selected filter as the first filter in the list */
        WriteDWORD(24, 1);
       
        /* Allocate space to hold the filename */
        s_file = Interop.Allocate(512);
        s_file.WriteString(0, file);
        WriteDWORD(28, s_file.DataPtr);
       
        /* Set the maximum size of the file path */
        WriteDWORD(32, 512);
       
        /* Set the title of the dialog */
        s_title = Interop.Allocate(2 * title.length + 1);
        s_title.WriteString(0, title);
        WriteDWORD(48, s_title.DataPtr);
       
        /* Set the flags for the dialog style */
        WriteDWORD(52, OFN_ENABLESIZING | OFN_EXPLORER | OFN_HIDEREADONLY | OFN_LONGNAMES | OFN_PATHMUSTEXIST);
    }
    /* Call the function */
    var ret = Interop.Call('comdlg32', 'GetSaveFileNameW', _SAVEFILENAME);
    //return
            // if ret = 0
                       // since ret == 0 is true return '__none'
                                  // since ret != 0 then return the filename

    return ret === 0 ? '__none' : s_file.ReadString(0);
}

This is untested code. You need to learn to start doing things yourself.