GP SoftwareTwitter
Opus FAQsManualCommandsObjects

Opus VBScript can't use Win32 APIs?


#1

hi folks, Opus is insanely great, it is my main workflow management tool

Today, I tried to write a script that on pressing a global hotkey would activate a running app.
It works fine in excel as :

Private Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare PtrSafe Function BringWindowToTop Lib "user32" (ByVal lngHWnd As Long) As Long
Private Declare PtrSafe Function Beep Lib "kernel32" (ByVal dwFreq As Long, ByVal dwDuration As Long) As Long

option explicit

Sub ActivateApp()  ' <<=== for opus, comment this out


    Dim wHandle_Sought ' as long
    wHandle_Sought = FindWindow("WindowsForms10.Window.8.app.0.2ea1a2_r14_ad1", vbNullString)
    If wHandle_Sought = 0 Then
      Beep 1500, 150 ' indicate fail
      Sleep 100
      Beep 1000, 100
      ' qq invoke the app ?
      ' always, or based on an envtVar value ?
    Else
      BringWindowToTop wHandle_Sought
    End If
    
End Sub  ' <<=== for opus, comment this out

but when I run it in Opus, it says that the Win-32 API lines (i.e. 1 through 4) are invalid,
i.e. :

  Declare  Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As L...
                 ^
   Syntax error (0x800a03ea)
   Parse error - script aborted

How do we resolve this ?


#2

VBScript is not the same as Visual Basic.

VBScript has no direct way to access Win32 APIs (except ones exposed via scripting-compatible objects).

That's a limitation of Windows / VBScript, not something Opus is/isn't doing.