Paste URL from clipboard text?

Hi, how can I achieve this:

When the clipboard contains any URL text (e.g. "http://resource.dopus.com") then Ctrl-V should create a Internet Shortcut file (e.g. "Clipboard URL.url") instead of a text file "Clipboard Text.txt".

You may be able to do this with one of the scripting tools, like AutoHotKey. Otherwise, a request to GPSoftware would be the only other way afaik.

1 Like

.URL files are just text files so you can do it using an MS-DOS Batch Function button that runs this:

@runmode hide
echo.[InternetShortcut]>"Clipboard URL.url"
echo.URL={clip}>>"Clipboard URL.url"

Full button code for pasting on to a toolbar:

<?xml version="1.0"?>
<button backcol="none" display="both" textcol="none">
	<label>Paste URL</label>
	<icon1>#clippasteshortcut</icon1>
	<function type="batch">
		<instruction>@runmode hide</instruction>
		<instruction>echo.[InternetShortcut]&gt;&quot;Clipboard URL.url&quot;</instruction>
		<instruction>echo.URL={clip}&gt;&gt;&quot;Clipboard URL.url&quot;</instruction>
	</function>
</button>

Leo, you are a genius, thank you!

Before having seen your answer I've written a small program which decides itself if the clipboard text is an URL or just simple text (see attached file):


This program can be used with Directory Opus to paste text from the clipboard to a file into the current Lister, either as a text file or as a Internet Shortcut:

In Directory Opus, create a button or a keyboard command with the following script (for example):

<Path to the program>\PasteTextOrURL.exe {s}

