K-Meleon

KMeleonWiki > Resources > MacroLibrary > KmmFavRenAdd

Submitted by: JamesD (with help and support from kko and JujuLand)



Notes:

This macro will allow a user to change the name of a favorite during the add process and allow the user to select which folder/subfolder in Favorites to which the renamed favorite will be added.

IMPORTANT: The favorite is written as an internet shortcut of the style filename.url by Windows Scripting Host outside of K-melon. That means KM will have to be stopped and re-started before you can see/use the favorite. The exception to this is the Favorite's Edit menu item. Edit will show the new internet shortcut with out a restart. The user may cancel from the rename prompt and the process will be aborted. A cancel from the folder prompt will result in the default folder being used. That will be the Favorites folder.

SPECIAL NOTE: There are now two (2) versions of FavRenAdd. The original version works well for languages which encode correctly in ASCII. For languages that require encoding in UTF-8, there is now an international version. Each version consists of a FavRenAdd.kmm file and a FavRenAdd.js file. The files for the original version are listed first and the files for the international version are listed next. The two versions cannot be used together because they create the same menu items under Favorites.

Windows Script Host 2.0 (or better) required for regular, ASCII, version! Windows Script Host 1.0 (or better) required for international, version!

WSH is part of Microsoft Internet Explorer 5.0 and newer. Latest version of WSH is available in the Download Section of Microsoft's Scripting Homepage.


Updates:

2010-01-01
Fix by siria for using alternate favorites locations and minor bug fixes
2008-09-25
Added macroinfo data for menus
2008-08-15
Added international version
2008-08-12
Fixed translate function
2008-06-07
Added to Document popup menu
2007-10-15
Updated code for KM 1.5 macro language.
2007-09-17
Additional work to better handle very long data.
2007-09-16
Added workaround to handle very long data.
2007-09-06
Added error handler for very long data.
2007-09-01
Removed the new item perfs limitation.
2007-08-27
Localization support added.
2007-08-23
Confirmation message removed from JScript file.




Open your User Macro Folder (Edit > Configuration > User-Defined Macros) or your Macro Folder (Edit > Configuration > Macros) and create the following text file(s):

FavRenAdd.kmm

#  K-Meleon Macros (http://kmeleon.sourceforge.net/wiki/index.php?id=MacroLanguage)
#
# ---------- FavRenAdd.kmm
# ---------- K-Meleon Macro Language Rename/Add page to Favorites ---------------
#
# Dependencies        : main.kmm, Favorites plugin active, JScript, Windows Scripting Host
# Resources           : -
# Preferences         : "kmeleon.plugins.favorites.directory"
# Version             :  2.4.2   2010-01-01
# --------------------------------------------------------------------------------
#
_FavRenAdd_RnameTrue{
####   look three places for the favorites directory
$_FavRenAdd_FavFolder=getpref(STRING,"kmeleon.plugins.favorites.directory");
$_FavRenAdd_FavFolder=substr($_FavRenAdd_FavFolder,0,length($_FavRenAdd_FavFolder)-1);
$_FavRenAdd_FavFolder==""?$_FavRenAdd_FavFolder=readreg("HKCU","Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders\\Favorites"):0;
$_FavRenAdd_FavFolder==""?$_FavRenAdd_FavFolder=readreg("HKLM","Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders\\Favorites"):0;
####   $_FavRenAdd_FavFolder should have the path for the favorites folder   c:\something\favorites
####   call _FavRenAdd_RnameTrue_loop until $_FavRenAdd_SubFolder is no longer a "?"
$_FavRenAdd_SubFolder="?";   while($_FavRenAdd_SubFolder=="?") &_FavRenAdd_RnameTrue_loop;
$_FavRenAdd_Title = substr($_FavRenAdd_value,1);
macros(_FavRenAdd_Sanitize_Title) ;                                
$_FavRenAdd_PathName = $_FavRenAdd_FavFolder."\\".$_FavRenAdd_SubFolder."\\".$_FavRenAdd_Title.".url" ;
$_FavRenAdd_Ext_Interger = 1;
while ($_FavRenAdd_Ext_Interger > 0 ) &_FavRenAdd_Duplicates_loop ;
$_FavRenAdd_s1 = "k-meleon.FavRenAdd.s1" ;
$_FavRenAdd_s2 = "k-meleon.FavRenAdd.s2" ;
delpref($_FavRenAdd_s1);
delpref($_FavRenAdd_s1);
setpref(STRING,$_FavRenAdd_s1,$_FavRenAdd_PathName );
setpref(STRING,$_FavRenAdd_s2,$_FavRenAdd_CurURL );
$_FavRenAdd_Profile_Path = getfolder( ProfileFolder );
# $_fmax: The length of the command line must not exceed 258 characters!
$_fmax=258; $_fcmd="(wscript.exe \"" .$_FavRenAdd_Path. "\" \"".$_FavRenAdd_Profile_Path.")" ;
length($_fcmd)>$_fmax ? &_FavRenAdd_Error : exec("wscript.exe \"".$_FavRenAdd_Path."\" \"".$_FavRenAdd_Profile_Path."\"") ;
}

