Windows Shortcuts

Hi All,

Does anyone know how to programmatically create windows shortcuts (.lnk
files) using Ruby?
Have been googling for a couple of days without much luck.

Cheers,
Assaph

···

________________________________
Assaph Mehr
Email: assaph@avaya.com
Phone: +61-2-9352 9247
Auslabs (Avaya Labs Australia)
_______________________________

Mehr, Assaph (Assaph) wrote:

Hi All,

Does anyone know how to programmatically create windows shortcuts (.lnk
files) using Ruby?
Have been googling for a couple of days without much luck.

This is a method that depends on the Windows Scripting Host being installed, which it is by default on 2k and XP, I believe ... possibly others, but I don't know for sure.

  def create_shortcut(targetFileName, linkName)
    shell = WIN32OLE.new("WScript.Shell")
    scut = shell.CreateShortcut(linkName + '.lnk')
    scut.TargetPath = File.expand_path(targetFileName)
    scut.Save
    scut
  end

The method returns the scut instance because there are other things you can do with it that this default method glosses over. This method is apart of my anemic clUtil lib (BSD license) you can get here: http://www.clabs.org/dl/clutil/ (see also http://www.clabs.org/ruby.htm\).

···

--
Chris
http://clabs.org/blogki

=begin

Hi Assaph,

How about this?

"Mehr, Assaph (Assaph)" <assaph@avaya.com> wrote in message

Does anyone know how to programmatically create windows shortcuts (.lnk
files) using Ruby?

=end
require 'win32ole'

shell = WIN32OLE.new("WScript.Shell")

# Desktop shortcut
strDesktop = shell.SpecialFolders("Desktop")
oShellLink = shell.CreateShortcut(strDesktop + "\\Shortcut Script.lnk")
oShellLink.TargetPath = "c:\\atest\\tst_shortcut.rb" #WScript.ScriptFullName
oShellLink.WindowStyle = 1
oShellLink.Hotkey = "CTRL+SHIFT+F"
oShellLink.IconLocation = "notepad.exe, 0"
oShellLink.Description = "Shortcut Script"
oShellLink.WorkingDirectory = strDesktop
oShellLink.Save

# URL shortcut
oUrlLink = shell.CreateShortcut(strDesktop + "\\Rubyforge.url")
oUrlLink.TargetPath = "http://rubyforge.org/&quot;
oUrlLink.Save

__END__