Welcome to the VanDyke Software Forums

Join the discussion today!


Go Back   VanDyke Software Forums > Scripting

Notices

Reply
 
Thread Tools Rating: Thread Rating: 2 votes, 5.00 average. Display Modes
  #1  
Old 01-27-2009, 11:47 AM
WesR WesR is offline
Registered User
 
Join Date: Mar 2005
Location: USA
Posts: 149
File Open Dialog

I have a script that presents a file open dialog that works fine, but I have been looking for a way to tell the user what file to open. I've seen references to a dialogTitle object, but I get an error when I try to use it.

Here is an example of what I am doing:

Code:
	Set objDialog = CreateObject("UserAccounts.CommonDialog")
'	objDialog.dialogTitle = "Open Log File"
	objDialog.Filter = "Text Files|*.txt|Log Files|*.log|All Files|*.*"
	objDialog.FilterIndex = 1
	Result = objDialog.ShowOpen
	If Result = 0 Then
		exit sub
	End If
If I uncomment the dialogTitle, I get:

Microsoft VBScript runtime error
Error: Object doesn't support this property or method: 'objDialog.dialogTitle'

Does anyone know of an object to set the dialog's Title?

Thanks,

Wes
Reply With Quote
  #2  
Old 01-27-2009, 05:38 PM
miked's Avatar
miked miked is offline
Registered User
 
Join Date: Feb 2004
Posts: 2,040
The DialogTitle method is not available with the UserAccounts.CommonDialog object.

My guess is that you've seen references to the DialogTitle method in relation to the MSComDlg.CommonDialog object which is generally no longer available (from what I remember, the MsComDlg.CommonDialog object was only available with something in MS Office 97, and isn't available by default in Windows XP OS versions; in fact, the UserAccounts.CommonDialog object is *only* available in Windows XP versions... it's been removed from Vista, for example.).

Perhaps it would work to just pop up a MsgBox letting the user know, "Please select the <specify what kind of file> file when the next dialog appears."?

I'm not an expert on this object, as it's not provided by VanDyke Software, but I don't think there's way to "title" the dialog box with UserAccounts.CommonDialog object.

I've created a feature request for the ability to open a file browser dialog from within SecureCRT's scripting API (that would also allow you to specify the title of the file browse dialog, too, hopefully). If a version becomes available with this functionality, we'll post here. If implemented and you want to be notified personally, please let us know and reference forum thread 3407.

In case you're interested in an alternative to using the UserAccounts.CommonDialog for opening files, here is code that uses the InternetExplorer.Application object. Since Internet Explorer is installed by default on most Windows systems, this code should be more portable than the UserAccounts.CommonDialog method.

Code:
Set ie = CreateObject("InternetExplorer.Application")
ie.Offline = True
IE.navigate "about:blank"

Do
  WScript.Sleep 100
Loop while ie.Busy

szHTMLBody = "<input type=file name=file>"
ie.document.Body.InnerHTML = szHTMLBody
ie.width = 0
ie.height = 0
'ie.Visible = True
' Bring up the file browse dialog
ie.Document.All("file").Click

if ie.Document.All("file").Value = "" then
    MsgBox "User Cancelled."
else
    MsgBox ie.Document.All("file").Value