_FavRenAdd_Error{
setclipboard($_FavRenAdd_Path. "\n" .$_FavRenAdd_Profile_Path. "\n" .length($_fcmd) );
alert(_("Unable to create favorite - data too long!\n\nThe data was copied to the clipboard."),_("Failure to complete"),EXCLAIM);
}

_FavRenAdd_Sanitize_Title{
$_FavRenAdd_RevTitle = gsub( "?"," ",$_FavRenAdd_Title); 
$_FavRenAdd_RevTitle = gsub( ":"," ",$_FavRenAdd_RevTitle); 
$_FavRenAdd_RevTitle = gsub( "*"," ",$_FavRenAdd_RevTitle); 
$_FavRenAdd_RevTitle = gsub( "<"," ",$_FavRenAdd_RevTitle); 
$_FavRenAdd_RevTitle = gsub( ">"," ",$_FavRenAdd_RevTitle); 
$_FavRenAdd_RevTitle = gsub( "/"," ",$_FavRenAdd_RevTitle); 
$_FavRenAdd_RevTitle = gsub( "|"," ",$_FavRenAdd_RevTitle); 
$_FavRenAdd_RevTitle = gsub( "\\","",$_FavRenAdd_RevTitle);
$_FavRenAdd_Title = gsub( "\"","^",$_FavRenAdd_RevTitle);
}

_FavRenAdd_Duplicates_loop{
$_FavRenAdd_Exists=readfile($_FavRenAdd_PathName);
$_FavRenAdd_Exists=1.$_FavRenAdd_Exists ;
$_FavRenAdd_Len2=length($_FavRenAdd_Exists);
$_FavRenAdd_Len2 == 1 ? $_FavRenAdd_Ext_Interger=0 : &_FavRenAdd_Extend;
}

_FavRenAdd_Extend{
$_FavRenAdd_Wrk_Int = $_FavRenAdd_Ext_Interger - 1 ;
$_FavRenAdd_Ext_Interger == 1 ? $_FavRenAdd_PathName = gsub(".url" , "[".$_FavRenAdd_Ext_Interger."].url",$_FavRenAdd_PathName):0;
$_FavRenAdd_Ext_Interger > 1 ? $_FavRenAdd_PathName = gsub("[".$_FavRenAdd_Wrk_Int."].url" , "[".$_FavRenAdd_Ext_Interger."].url" , $_FavRenAdd_PathName):0;
$_FavRenAdd_Ext_Interger = $_FavRenAdd_Ext_Interger + 1 ;
}

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

