The book you need to succeed! Vbscript, jscript



Yüklə 12,95 Mb.
Pdf görüntüsü
səhifə83/91
tarix07.11.2018
ölçüsü12,95 Mb.
#78682
1   ...   79   80   81   82   83   84   85   86   ...   91

222
 Part 
II
 
Windows JScript_viewoptions.js'>JScript'>VBScript and JScript
Afterward, you need to access the file collection associated with the special folder. You do this 
through the 
FileSystemObject
, like this:
VBScript
Set fs = WScript.CreateObject(“Scripting.FileSystemObject”)
Set f = fs.GetFolder(smenu) 
Set fc = f.Files
JScript
fs = new ActiveXObject(“Scripting.FileSystemObject”);
f = fs.GetFolder(smenu);
fc = new Enumerator(f.Files);
Once you have the file collection, you can use 
For
 looping to examine the contents of the collection. 
This example places the full name and path for menu options on separate lines:
VBScript
For Each f1 in fc
  s = s & f1
  s = s & Chr(10) & Chr(13)
Next
CheckMenu = s
End Function
JScript
for (; !fc.atEnd(); fc.moveNext())

  f1 = fs.GetFile(fc.item());
  s += f1 + “\r\n”
}
return (s) 
}
If you want to display only the option name, you can use the 
name
 property of the 
File
 object, like this:
VBScript
For Each f1 in fc
  
  s = s & f1.name
  s = s & Chr(10) & Chr(13)
Next
CheckMenu = s
End Function
86804c11.indd   222
86804c11.indd   222
1/21/09   1:26:39 PM
1/21/09   1:26:39 PM


223
 Confi guring Menus, Shortcuts, and Startup Applications 
11
JScript
for (; !fc.atEnd(); fc.moveNext())

  f1 = fs.GetFile(fc.item());
  s += f1.name + “\r\n”
}
return (s)
}
Listing 11-8 shows how these procedures could come together in an actual script. This example dis-
plays all of the options on the current user’s Programs menu.
LISTING 11-8
Viewing Menu Options
VBScript
viewoptions.vbs
Function CheckMenu(mname)
 Dim fs, f, f1, fc, s, smenu, ws 
 Set ws = WScript.CreateObject(“WScript.Shell”) 
 smenu = ws.SpecialFolders(mname)
 Set fs = WScript.CreateObject(“Scripting.FileSystemObject”)
 Set f = fs.GetFolder(smenu) 
 Set fc = f.Files 
 For Each f1 in fc 
    s = s & f1.name 
    s = s & Chr(10) & Chr(13) 
 Next 
 CheckMenu = s 
End Function
WScript.Echo CheckMenu(“Programs”)
JScript
viewoptions.js
function CheckMenu(mname) 
{
   var fs, f, fc, s;
   var ws = WScript.CreateObject(“WScript.Shell”)
   smenu = ws.SpecialFolders(mname)
   fs = new ActiveXObject(“Scripting.FileSystemObject”);
   f = fs.GetFolder(smenu);
   fc = new Enumerator(f.Files);
continued
86804c11.indd   223
86804c11.indd   223
1/21/09   1:26:39 PM
1/21/09   1:26:39 PM


224
 Part 
II
 
Windows VBScript and JScript
   s = “”;
   for (; !fc.atEnd(); fc.moveNext())
   {
      f1 = fs.GetFile(fc.item());
      s += f1.name + “\r\n”
   }
   return (s)

WScript.Echo(CheckMenu(“Programs”))
Updating current shortcuts and menu options
Through Windows scripts, you can update the properties of any shortcut or menu option. You do 
this by creating a new shortcut with the exact same name as the old shortcut. For example, if you 
created a Start menu shortcut named Notes.lnk, you can update its settings by creating a new short-
cut named Notes.lnk.
In most cases, only the options you specifically set for the shortcut are overwritten. If necessary, you 
can clear an existing option by setting its value to an empty string. For example, Listing 11-5 creates 
a shortcut for Notepad. This shortcut sets an argument that opens a document called curr.vbs. If you 
delete curr.vbs and don’t want to use it anymore, you can update the shortcut as shown in 
Listing 11-9.
LISTING 11-9
Updating a Shortcut
VBScript
update.vbs
Set ws = WScript.CreateObject(“WScript.Shell”)  
pmenu = ws.SpecialFolders(“AllUsersPrograms”)
Set scut = ws.CreateShortcut(pmenu & “\Web Script.LNK”)  
scut.TargetPath = “%windir%\notepad.exe”  
scut.Arguments = “”
scut.IconLocation = “iexplore.exe, 0”
scut.Save
JScript
update.js
var ws = WScript.CreateObject(“WScript.Shell”)
pmenu = ws.SpecialFolders(“AllUsersPrograms”)
LISTING 11-8 
(continued)
86804c11.indd   224
86804c11.indd   224
1/21/09   1:26:39 PM
1/21/09   1:26:39 PM


225
 Confi guring Menus, Shortcuts, and Startup Applications 
11
var scut = ws.CreateShortcut(pmenu + “\\Web Script.LNK”)
scut.TargetPath =”%windir%\\notepad.exe “
scut.Arguments = “”
scut.IconLocation = “iexplore.exe, 0”
scut.Save()
Deleting shortcuts and menu options
Shortcuts and menu options are specified in files. You can delete them as you would any system file. 
If a shortcut called Notes.lnk is in the current working directory, you can delete it as follows:
VBScript
Dim fs
Set fs = CreateObject(“Scripting.FileSystemObject”)
fs.DeleteFile “Notes.LNK”
JScript
var fs
fs = new ActiveXObject(“Scripting.FileSystemObject”);
fs.DeleteFile(“Notes.LNK”)
If a shortcut is in a special folder, such as the Start menu folder, you need to obtain the related folder 
object before trying to delete the shortcut. Use the path to the folder to retrieve the shortcut using 
the 
GetFile
 method of 
FileSystemObject
. Afterward, call the 
Delete
 method of the 
File
 
object. This removes the shortcut. Listing 11-10 shows an example of deleting a shortcut from the 
Start menu.
LISTING 11-10
Deleting Start Menu Options
VBScript
deleteoption.vbs
Dim ws, fs, f, smenu
Set ws = WScript.CreateObject(“WScript.Shell”)  
Set smenu = ws.SpecialFolders(“StartMenu”)  
Set fs = CreateObject(“Scripting.FileSystemObject”)  
Set f = fs.GetFile(smenu & “\Notes.LNK”)
f.Delete
continued
86804c11.indd   225
86804c11.indd   225
1/21/09   1:26:39 PM
1/21/09   1:26:39 PM


Yüklə 12,95 Mb.

Dostları ilə paylaş:
1   ...   79   80   81   82   83   84   85   86   ...   91




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©genderi.org 2024
rəhbərliyinə müraciət

    Ana səhifə