4
$\begingroup$

Is there a way to mimic the Ctrl + V for a given file?

I've got Mathematica to locate a certain file path, but I don't bother to copy the file in File Explorer (I will do this many times).

$\endgroup$
  • 1
    $\begingroup$ Clipboard? What platform? Windows? $\endgroup$ – Peter Mortensen Jan 3 '16 at 21:12
  • $\begingroup$ If you are using OS X I would recommend using apple script (or java script) $\endgroup$ – Sascha Jan 3 '16 at 22:58
  • 1
    $\begingroup$ Similar (but not necessarily a duplicate): Clipboard with transparency $\endgroup$ – Peter Mortensen Jan 3 '16 at 23:18
  • $\begingroup$ To make it more clear what you are asking, can you add an example of what you are trying to achieve? $\endgroup$ – Peter Mortensen Jan 16 '16 at 19:07
4
$\begingroup$

Building on Todd Gayleys code from the answer that Peter Mortensen pointed to in his comment here is code that I think does what the OP wants: it copies a filename to the clipboard in such a way that you can use Ctrl-V in windows explorer to copy that file to the directory which is shown in the explorer. This code of course is limited to Windows boxes:

 Needs["NETLink`"]

 FileNameToClipboard[fname_String] := Module[{dataObject},
   InstallNET[];
   NETBlock[
     dataObject = NETNew["System.Windows.Forms.DataObject"];
     dataObject@SetData["FileDrop", {fname}];
     LoadNETType["System.Windows.Forms.Clipboard"];
     Clipboard`SetDataObject[dataObject]]]

I'm not a .NET expert so I don't know if I really am using these object/methods correctly, but for me the code did what I expected...

You may have noticed that one needs to submit a list of strings to the DataOPject's SetData method. This will automatically be transformed to a collection/array that the method seems to understand and makes it trivial to provide a list of files to put to the clipboard (as Peter Mortensen has suggested). All you need to change to make this work with a list of filenames is:

  1. change the pattern for the definition to FileNameToClipboard[fname__String]:=...
  2. add another definition: FileNameToClipboard[{fnames__String}]:=FileNameToClipboard[fnames]
$\endgroup$
  • $\begingroup$ This is not asked for by the OP, but perhaps a generalisation could be demonstrated (as a variation to the already-existing code) - working for a list of files? SetDataObject will probably also work for this, but I have used the .NET function [Windows.Clipboard]::SetFileDropList($fileSet); (PowerShell syntax. It may require import of assembly "PresentationCore") from PowerShell. $fileSet (PowerShell syntax) must be of type System.Collections.Specialized.StringCollection. I created an object of that type and used Add (there is probably a more elegant way to do it). $\endgroup$ – Peter Mortensen Jan 4 '16 at 18:40
  • $\begingroup$ cont': (I don't have access to Mathematica on Windows, only on Raspberry Pi, so I can't test it (but I have tested it from PowerShell and it works as expected).) (SetFileDropList (MSDN).) $\endgroup$ – Peter Mortensen Jan 4 '16 at 18:47
  • $\begingroup$ @PeterMortensen: in fact it is almost trivial to add this, as the SetData method wants a list of strings even for a single filename (which was the only tricky part to find out in the original code). For details how you can make the function work for multiple files see my edit. I don't fully understand the magic which makes this work automatically, but most probably a list of strings is just transfered into something that happens to implement the interface which the method expects... $\endgroup$ – Albert Retey Jan 4 '16 at 20:50
2
$\begingroup$

One way to approach this is to ask the OS to do it. For example, I have a file "test.txt". I can copy this to "test2.txt" using:

Run["cp test.txt test2.txt"]

and the OS will create (copy) the file to the new name. You can programmatically change the file names and paths in the string. Run also works to run other OS functions as well.

You can, of course, also use CopyFile.

$\endgroup$
  • $\begingroup$ It seems that I need to elaborate more... I'm fully aware of CopyFile, but my intention is copying the file to clipboard (just like selecting a file and press Ctrl-V) for later use in another software. $\endgroup$ – mmjang Jan 3 '16 at 15:16
2
$\begingroup$

Your question is not very clear and as it stands I'm not sure Mathematica is the right tool for the job but your comment regarding CopyFile to bill's answer seems to indicate that you wish to copy the contents of your file to the system clipboard in which case something like:

CopyToClipboard[Import[file, "Text"]]

where file is the path to your file, may be what you require. Depending on the contents of your file importing in "Text" format may or may not work for you.

As I understand it, this is not quite the same as using Ctrl+C and then Ctrl+V in File Explorer which is essentially an "intelligent" version of CopyFile (i.e. it handles renaming files if pasted in the same directory).

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.