use Win32::Shortcut;Then, use this command to create a shortcut object:
$LINK = new Win32::Shortcut();This function will create a $LINK object on which you can apply the Methods and Properties explained later.
The object is not yet a shortcut file; it is just the definition of a shortcut. Basically, you can do 2 things:
For the rest, the object can be accessed as it were a normal associative array reference. It has the following keys (here referred as properties):
$LINK->{'File'}
$LINK->{'Path'} $LINK->Path()
$LINK->{'ShortPath'}
$LINK->{'WorkingDirectory'} $LINK->WorkingDirectory()
$LINK->{'Arguments'} $LINK->Arguments()
$LINK->{'Description'} $LINK->Description()
$LINK->{'ShowCmd'} $LINK->ShowCmd()
$LINK->{'Hotkey'} $LINK->Hotkey()
$LINK->{'IconLocation'} $LINK->IconLocation()
$LINK->{'IconNumber'} $LINK->IconNumber()
Thus, assuming you have a shortcut file named
test.lnk
in your current directory, this simple script will tell you where this shortcut points to:
use Win32::Shortcut;
$LINK=new Win32::Shortcut();
$LINK->Load("test.lnk");
print "Shortcut to: $LINK->{'Path'} $LINK->{'Arguments'} \n";
$LINK->Close();
But you can also modify its values:
use Win32::Shortcut;
$LINK=new Win32::Shortcut();
$LINK->Load("test.lnk");
$LINK->{'Path'}=~s/C:/D:/i; # move the target from C: to D:
$LINK->{'ShowCmd'}=SW_NORMAL; # runs in a normal window
and then you can save your changes to the shortcut file with this command:
$LINK->Save(); $LINK->Close();or you can save it with another name, creating a new shortcut file:
$LINK->Save("test2.lnk");
$LINK->Close();
Finally, you can create a completely new shortcut:
$LINK=new Win32::Shortcut();
$LINK->{'Path'}="C:\PERL5\BIN\PERL.EXE";
$LINK->{'Arguments'}="-v";
$LINK->{'WorkingDirectory'}="C:\PERL5\BIN";
$LINK->{'Description'}="Prints out the version of Perl";
$LINK->{'ShowCmd'}=SW_SHOWMAXIMIZED;
$LINK->Save("Perl Version Info.lnk");
$LINK->Close();
Note also that in the examples above the two lines:
$LINK=new Win32::Shortcut();
$LINK->Load("test.lnk");
can be collapsed to:
$LINK=new Win32::Shortcut("test.lnk");
$LINK->Close();
$LINK->Load("test.lnk") or print "test.lnk not found!";
print join("\n", $LINK->Path,
$LINK->ShortPath,
$LINK->Arguments,
$LINK->WorkingDirectory,
$LINK->Description,
$LINK->ShowCmd,
$LINK->Hotkey,
$LINK->IconLocation,
$LINK->IconNumber);
}
$LINK = new Win32::Shortcut();
$RegEdit = new Win32::Shortcut("Registry Editor.lnk");
die "File not found" if not $RegEdit;
# if the target doesn't exist...
if(! -f $LINK->Path) {
# save the actual target for comparison
$oldpath = $LINK->Path;
# try to resolve it (with dialog box)
$newpath = $LINK->Resolve(0);
die "Not resolved..." if $newpath == $oldpath;
}
$LINK->Save();
$LINK->Save("Copy of " . $LINK->{'File'});
$LINK->Set("C:\PERL5\BIN\PERL.EXE",
"-v",
"C:\PERL5\BIN",
"Prints out the version of Perl",
SW_SHOWMAXIMIZED,
hex('0x0337'),
"C:\WINDOWS\SYSTEM\COOL.DLL",
1);
# it is the same of...
$LINK->Path("C:\PERL5\BIN\PERL.EXE");
$LINK->Arguments("-v");
$LINK->WorkingDirectory("C:\PERL5\BIN");
$LINK->Description("Prints out the version of Perl");
$LINK->ShowCmd(SW_SHOWMAXIMIZED);
$LINK->Hotkey(hex('0x0337'));
$LINK->IconLocation("C:\WINDOWS\SYSTEM\COOL.DLL");
$LINK->IconNumber(1);
$OBJECT->{'property'}
Eg., assuming that you have created a shortcut object with:
$LINK=new Win32::Shortcut();you can for example see its description with:
print $LINK->{'Description'};
You can of course also set it:
$LINK->{'Description'}="This is a description";
From version 0.02, those properties have also a corresponding method (subroutine), so
you can write the 2 lines above using this syntax too:
print $LINK->Description;
$LINK->Description("This is a description");
The properties of a shortcut reflect the content of the Shortcut Properties Dialog Box,
which can be obtained by clicking the third mouse button on a shortcut file in the
Windows 95 (or NT 4.0) Explorer and choosing "Properties" (well, I hope you already knew :).
| Value | Meaning | Constant |
| 1 | Normal Window | SW_SHOWNORMAL |
| 3 | Maximized | SW_SHOWMAXIMIZED |
| 7 | Minimized | SW_SHOWMINNOACTIVE |
Any other value (theoretically should) result in a Normal Window display.