_FavRenAdd_RenameAdd{
macroinfo = _("Edit favorite's title during the add process and allow for selection of folder.");
$_FavRenAdd_CurrTitle = $TITLE;
$_FavRenAdd_CurURL = $URL;
$_FavRenAdd_value=prompt($_FavRenAdd_CurrTitle,_("Rename the Favorite Title here"),$_FavRenAdd_CurrTitle);
###  cancel from prompt will return a length of zero to which we add one
$_FavRenAdd_value = 1.$_FavRenAdd_value;
$_FavRenAdd_Len=length($_FavRenAdd_value);
###  if the length is 1 cancelled was pressed on the prompt box
$_FavRenAdd_Len == 1 ? &_FavRenAdd_Cancelled :&_FavRenAdd_RnameTrue ;
}

_FavRenAdd_Cancelled{
$FavRenAdd_Button=confirm(_("Did you mean to cancel?"),_("Affirm cancel") ,YESNO,QUESTION);
$FavRenAdd_Button == "YES" ? 0 : &_FavRenAdd_RenameAdd ;
}

_FavRenAdd_ModMenu{
### add another option to Favorites menu if favorites plugin is active
$kFavorites=="true"?setmenu("F&avorites",macro,_("&Rename-Add"),_FavRenAdd_RenameAdd,1):0;

### show in Document Popup ;
$kFavorites=="true"?setmenu("DocumentSave",macro,_("&Rename-Add to Favorites"),_FavRenAdd_RenameAdd,1):0;
}

_FavRenAdd_GetPath{
### FavRenAdd.js is supposed to be located in the same folder as FavRenAdd.kmm (this file).
### If it isn't, Windows Script Host will notify the user.
$_FavRenAdd_Data=readfile(getfolder(MacroFolder)."\\FavRenAdd.kmm");
$_FavRenAdd_Path=getfolder($_FavRenAdd_Data==""?UserMacroFolder:MacroFolder)."\\FavRenAdd.js";
}

$OnStartup=$OnStartup."_FavRenAdd_GetPath;";
$OnInit=$OnInit."_FavRenAdd_ModMenu;";
$macroModules=$macroModules."FavRenAdd;";

FavRenAdd.js

/* FavRenAdd.js by JamesD, version  2.1  2007-09-17

   This script is a helper application for the K-Meleon Macro Module FavRenAdd.kmm that
   enables you to rename a favorite, select a folder, and write the favorite to that folder.

   Windows Script Host 2.0 (or better) required!

   WSH is part of Microsoft Internet Explorer 5.0 and newer. Latest version of WSH is
   available at http://msdn.microsoft.com/scripting/
*/

if(WScript.Arguments.count()==1)
{
var a , words , t , look_1 , look_2 , wordslenght;
var ForReading = 1,  FavRenAddFileName , FavRenAddDescription ;
var FavRenAdd_Profile_Path  = WScript.Arguments(0);
var FavRenAdd_String_1      = "k-meleon.FavRenAdd.s1";
var FavRenAdd_String_2      = "k-meleon.FavRenAdd.s2";
var FavRenAdd_Prefs_Filespec= FavRenAdd_Profile_Path + "\\Prefs.js" ;
var string1_length = FavRenAdd_String_1.length ;
var string2_length = FavRenAdd_String_2.length ;
var objfso = new ActiveXObject("Scripting.FileSystemObject");
if (objfso.FileExists(FavRenAdd_Prefs_Filespec))
        {
        a = objfso.OpenTextFile(FavRenAdd_Prefs_Filespec, ForReading);
        t = 0 ;
        do
                {                
                words = a.ReadLine();
                 look_1 = words.indexOf(FavRenAdd_String_1);
                 if (look_1 > 0)
                         {
                         wordslength = words.length ;
                         FavRenAddFileName = words.substr( string1_length + look_1 + 4 , wordslength - (string1_length + look_1 + 7 )) ;
                         t = t + 1 ;                
                         }
                         
                 look_2 = words.indexOf(FavRenAdd_String_2);
                 if (look_2 > 0)
                         {
                         wordslength = words.length ;
                         FavRenAddDescription = words.substr( string2_length + look_2 + 4 , wordslength - (string2_length + look_2 + 7 )) ;
                         t = t + 1 ;                
                         }
                }
                while (t < 2);
        
        a.Close();
        }
var WshShell = new ActiveXObject("WScript.Shell")
var oShellLink = WshShell.CreateShortcut(FavRenAddFileName);
oShellLink.TargetPath = FavRenAddDescription;
oShellLink.Save();
}
else
{
objArgs = WScript.Arguments ;
WScript.Echo(WScript.Arguments.Count());
for (i=0; i<objArgs.length; i++)
{
    WScript.Echo(objArgs(i))
}
}                

