命令行访问剪贴板

Table of Contents

有的时候虽然已经用鼠标右键拷贝文件,但在终端shell中的时候,贴贴还是需要打开任务管理器来做,非常不方便,所以我就写了几个小工具gclip-toolbox来操作。以windows为例,剪贴板提供了多种数据类型的支持,text、image、droplist这三种是常见的数据类型,顾名思义就是文本、图像、文件路径列表。下面就用c#演示一下,对剪贴板中这三种数据类型的操作,用到的工具就只需要csc.exe这个c#编译器,没有三方依赖。

检测剪贴板内容的类型

using System;
using System.Collections.Specialized;
using System.Drawing;
using System.Windows.Forms;

class Program {
    [STAThread]
    static void Main(string[] args) {
        try {
            if (Clipboard.ContainsText()) {
                string clipboardText = Clipboard.GetText();
                Console.WriteLine("Text found in the clipboard: " + clipboardText);
            }

            if (Clipboard.ContainsFileDropList()) {
                StringCollection filePaths = Clipboard.GetFileDropList();
                Console.WriteLine("File path list found in the clipboard:");
                foreach(string filePath in filePaths) {
                    Console.WriteLine(filePath);
                }
            }

            if (Clipboard.ContainsImage()) {
                Image clipboardImage = Clipboard.GetImage();
                Console.WriteLine("Image found in the clipboard: " + clipboardImage);
            }
        } catch (Exception ex) {
            Console.WriteLine("An exception occurred: " + ex.Message);
        }
    }
}

上面的代码是显示剪贴板中数据类型的demo,只要通过Clipboard对象就可以对其进行后续的操作。

gcopy 拷贝文件

using System;
using System.IO;
using System.Windows.Forms;
using System.Collections.Specialized;

class Program {
    [STAThread]
    static void Main(string[] args) {
        try {
            if (args.Length > 0) {
                StringCollection filePaths = new StringCollection();

                foreach(string arg in args) {
                    if (File.Exists(arg)) {
                        string fullPath = Path.GetFullPath(arg);
                        filePaths.Add(fullPath);
                        Console.WriteLine("File added: " + fullPath);
                    } else {
                        Console.WriteLine("File does not exist: " + arg);
                    }
                }

                Clipboard.SetFileDropList(filePaths);
            }

        } catch (Exception ex) {
            Console.WriteLine("An exception occurred: " + ex.Message);
        }
    }
}

这是gclip-toolbox下的gcopy.cs文件,它的作用就是针对文件们,用gcopy这样的命令将其复制到剪贴板中作为droplist数据。用法也非常简单

csc.exe gcopy.cs
gcopy [flie1] [file2] [file3] ...

gpaste 贴贴文件

using System;
using System.IO;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Windows.Forms;
using System.Text;
using System.Runtime.Serialization.Json;

class Program {
    [STAThread]
    static void Main(string[] args) {
        try {
            List < string > result = new List < string > ();

            StringCollection fileDropList = Clipboard.GetFileDropList();

            if (fileDropList != null && fileDropList.Count > 0) {

                bool resultAdded = false;
                foreach(string destinationFolderPath in args.Length > 0 ? args : new [] {
                    Directory.GetCurrentDirectory()
                }) {
                    foreach(string sourceFilePath in fileDropList) {
                        string destinationFilePath = Path.Combine(destinationFolderPath, Path.GetFileName(sourceFilePath));
                        if (!IsValidPath(destinationFolderPath) || !IsValidPath(sourceFilePath)) {
                            continue;
                        }

                        File.Copy(sourceFilePath, destinationFilePath, true);
                        if (!resultAdded) {
                            result.Add(sourceFilePath);
                        }
                    }
                    resultAdded = true;
                }
            } else {
                Console.WriteLine("Clipboard does not contain a list of files.");
            }

            Console.WriteLine(string.Join("\n", result));
        } catch (Exception ex) {
            Console.WriteLine("An exception occurred: " + ex.Message);
        }
    }

    static bool IsValidPath(string path) {
        return Directory.Exists(path) || File.Exists(path);
    }
}

这是gclip-toolbox下的gpaste,它就是上面gcopy的贴贴操作,只要指定目录就可以将剪贴板中拷贝的文件复制到特定路径,或者不指定路径直接把文件贴贴到当前目录中。

gpaste
gpaste [dir1] [dir2] ...

gpaste-image 贴贴图像

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;
using System.Collections.Specialized;

class Program {

    [STAThread]
    static void Main(string[] args) {
        string defaultFilename = "clipboard_image.png";
        string filename = args.Length > 0 ? args[0] : defaultFilename;

        if (Clipboard.ContainsImage()) {
            Image clipboardImage = Clipboard.GetImage();
            clipboardImage.Save(filename, ImageFormat.Png);
            Console.WriteLine("Image saved successfully.");
        } else {
            Console.WriteLine("No image found in the clipboard.");
        }
    }
}

gpaste-image和gpaste相似,不过是针对文件这种数据类型的贴贴。

gpaste-image
gpaste-image [image name]