How to save clipboard contents to a variable in VBScript?

Hi, guys! How can I save clipboard contents (like files, folders, images, etc.) to a variable in VBScript and restore it, similar to how it's done in JScript?

That could mean a few things. The actual data in the files? The filenames/paths? Or like copy & paste in the file display?

Where is the JScript code you want to convert?

Like copy & paste in the file display.

var old=DOpus.GetClip()
...
DOpus.SetClip(old)

Simple scenario, backup and restore actual clipboard contents after executing some scripting.

It's almost the same, except you have to check if the returned data is going to be a string (text) or an object (files), because VBScript is an incredibly badly designed language. :slight_smile:

Dim old
If DOpus.GetClipFormat() = "files" Then
	Set old = DOpus.GetClip()
Else
	old = DOpus.GetClip()
End If

DOpus.SetClip old

If you're doing this to pass data to a command by changing the clipboard, and then aiming to restore it afterwards, there is probably a better approach without needing to use the clipboard at all. This method will still lose your clipboard data if it e.g. contains an image or some other type that isn't files or text.

1 Like

Yes, Leo, elaborate me on the new approach! Thanks!

I think you'll need to describe in more details what you're trying to do to find the best approach.

2 Likes

What I want to do is described by Leo:

If you're doing this to pass data to a command by changing the clipboard, and then aiming to restore it afterwards, there is probably a better approach

Thanks!

What command ? What data ? Where that data is coming from ? Why using the clipboard is required ? ... What's the functional goal you're trying to achieve ?

Okay, I think I know what you guys are alluding to. Now, I use the clipboard to get the selected items' paths. Maybe I could use a variable to store them.
Thanks!

Yes, if you can, avoid using the clipboard.
To use the selected files, you have a bunch of control codes at hand :
https://docs.dopus.com/doku.php?id=reference:command_reference:external_control_codes

More specifically:
https://docs.dopus.com/doku.php?id=reference:command_reference:external_control_codes:codes_for_passing_filenames

And if you're into a script, they are accessible through the ScriptCommandData.func.sourcetab object (selected, selected_dirs, selected_files). In a script-button, it will be ClickData.func.sourcetab.

Thanks for the kind help. It helps!