International Version:

FavRenAdd.kmm

#  K-Meleon Macros (http://kmeleon.sourceforge.net/wiki/index.php?id=MacroLanguage)
#
# ---------- FavRenAdd.kmm
# ---------- K-Meleon Macro Language Rename/Add page to Favorites ---------------
# ---------- INTERNATIONAL VERSION
#
# Dependencies        : - main.kmm, Favorites plugin active, JScript, Windows Scripting Host
# Resources           : - Registry keys "HKCU\\Software\\KMELEON\\FavRenAdd\\URLname"
#                                     and "HKCU\\Software\\KMELEON\\FavRenAdd\\URLdisc"
# Preferences         : - "kmeleon.plugins.favorites.directory"
# Version             : -  3.1.2   2010-01-01
# --------------------------------------------------------------------------------
#
_FavRenAdd_RnameTrue{
####   look three places for the favorites directory
$_FavRenAdd_FavFolder=getpref(STRING,"kmeleon.plugins.favorites.directory");
$_FavRenAdd_FavFolder=substr($_FavRenAdd_FavFolder,0,length($_FavRenAdd_FavFolder)-1);
$_FavRenAdd_FavFolder==""?$_FavRenAdd_FavFolder=readreg("HKCU","Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders\\Favorites"):0;
$_FavRenAdd_FavFolder==""?$_FavRenAdd_FavFolder=readreg("HKLM","Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders\\Favorites"):0;
####   $_FavRenAdd_FavFolder should have the path for the favorites folder   c:\something\favorites
####   call _FavRenAdd_RnameTrue_loop until $_FavRenAdd_SubFolder is no longer a "?"
$_FavRenAdd_SubFolder="?";   while($_FavRenAdd_SubFolder=="?") &_FavRenAdd_RnameTrue_loop;
$_FavRenAdd_Title = substr($_FavRenAdd_value,1);
macros(_FavRenAdd_Sanitize_Title) ;                                
$_FavRenAdd_PathName = $_FavRenAdd_FavFolder."\\".$_FavRenAdd_SubFolder."\\".$_FavRenAdd_Title.".url" ;
$_FavRenAdd_Ext_Interger = 1;
while ($_FavRenAdd_Ext_Interger > 0 ) &_FavRenAdd_Duplicates_loop ;
## enclosure of a parameter in double quotes is only necessary when the parameter can contain whitespace
## therefor the 1, 2, and 3 in the next "exec" lines do not have double quotes
## first check the line lengths
$_fmax=258 ; $_FavRenAdd_ExVal = 0 ;
$_fcmd = "(wscript.exe \"".$_FavRenAdd_Path."\" 1 \"".$_FavRenAdd_PathName.")" ;
length($_fcmd)>$_fmax ? &_FavRenAdd_Error1:0;
$_fcmd = "(wscript.exe \"".$_FavRenAdd_Path."\" 2 \"".$_FavRenAdd_CurURL.")" ;
length($_fcmd)>$_fmax ? &_FavRenAdd_Error2:0;
$_FavRenAdd_ExVal < 2 ? &_FavRenAdd_RunScript:0;
}

_FavRenAdd_RunScript{
exec("wscript.exe \"".$_FavRenAdd_Path."\" 1 \"".$_FavRenAdd_PathName."\"");
exec("wscript.exe \"".$_FavRenAdd_Path."\" 2 \"".$_FavRenAdd_CurURL."\"");
exec("wscript.exe \"".$_FavRenAdd_Path."\" 3 \"execute\"") ;
}

