K-Meleon

KMeleonWiki > Resources > MacroLibrary > KmmShortcut

Submitted by: kko



Notes:

This macro introduces Internet Explorer's "Create Shortcut" feature into K-Meleon. It empowers you, to send the URL of the current page or frame or of a link quickly to your desktop as an Internet Shortcut to revisit it later. This is a valuable functionality when you find an interesting page that you don't want to read instantly, but in the near future. Or when you want to save an email address from a link to add it to your address book later. The shortcut on your desktop will remind you, whereas a bookmark is quickly forgotten...

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.

As an alternative to this very simple Windows Scripting solution (Shortcut.js), Alain Aupeix offers a compiled executable (WebShortcut.exe) with extended functionality. Have a look at his Page du K-Meleon.


Updates:

2008-09-09
The macro now works in K-Meleon 1.1 and 1.5 without changes. Locales updated.
2007-09-10
Bugfix.
2007-09-06
Shortcut.js updated to version 1.3. Macro takes care of the command line's length (which is limited).
2007-06-19
Localization support added.
2006-11-27
Updated to the K-Meleon 1.1 macro system.
2005-10-26
Minor macro update. Link to Alain's website added.
2005-10-19
Shortcut.js updated to version 1.1 to take care of wyciwyg:// URLs. Macros improved to properly work on pages that have no title.
2005-09-29
Macros improved. LinkToDesktop now uses the link's text as shortcut name. FrameToDesktop now uses the frame's title as shortcut name.




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

Shortcut.kmm

# K-Meleon Macros (http://kmeleon.sourceforge.net/wiki/index.php?id=MacroLanguage)

# ---------- Send URLs to Windows desktop as Internet Shortcut (*.url) ---------------------------------------------
#
# Dependencies        : main.kmm (JS)
# Resources        : Shortcut.js
# Preferences        : -
#
# ------------------------------------------------------------------------------------------------------------------

# For K-Meleon 1.5 and newer
Shortcut_PageToDesktop{
macroinfo=_("Create a shortcut to the current page on the Desktop");
&Shortcut_PageToDesktop11;
}
Shortcut_LinkToDesktop{
macroinfo=_("Create a shortcut to the current link on the Desktop");
&Shortcut_LinkToDesktop11;
}
Shortcut_FrameToDesktop{
macroinfo=_("Create a shortcut to the current frame on the Desktop");
&Shortcut_FrameToDesktop11;
}

# For K-Meleon 1.1
Shortcut_PageToDesktop11{
$_Shortcut_URL=$URL;
$_Shortcut_Title=$TITLE==$URL?"":gsub("\"","'",$TITLE);
&_Shortcut;
}
Shortcut_LinkToDesktop11{
$_Shortcut_URL=$LinkURL;  $__temp=$TITLE==$URL?"":$TITLE; $JS_doitLink="top.document.title=a[j].textContent;j=a.length"; &JS_hndlLink;
$_Shortcut_Title=$TITLE==$URL?"":gsub("\"","'",$TITLE); injectJS("top.document.title='".$__temp."'");
&_Shortcut;
}
Shortcut_FrameToDesktop11{
$_Shortcut_URL=$FrameURL; $__temp=$TITLE==$URL?"":$TITLE; $JS_doit="top.document.title=title"; &JS_hndlDoc;
$_Shortcut_Title=$TITLE==$URL?"":gsub("\"","'",$TITLE); injectJS("top.document.title='".$__temp."'");
&_Shortcut;
}

# ----- PRIVATE

$_Shortcut_Path="";
$_Shortcut_Menu="C&reate Shortcut";

_Shortcut{
index($_Shortcut_URL,"wyciwyg:")==0?$_Shortcut_URL=substr($_Shortcut_URL,index($_Shortcut_URL,hostname($_Shortcut_URL))+length(hostname($_Shortcut_URL))+1):0;
# $_max: The length of the command line must not exceed 258 characters!
$_max=258; $_cmd="wscript.exe \"".$_Shortcut_Path."\" \"".$_Shortcut_URL."\""; $_len=length($_cmd);
0<($_len-$_max)?&_Shortcut_Error:(-4<($_len-$_max)?exec($_cmd):&_Shortcut_Create);
}
_Shortcut_Create{
$_Shortcut_Title==""?($_Shortcut_Title=index($_Shortcut_Title,"mailto:")==0?_("New Contact"):_("New Internet Shortcut")):0;
$_cmd=$_cmd." \"".substr($_Shortcut_Title,0,$_max-3-$_len)."\""; exec($_cmd);
}
_Shortcut_Error{
setclipboard($_Shortcut_URL); alert(_("Unable to create shortcut - URL too long!\n\nThe URL was copied to the clipboard."),gsub("&","",_($_Shortcut_Menu)),EXCLAIM);
}