end if
ie.quit
__________________
Mike
VanDyke Software
Technical Support
[http://www.vandyke.com/support]
Reply With Quote
  #3  
Old 01-27-2009, 05:47 PM
WesR WesR is offline
Registered User
 
Join Date: Mar 2005
Location: USA
Posts: 149
Thanks, I suspected something like that since about half the examples I found had that line commented out. I didn't have a clue why.

I currently am using the Msgbox popup prior to the open dialog, but I was looking for a single step method.

I'll play with the I.E. dialog you suggested. Probably the better way to go anyway.

Thanks,

Wes
Reply With Quote
  #4  
Old 01-28-2009, 08:03 AM
WesR WesR is offline
Registered User
 
Join Date: Mar 2005
Location: USA
Posts: 149
Hi,

My programming skills aren't quite what they should be... When I try to run the code suggested, I get an 'object required: Wscript' error.

I believe I need to create this object or something, but I don't have a clue what I'm really doing here. What am I missing - other than basic object oriented programming skills...

Wes
Reply With Quote
  #5  
Old 01-28-2009, 10:25 AM
miked's Avatar
miked miked is offline
Registered User
 
Join Date: Feb 2004
Posts: 2,040
Hi Wes,

Sorry about that - I posted a standalone code snippet instead of one that SecureCRT could use. The error happens because of the WScript.Sleep call.

Here is an explanation about why WScript.Sleep and some other WScript.methods are not available when CRT/SecureCRT is hosting the script (instead of WScript.exe or CScript.exe).

You'll need to find a different way to sleep while waiting for the InternetExplorer.Application object.

If you replace WScript.Sleep 100 with Crt.Sleep 100 does the code work for you?
__________________
Mike
VanDyke Software
Technical Support
[http://www.vandyke.com/support]
Reply With Quote
  #6  
Old 01-28-2009, 11:45 AM
WesR WesR is offline
Registered User
 
Join Date: Mar 2005
Location: USA
Posts: 149
That does work for me, but I think that will make it so if cannot be run as a standalone script, correct? For this particular script, I know some people have been running it as standalone.

Is there a check that I could put in in order to determine whether the script is running under SecureCRT or not and then call the appropriate sleep method?

Thanks,

Wes
Reply With Quote
  #7  
Old 01-28-2009, 12:51 PM
miked's Avatar
miked miked is offline
Registered User
 
Join Date: Feb 2004
Posts: 2,040
I'm not sure how to detect which script engine is running. However, using On Error Resume Next seems like one possible workaround:
Code:
  On Error Resume Next
  WScript.Sleep 100
  if Err.Number <> 0 then
      ' MsgBox "WScript.Sleep not found, let's use Crt.Sleep instead"
      Crt.Sleep 100
  end if
Will this be an acceptable workaround for you?
__________________
Mike
VanDyke Software
Technical Support
[http://www.vandyke.com/support]
Reply With Quote
  #8  
Old 01-28-2009, 01:37 PM
WesR WesR is offline
Registered User
 
Join Date: Mar 2005
Location: USA
Posts: 149
Yes, that will probably work (haven't tried it yet).

Does the 'on resume next' command need to be turned off or is is only applicable to the command which follows it?

Thanks,

Wes
Reply With Quote
  #9  
Old 01-28-2009, 02:24 PM
miked's Avatar
miked miked is offline
Registered User
 
Join Date: Feb 2004
Posts: 2,040
It remains active within that procedure until it's disabled with On Error GoTo 0. You'll probably want to use On Error GoTo 0 after the End If statement, unless you plan to do any other error checking.
Code:
On Error Resume Next
WScript.Sleep 100
if Err.Number <> 0 then
    ' MsgBox "WScript.Sleep not found, let's use Crt.Sleep instead"
    Crt.Sleep 100
end if
On Error GoTo 0
Here is a quote from Microsoft's VBScript Language Reference which is available for download as "script56.chm", or available at http://msdn.microsoft.com/en-us/libr...0h(VS.85).aspx.
Quote:
An On Error Resume Next statement becomes inactive when another procedure is called, so you should execute an On Error Resume Next statement in each called routine if you want inline error handling within that routine. When a procedure is exited, the error-handling capability reverts to whatever error-handling was in place before entering the exited procedure.

Use On Error GoTo 0 to disable error handling if you have previously enabled it using On Error Resume Next.
__________________
Mike
VanDyke Software
Technical Support
[http://www.vandyke.com/support]
Reply With Quote
  #10  
Old 01-28-2009, 02:56 PM
WesR WesR is offline
Registered User
 
Join Date: Mar 2005
Location: USA
Posts: 149
Excellent,

Thanks for you help Michael,

Wes
Reply With Quote
  #11  
Old 01-28-2009, 03:26 PM
miked's Avatar
miked miked is offline
Registered User
 
Join Date: Feb 2004
Posts: 2,040
You're welcome, Wes. If you have any other questions or find another solution please let us know.
__________________
Mike
VanDyke Software
Technical Support
[http://www.vandyke.com/support]
Reply With Quote
  #12  
Old 01-29-2009, 11:55 AM
WesR WesR is offline
Registered User
 
Join Date: Mar 2005
Location: USA
Posts: 149
FYI, I found this little bit of code which I think should work in standalone mode. I does seem to work when running with SecureCRT:

Code:
		Do Until IE.ReadyState = 4
		Loop
Wes
Reply With Quote
  #13  
Old 01-29-2009, 01:16 PM
miked's Avatar
miked miked is offline
Registered User
 
Join Date: Feb 2004
Posts: 2,040
Thanks Wes,

The ie.ReadyState seems to work for me, too. Removing the Sleep code from the IE.Busy loop seems to work the same:ie.Busy, too.

The reason Sleep is used in the loop is as a safeguard against burning too many CPU cycles if IE were busy for or unable to get into a ready state for some reason. Another option might be to use a counter and decide when you might want to break out. In practice I haven't seen the number of loops for this construct go above 10 even without Crt.Sleep or WScript.Sleep. However, if you were to set the destination to something other than about:blank or there were some type of problem, the number of cycles can go into the thousands pretty fast.
__________________
Mike
VanDyke Software
Technical Support
[http://www.vandyke.com/support]
Reply With Quote
  #14  
Old 02-03-2009, 07:34 AM
WesR WesR is offline
Registered User
 
Join Date: Mar 2005
Location: USA
Posts: 149
Hi again,

I used the ie object to replace the Common.Dialog file open, my next question if do you know of a way to specify the file types? i.e in the Common.Dialog, I could do this:

objDialog.Filter = "Text Files|*.txt|Log Files|*.log|All Files|*.*"

Is there anything you know of that I can use to set this functionality in the ie dialog? I know this is getting out of the realm of SecureCRT functionality, but any help is appreciated.

Thanks,

Wes
Reply With Quote
  #15  
Old 02-03-2009, 03:44 PM
miked's Avatar
miked miked is offline
Registered User
 
Join Date: Feb 2004
Posts: 2,040
Hi Wes,

I don't think there is a way to set a file filter using the Internet Explorer object. However, I did update the feature request mentioned in an earlier post (for a file-browse addition to the SecureCRT scripting API).
__________________
Mike
VanDyke Software
Technical Support
[http://www.vandyke.com/support]
Reply With Quote
Reply

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -6. The time now is 12:44 PM.