_FavRenAdd_Error1{
alert(_("Unable to create favorite - data too long!  Line length, "). length($_fcmd). _(" and line are displayed\n\n"). $_fcmd, _("String length too long!"), EXCLAIM) ;
$_FavRenAdd_ExVal = $_FavRenAdd_ExVal + 2;
}

_FavRenAdd_Error2{
alert(_("Unable to create favorite - data too long!  Line length, "). length($_fcmd). _(" and line are displayed\n\n"). $_fcmd, _("String length too long!"), EXCLAIM) ;
$_FavRenAdd_ExVal = $_FavRenAdd_ExVal + 3;
}

_FavRenAdd_Sanitize_Title{
$_FavRenAdd_RevTitle = gsub( "?"," ",$_FavRenAdd_Title); 
$_FavRenAdd_RevTitle = gsub( ":"," ",$_FavRenAdd_RevTitle); 
$_FavRenAdd_RevTitle = gsub( "*"," ",$_FavRenAdd_RevTitle); 
$_FavRenAdd_RevTitle = gsub( "<"," ",$_FavRenAdd_RevTitle); 
$_FavRenAdd_RevTitle = gsub( ">"," ",$_FavRenAdd_RevTitle); 
$_FavRenAdd_RevTitle = gsub( "/"," ",$_FavRenAdd_RevTitle); 
$_FavRenAdd_RevTitle = gsub( "|"," ",$_FavRenAdd_RevTitle); 
$_FavRenAdd_RevTitle = gsub( "\\","",$_FavRenAdd_RevTitle);
$_FavRenAdd_Title = gsub( "\"","^",$_FavRenAdd_RevTitle);
}

_FavRenAdd_Duplicates_loop{
$_FavRenAdd_Exists=readfile($_FavRenAdd_PathName);
$_FavRenAdd_Exists=1.$_FavRenAdd_Exists ;
$_FavRenAdd_Len2=length($_FavRenAdd_Exists);
$_FavRenAdd_Len2 == 1 ? $_FavRenAdd_Ext_Interger=0 : &_FavRenAdd_Extend;
}

_FavRenAdd_Extend{
$_FavRenAdd_Wrk_Int = $_FavRenAdd_Ext_Interger - 1 ;
$_FavRenAdd_Ext_Interger == 1 ? $_FavRenAdd_PathName = gsub(".url" , "[".$_FavRenAdd_Ext_Interger."].url",$_FavRenAdd_PathName):0;
$_FavRenAdd_Ext_Interger > 1 ? $_FavRenAdd_PathName = gsub("[".$_FavRenAdd_Wrk_Int."].url" , "[".$_FavRenAdd_Ext_Interger."].url" , $_FavRenAdd_PathName):0;
$_FavRenAdd_Ext_Interger = $_FavRenAdd_Ext_Interger + 1 ;
}

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

_FavRenAdd_RenameAdd{
macroinfo = _("Edit favorite's title during the add process and allow for selection of folder.");
$_FavRenAdd_CurrTitle = $TITLE;
$_FavRenAdd_CurURL = $URL;
$_FavRenAdd_value=prompt($_FavRenAdd_CurrTitle,_("Rename the Favorite Title here"),$_FavRenAdd_CurrTitle);
###  cancel from prompt will return a length of zero to which we add one
$_FavRenAdd_value = 1.$_FavRenAdd_value;
$_FavRenAdd_Len=length($_FavRenAdd_value);
###  if the length is 1 cancelled was pressed on the prompt box
$_FavRenAdd_Len == 1 ? &_FavRenAdd_Cancelled :&_FavRenAdd_RnameTrue ;
}

_FavRenAdd_Cancelled{
$FavRenAdd_Button=confirm(_("Did you mean to cancel?"),_("Affirm cancel") ,YESNO,QUESTION);
$FavRenAdd_Button == "YES" ? 0 : &_FavRenAdd_RenameAdd ;
}