When you execute this command in Directory Opus the following will happen:

  • If the clipboard contains just simple text then a text file "Clipboard Text.txt" will be created in the current Lister which contains the text from the clipboard.

  • If the clipboard contains a Internet URL text (e.g. http://resource.dopus.com) then a Internet Shortcut file will be created in the current Lister pointing to the URL from the clipboard.

This program is free to use for everybody. If anybody wants to extend it (e.g. to work with other URL types, etc.) then please leave a comment here.


I plan to extend the program to work with different image formats too (could paste different image file formats from a clipboard image, etc.). Any other ideas are welcome.

PasteTextOrURL.zip (183 KB)

I thought you wanted the press of Ctrl V to automatically determine whether the clipboard contents was a url or other text that's why my answer was that it cannot be done with Opus alone.

That's right, for this purpose I wrote the above program which has this functionality (when the clipboard contains simple text). I will extend its functionality either to:

  • reverse to the DOpus funktionality when the clipboard contains anything else than simple text, or:
  • include all of the current DOpus Paste functionality (paste images, files etc.), or:
  • a combination of both.

Beside this, I still would like DOpus to include this functionality (to distinguish between a URL and any other text in the clipboard) because it is faster.

Wow! When is that coming?

It's already attached above.

I'm trying to make a similar button on vbsript, but for some reason it does not work.

Option Explicit

Function OnClick(ByRef ClickData)

Dim dlg
Dim objFSO
Dim objFile
Dim output

Set dlg = ClickData.Func.Dlg

dlg.title = "Create URL Document"
dlg.message = "Enter web-address:"
dlg.buttons = "OK|Cancel"
' ? Clipboard contents are not automatically inserted
dlg.default = "{clip}"
dlg.Select = True
dlg.max = 256

output = dlg.show

Set objFSO = CreateObject("Scripting.FileSystemObject")
' ? If {s} is replaced by c:, the file is created
Set objFile = objFSO.CreateTextFile("{s}\{Date|yyyy-MM-dd}_{Time|HH.mm.ss}.url", True)
objFile.Write "[InternetShortcut]" & vbCrLf
objFile.Write "URL=" & Trim(dlg.input) & vbCrLf
objFile.Close

End Function

Using things like {clip} and {s}{Date|yyyy-MM-dd}_{Time|HH.mm.ss} will not work in VBScript.

You have to use VBScript code and the objects provided to you by Opus and WIndows to do those things.

For example, Opus has an object which lets you get the clipboard.

By the way, posting a whole script and just saying "it does not work" is a bad way to ask for help. If the problem is not obvious from just looking at the script then people probably will not help you.

It is best to give as much detail as possible about what is going wrong (what is the error message? what do you expect to happen? what happens instead? etc.).

It also helps to break the problem down into something as simple as possible by removing all the extra code that is not needed to demonstrate the problem. That not only means the people trying to help have less code to look at; it often means you solve the problem yourself by narrowing down where it is occurring and knowing what to focus on.

Ideally you want one line of code where you can point to it and say "I expect X to happen but instead see Y". (That isn't always possible, of course.)

You're also more likely to receive help if you link your account.

{s}{Date|yyyy-MM-dd}_{Time|HH.mm.ss} works fine, but in the current script not work {clip} and {s} only.

At the moment I replaced the following strings:

dlg.default = "{clip}"
to
dlg.default = DOpus.GetClip

and

Set objFile = objFSO.CreateTextFile("{s}{Date|yyyy-MM-dd}_{Time|HH.mm.ss}.url", True)
to
Set objFile = objFSO.CreateTextFile(ClickData.Func.SourceTab.Path & "{Date|yyyy-MM-dd}_{Time|HH.mm.ss}.url", True)

Now everything is OK. Thank you, Leo.

Those codes work in Opus commands. They don't mean anything to VBScript.

They work in that other script because they are fed to things which run Opus commands.

PeterPanino, you asked for suggestions (and thanks to you and Leo for the buttons).

  1. I have adapted Leo's button so that I can enter a chosen filename before pasting. I added the initial line:
    @Set TheFilename={DlgString|Enter the filename (without extension):}
    and then twice changing Clipboard URL.url to {$TheFilename}.url lower down.

Your .exe programme could allow a similar tactic if it had an optional second parameter for the filename without extension. Then my same initial line could be used, for example, with the command:
<path>\PasteTextOrURL.exe {s} {$TheFilename}

The second parameter should be optional, with the present filename as default, because many people would prefer to rename the file afterwards.

If your programme ends up handling ever more file types, a further optional parameter for the extension could be useful.


  1. I don't personally like the idea of redefining Ctrl+V, which is so well established as a pure paste operation. Better to have a separate button.

  1. Dragging the webpage's favicon to a DOpus lister also produces a .url file, but with two differences:
  • A filename is produced from somewhere in the webpage. This could be useful.
  • An icon is specified within the .url file by giving its path. I'm not sure what the purpose of this is, and anyway, the icon is in a Cache within the Firefox profile, so a backup routine would probably lose the pointer to it.

Is there any point in trying to emulate this? It would involve DOpus knowing and reading the webpage the URL had been copied from, and also storing the icon within the .url file somehow, so it would end up significantly more elaborate.

"Final" version of the button

Full script (workable)

Option Explicit

Function OnClick(ByRef ClickData)

Dim dlg
Dim input
Dim objFSO
Dim objFile

If DOpus.GetClipFormat = "text" Then
input = DOpus.GetClip
Else
input = ""
End If

Set dlg = ClickData.Func.Dlg

dlg.title = "Create URL Document"
dlg.message = "Enter URL-address:"
dlg.buttons = "OK|Cancel"
dlg.default = input
dlg.Select = True
dlg.max = 256
dlg.show

If dlg.result = 1 And dlg.input <> "" Then

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.CreateTextFile(ClickData.Func.SourceTab.Path & "\" & Year(Now) & "-" & Right("0" & Month(Now), 2) & "-" & Right("0" & Day(Now), 2) & "_" & Right("0" & Hour(Now), 2) & "." & Right("0" & Minute(Now), 2) & "." & Right("0" & Second(Now), 2) & ".url", True)
objFile.Write "[InternetShortcut]" & vbCrLf
objFile.Write "URL=" & Trim(dlg.input) & vbCrLf
objFile.Close

End If

End Function

Full button (workable)

<?xml version="1.0"?>
<button backcol="none" display="label" textcol="none">
	<label>URL Document</label>
	<icon1>#empty</icon1>
	<function type="script">
		<instruction>Option Explicit</instruction>
		<instruction />
		<instruction>Function OnClick(ByRef ClickData)</instruction>
		<instruction />
		<instruction>Dim dlg</instruction>
		<instruction>Dim input</instruction>
		<instruction>Dim objFSO</instruction>
		<instruction>Dim objFile</instruction>
		<instruction />
		<instruction>If DOpus.GetClipFormat = &quot;text&quot; Then</instruction>
		<instruction>input = DOpus.GetClip</instruction>
		<instruction>Else</instruction>
		<instruction>input = &quot;&quot;</instruction>
		<instruction>End If</instruction>
		<instruction />
		<instruction>Set dlg = ClickData.Func.Dlg</instruction>
		<instruction />
		<instruction>dlg.title = &quot;Create URL Document&quot;</instruction>
		<instruction>dlg.message = &quot;Enter URL-address:&quot;</instruction>
		<instruction>dlg.buttons = &quot;OK|Cancel&quot;</instruction>
		<instruction>dlg.default = input</instruction>
		<instruction>dlg.Select = True</instruction>
		<instruction>dlg.max = 256</instruction>
		<instruction>dlg.show</instruction>
		<instruction />
		<instruction>If dlg.result = 1 And dlg.input &lt;&gt; &quot;&quot; Then</instruction>
		<instruction />
		<instruction>Set objFSO = CreateObject(&quot;Scripting.FileSystemObject&quot;)</instruction>
		<instruction>Set objFile = objFSO.CreateTextFile(ClickData.Func.SourceTab.Path &amp; &quot;\&quot; &amp; Year(Now) &amp; &quot;-&quot; &amp; Right(&quot;0&quot; &amp; Month(Now), 2) &amp; &quot;-&quot; &amp; Right(&quot;0&quot; &amp; Day(Now), 2) &amp; &quot;_&quot; &amp; Right(&quot;0&quot; &amp; Hour(Now), 2) &amp; &quot;.&quot; &amp; Right(&quot;0&quot; &amp; Minute(Now), 2) &amp; &quot;.&quot; &amp; Right(&quot;0&quot; &amp; Second(Now), 2) &amp; &quot;.url&quot;, True)</instruction>
		<instruction>objFile.Write &quot;[InternetShortcut]&quot; &amp; vbCrLf</instruction>
		<instruction>objFile.Write &quot;URL=&quot; &amp; Trim(dlg.input) &amp; vbCrLf</instruction>
		<instruction>objFile.Close</instruction>
		<instruction />
		<instruction>End If</instruction>
		<instruction />
		<instruction>End Function</instruction>
	</function>
</button>

If URL-address contains ampersand, the link is broken, because in the batch files single ampersands don't work. Therefore, the quotes around {clip}, can solve the problem.

Have you considered using the inline rename to change the name after the file has been created? Personally I would rather use the inline rename than a dialog. You could have Leo's above script enter inline rename for the file after it has been created like so.

@runmode hide
echo.[InternetShortcut]>"Clipboard URL.url"
echo.URL={clip}>>"Clipboard URL.url"
Select "Clipboard URL.url" EXACT MAKEVISIBLE
rename inline

Aside, Getting SELECT to set the focus seemed impossible. It was not until I came across this post how to select a newly created file? that I realised its was possible via MAKEVISIBLE.

My intention was to answer PeterPanino's invitation for suggestions — renaming before or after file creation is of course only personal preference. But thanks for bringing to attention the clever method of selecting the newly created file.

Two remarks, which probably indicate that I have misunderstood something: First, what are the two "echo. . . ." lines doing? I added your five lines below the command for the .exe file , and commenting out these two lines doesn't seem to affect the outcome (using MS-DOS because otherwise the timing goes wrong). Secondly, your code only applies to a newly created .url file, whereas PeterPanino's programme creates a .txt file when the clipboard contains text.

Yes the solutions are different. An exe is one way, a script is IMO better as there is no hidden code or external resource needed. YankeeZulu's looks nice deals with a few edge cases, Leo's, is simpler but prob will do what is needed.

Leo's solution does not need the exe, so you would only add the last two lines. The echo is a dos method of pushing text to a file. echo.[InternetShortcut]>"Clipboard URL.url", means write [InternetShortcut] to file Clipboard URL.url.

The script is a mix of dos and dopus commands

@runmode hide - Dopus command: Hide the dos window
echo.[InternetShortcut]>"Clipboard URL.url" - Dos command: write line one to file.
echo.URL={clip}>>"Clipboard URL.url" - Dos command: write line two to file.
Select "Clipboard URL.url" EXACT MAKEVISIBLE - Dopus command: select and set focus on the file
rename inline - Dopus command: enter inline rename for the item that has focus.

You can use the inline rename after calling the exe without changing the exe. If you don't know the name of the file to select (perhaps the exe created a file with a timespamp in the name, or a txt file) you could select newest file in the folder.

The select and enter inline rename pattern should work with any of the provided solutions (the exe or the script).

Thanks, wowbagger, I've learnt something new — I didn't know about the echo. commands for getting text into a new file (I was testing them with PeterPanino's .exe command still there in the line above). Now, of course, I can adapt your lines to choose the filename in advance by
@Set TheURLName={dlgstring|Enter the name of the URL shortcut file (without extension):}

One problem, many alternative solutions, as happens so often with DOpus.

I have 4 variations of this. Lets say I download a file "7z922-x64.msi" into its own folder called "7-zip". Sometimes I want to copy some text and save it as (the parent folder...) "7-zip.txt", or a url and save it as "7-zip.url", or save it as (by selecting the file 7z922-x64.msi file first before pressing the button...) 7z922-x64.txt, or 7z922-x64.url. The 4 buttons I use for this are:

  1. "Paste {c} to Parent.txt"
@set namepart "{sourcepath|nopath|noterm}.txt"
Clipboard PASTE AS {$namepart}
  1. "Paste {clip} to Parent.url"
@set namepart "{sourcepath|nopath|noterm}.url"
@set clipsave "{clip}"
Clipboard EXPANDNEWLINES SET [{000214A0-0000-0000-C000-000000000046}]\nProp3=19,2\n[InternetShortcut]\nURL={clip}\nIDList=
Clipboard PASTE AS {$namepart}
Clipboard SET {$clipsave}
  1. "Paste {clip} to (Selected).txt"
@set clipsave {clip}
Clipboard COPYNAMES=nopaths REGEXP "(.*)\.([^.]*)" "\1"
@set namepart "{clip}.txt"
Clipboard SET {$clipsave}
Clipboard PASTE AS {$namepart}
  1. "Paste {clip} to (Selected).url"
@set clipsave "{clip}"
Clipboard COPYNAMES=nopaths REGEXP "(.*)\.([^.]*)" "\1"
@set namepart "{clip}.url"
Clipboard SET {$clipsave}
Clipboard EXPANDNEWLINES SET [{000214A0-0000-0000-C000-000000000046}]\nProp3=19,2\n[InternetShortcut]\nURL={clip}\nIDList=
Clipboard PASTE AS {$namepart}
Clipboard SET {$clipsave}

I got the funny looking url files by creating a .url shortcut using Windows Explorer and simply looking at it in a hex viewer. No idea if this works generically but it does at my home and work (Win 7 Pro and Win 8.1 Pro),

Spiro

Don't know if the way to solve the OP's question have changed since 2009, but @SpiroC your buttons (which do something different) work perfectly for me. :slight_smile:
Thanks!