General :  K-Meleon Web Browser Forum
General discussion about K-Meleon 
Macro -- wGet downloader
Posted by: MrWoo
Date: December 14, 2008 09:19AM

I have been finding time again to learn more of the KM macro language. I chose to try to make one for wGet, a simple download manager. There is an example in the resources, but it appears to be of older flavour. So here is what I have come up with... but I need some help.

# ---------- wGetk.kmm
# ---------- K-Meleon Macro Language to use wGet when downloading ---------------
#
# Dependencies : main.kmm
# Resources : -
# Preferences : -
# Version : 0.1 - 12.14.2008
#
# need in directory wGet: wget.exe, libeay32.dll, ssleay.dll
# download page: http://gnuwin32.sourceforge.net/packages/wget.htm
# download link: http://downloads.sourceforge.net/gnuwin32/wget-1.11.4-bin.zip
# --------------------------------------------------------------------------------

# find the root folder of K-Meleon
$PATH = getfolder(RootFolder);
# give a variable the path to the wGet.exe file enclose in quotes
$wGetk_Path="\"" . $PATH . "\\wGet\\wGet.exe\"";
# this is the scripted save to location for wGet, enclosed in quotes
$wGetk_SaveAs="\"c:\\Documents and Settings\\mrwoo\\My Documents\\My Downloads\"";
# this is the scripted save to location for the log file, enclosed in quotes
$wGetk_Log = "\"c:\\Documents and Settings\\mrwoo\\My Documents\\My Downloads\\wGet_log.txt\"";

# ---------- PUBLIC ----------------------------------------------

wGetk_Dload{
	# should there be checking for $LinkURL being valid or not ??
	
	# comment out one of the following depending if you want logging or not
	
	# this one keeps an appending log of the downloads
	exec($wGetk_Path . " -a " . $wGetk_Log . " -P " . $wGetk_SaveAs . " " . $LinkURL);
	# this one does not log at all
	#exec($wGetk_Path . " -P " . $wGetk_SaveAs . " " . $LinkURL);
}

wGetk_DloadAs{
	# is there a way to see what the extension of the link is ??
	# can it be checked, so that the promptforfile can be set correctly ??
	
	# give a variable a path and file name to save this to
	$_wG_saveme = promptforfile("c:\\","","*.*");
	
	# this one keeps an appending log of the downloads in declared directory
	exec($wGetk_Path . " -a " . $wGetk_Log . " -P " . $_wG_saveme . " " . $LinkURL);
	# this one does not log at all
	#exec($wGetk_Path . " -P " . $_wG_saveme . " " . $LinkURL);
	
	
	# is there any string manipulation to strip out either only directory or filename ??
}

# ----- PRIVATE ----------------------------------------------------

_wGetk_BuildMenu{
	# context menus for scripted save location..
	setmenu(LinkSave,macro,"wGet link",wGetk_Dload,ID_SAVE_LINK_AS);
	setmenu(ImageSave,macro,"wGet image",wGetk_Dload,ID_SAVE_IMAGE_AS);
	# context menus for manual choosing save location..
	setmenu(LinkSave,macro,"wGet link as..",wGetk_DloadAs,ID_SAVE_LINK_AS);
	setmenu(ImageSave,macro,"wGet image as..",wGetk_DloadAs,ID_SAVE_IMAGE_AS);
	# set accel key for save to scripted location
	setaccel("SHIFT ALT W","macros(wGetk_Dload)");
}

$OnInit=$OnInit."_wGetk_BuildMenu;";

# is this needed ?
$macroModules=$macroModules."wGetk;";

# End of kmm file --------------------------------------------------

In addition to the few commented questions in the macro, it would seem that I cannot get the promptforfile() to work correctly. Say I am going to download km_setup.zip. The prompt opens, and I navigate to a directory. Then I write in a filename, such as KM_151_Setup.zip (changed from the LinkURL), then when I press ok, there is an error, that it cannot be done. However, in this instance, the file is downloaded, but it is to KM rootfolder. What is going wrong? Is this a prompt for only opening? How do you prompt for saving? I thought it only returned a path. Should one supply a check for the return? Or is it handled internally?

And in the comments, you will note mostly questions regarding string manipulation. I think KM is a little short in this.

But still, this is my first attempt at incorporating an external app, and at using context menus.

I have tried many times in the last few months to go back to Opera, but I cannot. I have the thought of being able to learn more of the macro language to make it do more of what I want. So, will do just that maybe.