_FavRenAdd_ModMenu{
### add another option to Favorites menu if favorites plugin is active
$kFavorites=="true"?setmenu("F&avorites",macro,_("&Rename-Add"),_FavRenAdd_RenameAdd,1):0;

### show in Document Popup ;
$kFavorites=="true"?setmenu("DocumentSave",macro,_("&Rename-Add to Favorites"),_FavRenAdd_RenameAdd,1):0;
}

_FavRenAdd_GetPath{
### FavRenAdd.js is supposed to be located in the same folder as FavRenAdd.kmm (this file).
### If it isn't, Windows Script Host will notify the user.
$_FavRenAdd_Data=readfile(getfolder(MacroFolder)."\\FavRenAdd.kmm");
$_FavRenAdd_Path=getfolder($_FavRenAdd_Data==""?UserMacroFolder:MacroFolder)."\\FavRenAdd.js";
}

$OnStartup=$OnStartup."_FavRenAdd_GetPath;";
$OnInit=$OnInit."_FavRenAdd_ModMenu;";
$macroModules=$macroModules."FavRenAdd;";

FavRenAdd.js

/* FavRenAdd.js by JamesD, version  3.01  2008-07-30
   INTERNATIONAL VERSION

   This script is a helper application for the K-Meleon Macro Module FavRenAdd.kmm that
   enables you to rename a favorite, select a folder, and write the favorite to that folder.

   Windows Script Host 1.0 (or better) required!

   WSH is part of Microsoft Internet Explorer 5.0 and newer. Latest version of WSH is
   available at http://msdn.microsoft.com/scripting/
*/

if(WScript.Arguments.count()==2)
{
        var WshShell = new ActiveXObject("WScript.Shell")
        if (WScript.Arguments(0) == 1)
                {
                        WshShell.RegWrite ("HKCU\\Software\\KMELEON\\FavRenAdd\\URLname", WScript.Arguments(1), "REG_SZ");
                }
        if (WScript.Arguments(0) == 2)
                {
                        WshShell.RegWrite ("HKCU\\Software\\KMELEON\\FavRenAdd\\URLdisc", WScript.Arguments(1), "REG_SZ");
                }                
        if (WScript.Arguments(0) == 3)
                {
                        WScript.Sleep(1000);
                        var $_FavRenAdd_PathName = WshShell.RegRead ("HKCU\\Software\\KMELEON\\FavRenAdd\\URLname") ;
                        var $_FavRenAdd_CurURL   = WshShell.RegRead ("HKCU\\Software\\KMELEON\\FavRenAdd\\URLdisc") ;
                        var oShellLink = WshShell.CreateShortcut($_FavRenAdd_PathName);
                        oShellLink.TargetPath = $_FavRenAdd_CurURL;
                        oShellLink.Save();
                        WshShell.RegDelete ("HKCU\\Software\\KMELEON\\FavRenAdd\\URLname") ;
                        WshShell.RegDelete ("HKCU\\Software\\KMELEON\\FavRenAdd\\URLdisc") ;
                }                
}
else
{
objArgs = WScript.Arguments ;
WScript.Echo(WScript.Arguments.Count());
for (i=0; i<objArgs.length; i++)
{
    WScript.Echo(objArgs(i))
}
}

Localizations

\locales\de-DE\FavRenAdd.kml (German/Deutsch)

Specify where to add the new shortcut. Select your Favorites folder or one of its subfolders:=Geben Sie an, wo die neue Verknüpfung erstellt werden soll. Wählen Sie Ihren Favoritenordner oder einen seiner Unterordner aus:
Rename the Favorite Title here=Ändern Sie hier den Titel des Favoriten
Did you mean to cancel?=Wollten Sie abbrechen?
Affirm cancel=Abbruch bestätigen
&Rename-Add=&Umbenennen-Hinzufügen
Edit favorite's title during the add process and allow for selection of folder.=Neuer Favorit vor dem Hinzufügen umbenennen und Ordner auswählen 


Comments & Questions

K-Meleon

(c) 2000-2010 kmeleonbrowser.org. All rights reserved.
design by splif.