【问题标题】:VBScript Unterminated String ConstantVBScript 未终止的字符串常量
【发布时间】:2014-04-08 10:02:42
【问题描述】:

我从一个网站上获取了这个脚本。我正在尝试在 Windows 8 上添加任务栏和开始菜单的快捷方式。我的 VBScript 知识很薄弱。我很确定我只是在某处遗漏了一个“,但我不确定在哪里。Else WScript.Echo 之后的第一个“字符串”在我的文本编辑器中没有显示为蓝色,因为它应该是这样的那里也有东西。任何帮助将不胜感激。(错误声称在 5、60)

'Pin an application to a start menu or task bar
If WScript.Arguments.Count = 3 then
Call PinApplicationToTaskBar(WScript.Arguments(0), WScript.Arguments(1), WScript.Arguments(2))
Else
WScript.Echo "Missing parameters. String AppPathAndName  _
String ShortcutName Boolean OnStartMenu." & vbCr & vbCr & "  _
Example cmd.exe CMD  false" & vbCr & vbCr & _
"  Example %windir%\system32\SnippingTool.exe SnipIt false" 
End If

Public Sub PinApplicationToTaskBar(AppPathAndName, ShortcutName, OnStartMenu)
'This is on for a soft failure. 
'Uncomment this if error checking for a hard failure is needed for debugging.
On Error Resume Next

Dim FileSystemObj, ObjShell, ObjShellApp
Set ObjShell = WScript.CreateObject("WScript.Shell")
Set FileSystemObj = CreateObject("Scripting.FileSystemObject")

'Create a temp location for the short-cut to exist
TempShortcutLocation = FileSystemObj.GetFolder_
(ObjShell.ExpandEnvironmentStrings("%TEMP%"))
'Where is it being pinned too?  Determine the location where the pinned item will reside.
If(trim(lcase(OnStartMenu)) = "true") then ' pinned to start menu
    HasItAlreadyBeenPinnedShortCut = ObjShell.ExpandEnvironmentStrings_
    ("%APPDATA%") & _
    "\Microsoft\Internet Explorer\Quick Launch\User Pinned\StartMenu"
Else
    HasItAlreadyBeenPinnedShortCut = ObjShell.ExpandEnvironmentStrings_
    ("%APPDATA%") & _
    "\Microsoft\Internet Explorer\Quick Launch\User Pinned\TaskBar"
End If
'Temporary location for the application short-cut
TempShortcut = TempShortcutLocation & "\" & ShortcutName & ".lnk"
'Possible location of a pinned item
HasItAlreadyBeenPinnedShortCut =  HasItAlreadyBeenPinnedShortCut & "\" & ShortcutName & ".lnk"
'If this already exists, than exit this procedure. The application has already been pinned.
If(FileSystemObj.FileExists(HasItAlreadyBeenPinnedShortCut)) Then
    'MsgBox(HasItAlreadyBeenPinnedShortCut & " Already Pinned")
    Set ObjShell = Nothing
    Set FileSystemObj = Nothing
    Exit Sub
End If
'Create a short-cut using the shell
Set lnk = ObjShell.CreateShortcut(TempShortcut)
lnk.TargetPath = AppPathAndName ' Full application path and name
lnk.Arguments = ""
lnk.Description = ShortcutName 'The name that appears on the start menu.
lnk.Save 

Set ObjShellApp = CreateObject("Shell.Application")

'Get the newly created short-cut full path
Set ShortCutToPin =  ObjShellApp.NameSpace(TempShortcutLocation) 

If(FileSystemObj.FileExists(TempShortcut)) Then 
    Dim ShortCutToPinItem, verb
    'Set the location to pin the item to do based on the passed OnStartMenu argument
    If(trim(lcase(OnStartMenu)) = "true") then
        verbToDo = "Pin to Start Men&u"
    Else    
        verbToDo = "Pin to Tas&kbar"
    End If
    For Each ShortCutToPinItem in ShortCutToPin.Items()
        'Look for the pinning verb when the temporary short-cut name matches the passed ShortcutName argument
        If (ShortCutToPinItem.Name = ShortcutName) Then
            'Loop through the shell object's (the short-cut) commands looking for the pinning method.
            For Each verb in ShortCutToPinItem.Verbs 
                'The verb matches the verbToDo so pin it to verb's defined location
                If (verb.Name = verbToDo) Then verb.DoIt
            Next
        End If
    Next
    'Delete the temporary short-cut used to pin the application
    FileSystemObj.DeleteFile(TempShortcut) 
End If
'clean up
Set ObjShell =  Nothing
Set FileSystemObj = Nothing
Set ObjShellApp = Nothing
End Sub

为了参考和信用,代码来自这里。 http://www.codeproject.com/Tips/713824/Pin-a-shortcut-onto-the-Taskbar-or-Start-Menu

【问题讨论】:

    标签: windows-8 vbscript


    【解决方案1】:

    这个字符串连接搞砸了。它应该是这样的:

    WScript.Echo "Missing parameters. String AppPathAndName " & _
                 "String ShortcutName Boolean OnStartMenu." & vbCr & vbCr & _
                 "Example cmd.exe CMD false" & vbCr & vbCr & _
                 "Example %windir%\system32\SnippingTool.exe SnipIt false" 
    

    您必须同时使用续行 (_) 和字符串连接 (&) 运算符在 VBScript 中跨多行连接字符串。确保所有字符串文字都关闭(每个引号在同一行上都有一个匹配的右引号)。

    【讨论】:

    • 谢谢!过了那部分。现在我在 (6, 65) 处遇到错误
    • 那里也缺少一个引用。我已经更新了上面的代码。再试一次。
    • 好吧,在我真正回到这里之前,我就知道了。这个脚本显然有很多问题。现在我得到“在 (22, 2) 处的预期语句,这是这一行。(ObjShell.ExpandEnvironmentStrings("%TEMP%"))
    • 编写此脚本的人不知道如何使用续行。告诉你什么......无论你在哪里看到下划线 (_),只需将其删除并将该行与它下面的行合并即可。 :)
    • 这就是我在查找未终止字符串常量时的假设。感谢您的帮助。可悲的是,现在该脚本甚至不适用于 Windows 8 开始菜单 :(.
    猜你喜欢
    • 1970-01-01
    • 2011-04-27
    • 1970-01-01
    • 2011-07-24
    • 2018-09-16
    • 2012-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多