Thanks for any help.

MrWoo.

Options: ReplyQuote
Re: Macro -- wGet downloader
Posted by: desga2
Date: December 14, 2008 12:19PM

Quote
MrWoo
In addition to the few commented questions in the macro, it would seem that I cannot get the promptforfile() to work correctly. Say I am going to download km_setup.zip. The prompt opens, and I navigate to a directory. Then I write in a filename, such as KM_151_Setup.zip (changed from the LinkURL), then when I press ok, there is an error, that it cannot be done. However, in this instance, the file is downloaded, but it is to KM rootfolder. What is going wrong? Is this a prompt for only opening? How do you prompt for saving? I thought it only returned a path. Should one supply a check for the return? Or is it handled internally?

promptforfile() function work correctly, but in this function file selected must be exist. (This function is for open/read files not for save/create new files)
You can use promptforfolder() instead promptforfile() function.

In K-Meleon Macro Language Documentation:
Quote
K-Meleon Macro
$PATH = promptforfile( INITIAL_FOLDER, FILE_TYPE_DESCRIPTION, FILE_EXTENSION_PATTERN );

Prompts the user for a file, displaying only the files matching the pattern.

$PATH = promptforfolder( CAPTION [, INITIAL_FOLDER] );

Prompts the user for a folder.

This kko's documentation is better about this.

kko's documentation about Macro Language is more explained/extended/completed than K-Meleon wiki pages.

Are you tried to use ID_FILE_SAVE_AS ?

K-Meleon Macro Language have a lot of functions to use with strings:
    $SUB = gensub( r, s, h, t );

Searches the target string t for matches of the string r. If h is a string beginning with g or G, then all matches of r are replace with s. Otherwise, h is a number indicating which match of r to replace. The modified string is returned.

    $SUB = gsub( r, s, t );

Substitutes the string s for each substring matching the string r in the string t, and returns the modified string. This is equivalent to gensub( r, s, "G", t).

    $SUB = sub( r, s, t );

Just like gsub(), but only the first matching substring is replaced. This is equivalent to gensub( r, s, 1, t).

    $SUB = substr( s, i [, n] );

Returns the at most n-character substring of s starting at i. If n is omitted, the rest of s is used.


    $I = index( s, t );

Returns the index of the string t in the string s, or -1 if t is not present.


    $LEN = length( s );

Returns the length of the string s.


    $BASE = basename( URL [, SUFFIX] );

Returns URL with any leading directory components removed. If specified, also remove a trailing SUFFIX.

    $DIR = dirname( URL );

Returns URL with its trailing /component removed. If URL contains no '/', output '.' (meaning the current directory).

    $HOST = hostname( URL );

Returns the hostname of the given URL.

K-Meleon in Spanish



Edited 2 time(s). Last edit at 12/14/2008 12:28PM by desga2.

Options: ReplyQuote
Re: Macro -- wGet downloader
Posted by: MrWoo
Date: December 14, 2008 11:14PM

Thank you for responding.

I did not realize promptforfile was like that. I will play with promptforfolder.

Quote

Are you tried to use ID_FILE_SAVE_AS ?
I had looked at that, using id(). But I could not see how to use it. Can you give a line example of how it would be implemented. Is it used in a function like promptforfilder?

Quote

$SUB = gensub( r, s, h, t );
I will be honest, having used different string manipulation commands in different script languages, this is the most confusing one I have seen. Is it possible you could give a line example of this. Is this like a replacestring? Or more like a search or stringinstring?

Quote

$BASE = basename( URL [, SUFFIX] );

I also did not realize that this was here. How silly. I should have seen those.

Thank you.

MrWoo.

Options: ReplyQuote
Re: Macro -- wGet downloader
Posted by: JamesD
Date: December 15, 2008 03:34AM

@ MrWoo

Here is a small training macro which may help.

Training.kmm

#  K-Meleon Macros (http://kmeleon.sourceforge.net/wiki/index.php?id=MacroLanguage)
#
# ---------- Training.kmm
# ---------- K-Meleon Macro Language Training/Example ---------------
#
# Dependencies        : main.kmm
# Resources           : -
# Preferences         : -
# Version             : -
# --------------------------------------------------------------------------------
#
_Training_FolderCode{
$_Training_SubFolder=promptforfolder("Select a folder for use", $_Training_Path) ;
alert( $_Training_SubFolder, "Folder user selected", "INFO") ;
}