_Shortcut_BuildMenu{
$__s=index($KM_Version,"1.1")==0?"11":"";
setmenu(DocumentSave,macro,$_Shortcut_Menu,"Shortcut_PageToDesktop".$__s,0);
setmenu(LinkSave,macro,$_Shortcut_Menu,"Shortcut_LinkToDesktop".$__s,0);
setmenu(FrameSave,macro,$_Shortcut_Menu,"Shortcut_FrameToDesktop".$__s,0);
}
$OnInit=$OnInit."_Shortcut_BuildMenu;";

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

# ------------------------------------------------------------------------------------------------------------------
$macroModules=$macroModules."Shortcut;";

Shortcut.js

/* Shortcut.js by kko, version 1.3.1

   This script is a helper application for the K-Meleon Macro Module Shortcut.kmm that
   enables you to send the URL of a page, frame or link to your desktop as an Internet
   Shortcut (*.url). For local resources, normal Shortcuts (*.lnk) are generated.

   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/
*/
function CheckLoc(s) {
        if (s.indexOf("file:") == 0) {
                var t = s.replace("file:","").split("/"), u = new Array();
                for (var j in t) if(t[j]) u[u.length] = t[j]; t = unescape(u.join("\\"));
                if (fso.FileExists(t) || fso.FolderExists(t)) {
                        def = t.substring(t.lastIndexOf("\\",t.length));
                        ext = ".lnk"; s = t;
                }
        }
        return s;
}
function FileName(n) {
        return a + "\\" + Sanitize(b) + ((n) ? (" ("+(n+1)+")") : "") + ext;
}
function Sanitize(s) {
        var invalid = new Array(/\*/g,/"/g,/</g,/>/g,/\|/g,/\\/g,/\//g,/:/g,/\?/g);
        for (var j in invalid) s = s.replace(invalid[j]," ");
        var t = s.split(" "); s = new Array();
        for (var j in t) if (t[j]) s[s.length] = t[j];
        return s.join(" ");
}
if (WScript.Arguments.length > 0) {
        var shl = WScript.CreateObject("WScript.Shell");
        var fso = WScript.CreateObject("Scripting.FileSystemObject");
        var loc = WScript.Arguments(0), ext = ".url";
        var def = loc.lastIndexOf("/")==loc.length-1?loc.substr(0,loc.length-1):loc;
            def = def.substring(def.lastIndexOf("/")+1,def.length);
            loc = CheckLoc(loc);
        var   a = shl.SpecialFolders("Desktop");
        var   b = ((WScript.Arguments.length > 1) && (WScript.Arguments(1))) ? WScript.Arguments(1) : def;
        var   c = 0; for (; fso.FileExists(FileName(c)); c++);
        var lnk = shl.CreateShortcut(FileName(c)); lnk.TargetPath = loc; lnk.Save();
} else WScript.Echo("Syntax: "+WScript.ScriptName+" TargetURL [FileName]");

Localizations

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

Create a shortcut to the current page on the Desktop=Eine Verknüpfung zur aktuellen Seite auf dem Desktop erstellen
Create a shortcut to the current link on the Desktop=Eine Verknüpfung zum aktuellen Verweis auf dem Desktop erstellen
Create a shortcut to the current frame on the Desktop=Eine Verknüpfung zum aktuellen Frame auf dem Desktop erstellen
New Internet Shortcut=Neu Internetverknüpfung
New Contact=Neu Kontakt
C&reate Shortcut=Ve&rknüpfung erstellen
Unable to create shortcut - URL too long!\n\nThe URL was copied to the clipboard.=Verknüpfung kann nicht erstellt werden - URL zu lang!\n\nDie Adresse wurde in die Zwischenablage kopiert.


Comments & Questions

K-Meleon

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