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 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.

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 in the Download Section of Microsoft's Scripting Homepage.


Updates:

2007-08-27
Localization support added.
2007-08-23
Confirmation message removed from JScript file.
2007-09-01
Removed the new item perfs limitation.
2007-09-06
Added error handler for very long data
2007-09-16
Added workaround to handle very long data




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         : -
# Version             :  2.0   2007-09-16
# --------------------------------------------------------------------------------
#
_FavRenAdd_RnameTrue{
####   look three places for the favorites directory
$_FavRenAdd_FavFolder=getpref(STRING,"kmeleon.plugins.favorites.directory");
$_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 );
setclipboard($_FavRenAdd_s1."--".$_FavRenAdd_s2);
# $_fmax: The length of the command line must not exceed 258 characters!
$_fmax=258; $_fcmd="wscript.exe \"".$_FavRenAdd_Path."\" \"".$_FavRenAdd_Profile_Path."\" \"".$_FavRenAdd_s1."\" \"".$_FavRenAdd_s2."\"";
length($_fcmd)>$_fmax ? &_FavRenAdd_Error : exec("wscript.exe \"".$_FavRenAdd_Path."\" \"".$_FavRenAdd_Profile_Path."\" \"".$_FavRenAdd_s1."\"  \"".$_FavRenAdd_s2."\"") ;
}

_FavRenAdd_Error{
setclipboard($_FavRenAdd_Path. \n .$_FavRenAdd_Profile_Path .\n. $_FavRenAdd_s1 .\n. $_FavRenAdd_s2 .\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{
$_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;
}

_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.0  2007-09-16

   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()==3)
{
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      = WScript.Arguments(1);
var FavRenAdd_String_2      = WScript.Arguments(2);
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))
}
}                

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


Comments & Questions

K-Meleon

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