_Training_GensubCode{
# $SUB = gensub( r, s, h, t );
# Searches the target string t for matches of the string r.
# If h is a string beginning with g or G, then all matches 
# of r are replace with s. Otherwise, h is a number indicating 
# which match of r to replace. The modified string is returned.
$_Training_Target = "copper is a color for corporate" ;
alert($_Training_Target, "The target string") ;
$_Training_Match = "co" ;
$_Training_Replace = "To" ; 
$_Training_GOut = gensub($_Training_Match, $_Training_Replace, "G" ,$_Training_Target) ;
alert($_Training_GOut, "Global replacement", "INFO") ;
$_Training_GOut = gensub($_Training_Match, $_Training_Replace, "1" ,$_Training_Target) ;
alert($_Training_GOut, "First replacement only", "INFO") ;
}

_Training_BuildMenu{
# add another option to Help menu
$_Training_Popm = "Training" ; 
setmenu("KMDocs",separator,-1);
setmenu("KMDocs",popup,$_Training_Popm);
setmenu($_Training_Popm,macro,"PromptForFolder",_Training_FolderCode);
setmenu($_Training_Popm,macro,"Gensub",_Training_GensubCode);
}


_Training_GetPath{
$_Training_Path=getfolder(UserMacroFolder:MacroFolder);
}

$OnStartup=$OnStartup."_Training_GetPath;";
$OnInit=$OnInit."_Training_BuildMenu;";
$macroModules=$macroModules."Training;";


Options: ReplyQuote
Re: Macro -- wGet downloader
Posted by: JamesD
Date: December 15, 2008 03:43AM

ID_FILE_SAVE_AS is used in menus.cfg. I have copied a portion of that file here.


# ---------- Main Menu ---------------------------------------------------------------------------------------------

# ----- File

Network{
Work O&ffline=ID_OFFLINE
}
NavWindow{
Ne&xt Window=ID_WINDOW_NEXT
Pre&vious Window=ID_WINDOW_PREV
}
OpenSave{
&Open...=ID_FILE_OPEN
&Save Page As...=ID_FILE_SAVE_AS    <<===
}
&File{
!New
!NavWindow
!OpenSave
!Print
!Network
!Close
}


Options: ReplyQuote
Re: Macro -- wGet downloader
Posted by: MrWoo
Date: December 15, 2008 04:58AM

Very nice!

So here is how my version of the help file would be

gensub(<search string>,<replacment string>,<Flag>,<source string>)
<source string> = the string you wish to search in
<search string> = the string you wish to find in the <source string>
<replacement string> = the string you will replace the <search string> with
<Flag> = g or G = indicates that ALL instances of <search string> will be replaced with <replacement string>
<Flag> = Integer = indicates WHICH ONE instance of <search string> will be replaced with <replacement string>

I get it now. Thanks for the great example.

Now, please forgive me, but I am not understanding how to use things correctly. If I have a simple macro:

macro{
id(ID_FILE_SAVE_AS);
}

This opens the file save dialog.

If I try this:

macro{
$var = id(ID_FILE_SAVE_AS);
alert($var,"var is");
}

Then $var returns "", or maybe a non-error value. Your example shows how to use prompt for folder, but how do I get a custom save file name? I have the macro working now well for context menu download of a link. But saving as "filename" I would think would also be needed. I have not yet discovered how.

I assume the ID_FILE_SAVE_AS produces no return value? According to this
OpenSave{
&Open...=ID_FILE_OPEN
&Save Page As...=ID_FILE_SAVE_AS    <<===
}

This shows no return. Is is possible to expound on this menu.cfg and have a global variable? Or is the following part of the answer?

$ARG

K-Meleon's keyboard accelerators, its menu system and its plugins (but not the macros plugin itself) can call a macro with an argument:

setmenu($__m,macro,"&Toolbars","cfg_Skin(toolbars)");

A macro called this way, can get the argument reading the $ARG variable:

   cfg_Skin{
   macroinfo=_("Edit a skin configuration file");
   $ext="cfg"; &getExtensionHandler; $cfg_File=$ARG.".".$ext;
   .
   .
   }

Thanks for the time.

MrWoo.

Options: ReplyQuote
Re: Macro -- wGet downloader
Posted by: JamesD
Date: December 15, 2008 11:32AM

I have never used ID_FILE_SAVE_AS myself. I think, but I am not sure, that it will only save the current document. It simply lets the user pick the folder and name to use.

Options: ReplyQuote
Re: Macro -- wGet downloader
Posted by: JamesD
Date: December 15, 2008 12:28PM

Quote
MrWoo
# is this needed ?
$macroModules=$macroModules."wGetk;";

Yes, we should always identify to the system the macro name. This lets the name show in EDIT - PREFERENCES - MACRO ENTENSION. There the user can check whether or not to "read" and use the macro.

Options: ReplyQuote
Re: Macro -- wGet downloader
Posted by: JamesD
Date: December 15, 2008 02:48PM

I have expanded on the promptforfolder in Training.kmm.

Training.kmm

#  K-Meleon Macros (http://kmeleon.sourceforge.net/wiki/index.php?id=MacroLanguage)
#
# ---------- Training.kmm
# ---------- K-Meleon Macro Language Training/Example ---------------
#
# Dependencies        : main.kmm
# Resources           : -
# Preferences         : -
# Version             : -
# --------------------------------------------------------------------------------
#
_Training_FolderCode{
alert("Allow user to select folder. \n\n No initial folder \n No programed requirement", "Using promptforfolder") ;
$_Training_SubFolder=promptforfolder("Select a folder for use") ;
alert( $_Training_SubFolder, "Folder user selected", "INFO") ;
alert("Allow user to select folder. \n\n Set initial folder to user macro folder \n No programed requirement", "Using promptforfolder") ;
$_Training_SubFolder=promptforfolder("Select a folder for use", $_Training_Path) ;
alert( $_Training_SubFolder, "Folder user selected", "INFO") ;
macros(_Training_FolderCode2) ;
}

_Training_FolderCode2{
alert("Allow user to select folder. \n\n Set initial folder to Favorites \n Require Favorites or subfolder", "Using promptforfolder") ;
####   look three places for the favorites directory
$_Training_FavFolder=getpref(STRING,"kmeleon.plugins.favorites.directory");
$_Training_FavFolder==""?$_Training_FavFolder=readreg("HKCU","Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders\\Favorites"):0;
$_Training_FavFolder==""?$_Training_FavFolder=readreg("HKLM","Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders\\Favorites"):0;
####   $_Training_FavFolder should have the path for the favorites folder   c:\something\favorites
####   call _Training_RnameTrue_loop until $_Training_SubFolder is no longer a "?"
$_Training_SubFolder="?";   while($_Training_SubFolder=="?") &_Training_NameTrue_loop;
$_Training_SubFolder == "" ? alert($_Training_FavFolder, "The selected folder", "INFO") : alert($_Training_FavFolder."\\".$_Training_SubFolder, "The selected folder", "INFO") ;
}

_Training_NameTrue_loop{
$_Training_SubFolder=promptforfolder (_("Specify where to add the new shortcut. Select your Favorites folder or one of its subfolders:"),$_Training_FavFolder);
$_Training_SubFolder=$_Training_SubFolder=="" ? "" : index($_Training_SubFolder,$_Training_FavFolder) == 0 ? substr($_Training_SubFolder,length($_Training_FavFolder)+1) : "?";
}

_Training_GensubCode{
# $SUB = gensub( r, s, h, t );
# Searches the target string t for matches of the string r.
# If h is a string beginning with g or G, then all matches 
# of r are replace with s. Otherwise, h is a number indicating 
# which match of r to replace. The modified string is returned.
$_Training_Target = "copper is a color for corporate" ;
alert($_Training_Target, "The target string") ;
$_Training_Match = "co" ;
$_Training_Replace = "To" ; 
$_Training_GOut = gensub($_Training_Match, $_Training_Replace, "G" ,$_Training_Target) ;
alert($_Training_GOut, "Global replacement", "INFO") ;
$_Training_GOut = gensub($_Training_Match, $_Training_Replace, "1" ,$_Training_Target) ;
alert($_Training_GOut, "First replacement only", "INFO") ;
}

_Training_BuildMenu{
# add another option to Help menu
$_Training_Popm = "Training" ; 
setmenu("KMDocs",separator,-1);
setmenu("KMDocs",popup,$_Training_Popm);
setmenu($_Training_Popm,macro,"PromptForFolder",_Training_FolderCode);
setmenu($_Training_Popm,macro,"Gensub",_Training_GensubCode);
}

_Training_GetPath{
$_Training_Path=getfolder(UserMacroFolder:MacroFolder);
}

$OnStartup=$OnStartup."_Training_GetPath;";
$OnInit=$OnInit."_Training_BuildMenu;";
$macroModules=$macroModules."Training;";


Options: ReplyQuote
Re: Macro -- wGet downloader
Posted by: MrWoo
Date: December 15, 2008 05:46PM

Great job on the Training.kmm. This is very useful, to both view the macro at the same time see how it displays prompts. I think having examples of things like loops and even If/Then statements like this are quite informative. Myself, I understand what it all is, but the syntax structure is just a little different than what I have used before. So an example script where I don't have to follow the logic into areas that open other macros is easier. Using it in Scite helps too, because I can fold each area and follow the logic better.

Thanks!

MrWoo.



Edited 1 time(s). Last edit at 12/15/2008 05:48PM by MrWoo.

Options: ReplyQuote
Re: Macro -- wGet downloader
Posted by: desga2
Date: December 16, 2008 02:24PM

Ok id(ID_FILE_SAVE_AS) not solved your problem.

Try to use first promptforfolder() and after complete with a simple prompt for file name:
$VALUE = prompt( MESSAGE [, TITLE [, DEFAULT_STRING]] );

For example in you code:
wGetk_DloadAs{
	&_downloadFolder = getpref(STRING, "kmeleon.download.dir");
# Or you can use: &_downloadFolder = getpref(STRING, "kmeleon.download.saveDir");
	# give a variable a path and file name to save this to
	$_wG_saveme = promptforfolder("Select a folder for download", &_downloadFolder);
	$_fileName = prompt("Enter file name:", "wGet downloader");
	$_wG_saveme = $_wG_saveme . "\\" . $_fileName;
	# this one keeps an appending log of the downloads in declared directory
	exec($wGetk_Path . " -a " . $wGetk_Log . " -P " . $_wG_saveme . " " . $LinkURL);
	# this one does not log at all
	#exec($wGetk_Path . " -P " . $_wG_saveme . " " . $LinkURL);
}

K-Meleon in Spanish



Edited 1 time(s). Last edit at 12/16/2008 02:24PM by desga2.

Options: ReplyQuote
Re: Macro -- wGet downloader
Posted by: MrWoo
Date: January 07, 2009 04:53PM

Thanks desga2. That led me to almost finishing this macro. I am currently stuck on something though.

If I go to an ftp site, and right click on a file, the return $LinkURL within the macro is something like

File%201900%20.zip

When holding pointer over the file, the message at bottom shows the link as File1900.zip. When passing this to wGet, it obviously fails because the %LinkURL is not correct with those %20 in there. Why is this happening? And is there a way to fix it? I should say, if it always will be this way, I can handle that. If it is to vary depending, I would if there is a finite list.

EDIT: Is this a space character? The filename would be File 1900 update.zip or something. This is normal?

MrWoo.



Edited 2 time(s). Last edit at 01/07/2009 04:57PM by MrWoo.

Options: ReplyQuote
Re: Macro -- wGet downloader
Posted by: JamesD
Date: January 07, 2009 06:03PM

@ MrWoo

That %20 is a space character. Some other %x replacements which might come up in a URL are:

"%20"  space
"%3D"  =
"%2C"  .
"%2B"  +
"%26"  &

Try a bit of code like this. $_Snippet_link = gsub("%20"," ",$_Snippet_link);

Options: ReplyQuote
Re: Macro -- wGet downloader
Posted by: desga2
Date: January 07, 2009 06:49PM

First, there is an old macro to wGet Downloader in External Aplications wiki page, this could help you. It's good to someone update this macro for other macro better and more recent, thanks.

Second, about File%201900%20.zip, yes this is a URL encode to a space (%20=" ").
You can do it by two ways, or replaced %20 for a space " " to obtain "File 1900 .zip" or delete %20 codes to obtain "File1900.zip" without spaces. For do it you can use
$SUB = gsub( r, s, t );
look it in Macro Documentation.

For example:

$_new_link = gsub("%20", " ", $LinkURL);

You can found more info about this escape codes or URL encoding.

-EDITED: JamesD wins me, I'm getting old. tongue sticking out smiley

You can look an example about this in this macro mine.

K-Meleon in Spanish



Edited 2 time(s). Last edit at 01/07/2009 06:55PM by desga2.

Options: ReplyQuote
Re: Macro -- wGet downloader
Posted by: MrWoo
Date: January 07, 2009 08:37PM

Thanks yet again to you both.

MrWoo.

Options: ReplyQuote


K-Meleon forum is powered by Phorum.