Processing Ajax...

Title

Message

Confirm

Confirm

Confirm

Confirm

Are you sure you want to delete this item?

Confirm

Are you sure you want to delete this item?

Confirm

Are you sure?

Export Online Pinned Items

Description
This macro will prompt you to select the Online Pinned Items that you want to export. It will then prompt to select a folder and will save each item to its own text file in that folder.
Language
C#.net
Minimum Version
Created By
Thomas Malloch (BFS)
Contributors
-
Date Created
4d ago
Date Last Modified
4d ago

Macro Code

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.IO;
using System.Text;
using System.Drawing.Imaging;

public static class ClipboardFusionHelper
{
	public static string ProcessText(string text)
	{
		//open the custom form
		using(ClipboardHistorySelectForm dialog = new ClipboardHistorySelectForm())
            dialog.ShowDialog();
			
        return null;		
	}
	
	//this form will display a multi-select list with all the clipboard history items
	private class ClipboardHistorySelectForm : Form
	{
		//the form's controls
		private ListView lvClipboardHistory;
		private Button btnExport;
		private Button btnCancel;
		private Button btnSelectAll;
		
		//the constrcutor will build the form and the controls
		public ClipboardHistorySelectForm()
		{
			this.lvClipboardHistory = new ListView();
			this.btnExport = new Button();
			this.btnCancel = new Button();
			this.btnSelectAll = new Button();
			
			this.SuspendLayout();
			
			//add a column to the list so we get a vertical scroll bar
			ColumnHeader column = new ColumnHeader();
			column.Width = 288;
			
			//set up the listview
			this.lvClipboardHistory.Location = new Point(16, 16);
			this.lvClipboardHistory.Size = new Size(312, 392);
			this.lvClipboardHistory.View = View.Details;
			this.lvClipboardHistory.HeaderStyle = ColumnHeaderStyle.None;
			this.lvClipboardHistory.Columns.Add(column);
			this.lvClipboardHistory.FullRowSelect = true;
			
			//set up the select all button
			this.btnSelectAll.Location = new System.Drawing.Point(8, 416);
			this.btnSelectAll.Size = new System.Drawing.Size(80, 24);
			this.btnSelectAll.Text = "Select All";
			
			//set up the export button
			this.btnExport.Location = new System.Drawing.Point(160, 416);
			this.btnExport.Size = new System.Drawing.Size(80, 24);
			this.btnExport.Text = "Export";

			//set up the cancel button
			this.btnCancel.Location = new System.Drawing.Point(248, 416);
			this.btnCancel.Size = new System.Drawing.Size(80, 24);
			this.btnCancel.Text = "Cancel";
			
			//set up the form
			this.AcceptButton = this.btnExport;
			this.CancelButton = this.btnCancel;
			this.ClientSize = new Size(344, 456);
			this.Controls.Add(this.btnSelectAll);
			this.Controls.Add(this.btnCancel);
			this.Controls.Add(this.btnExport);
			this.Controls.Add(this.lvClipboardHistory);
			this.Text = "Online Pinned";
			
			this.ResumeLayout(false);
			
			//set up the events
			this.btnCancel.Click += this.btnCancel_Click;
			this.btnExport.Click += this.btnExport_Click;
			this.btnSelectAll.Click += this.btnSelectAll_Click;
			this.Load += this.Form_Load;
		}
		
		//when the form loads, fill the listview with the items from the clipboard history
		private void Form_Load(object sender, EventArgs e)
		{
			this.lvClipboardHistory.Items.Clear();
			foreach(string text in BFS.ClipboardFusion.CFOGetAllPinnedText())
			{
                HistoryListViewItem item = new HistoryListViewItem();
                item.HasText = true;
                item.HasImage = false;
                item.HistoryText = text;               
                item.Text = item.HistoryText;
                if(item.Text.Length > 50)
                    item.Text = item.Text.Substring(0, 50);
                
                if((string.IsNullOrEmpty(item.Text)) && (item.HasImage))
                    item.Text = "[Image]";
                
				this.lvClipboardHistory.Items.Add(item);
            }
		}
		
		//this function gets called when the Export button is clicked
		private void btnExport_Click(object sender, EventArgs e)
		{
			string baseFolder = "";
			using(FolderBrowserDialog dialog = new FolderBrowserDialog())
			{
				dialog.Description = "Save Items to Selected Folder";
				if(dialog.ShowDialog(this) != DialogResult.OK)
					return;
				
				baseFolder = dialog.SelectedPath;
			}
			
			//add the selected clipboard history items to the list
			for(int i = 0; i < this.lvClipboardHistory.SelectedItems.Count; i++)
			{
                HistoryListViewItem historyItem = this.lvClipboardHistory.SelectedItems[i] as HistoryListViewItem;
                if(historyItem == null)
                    continue;
			
                if(historyItem.HasText)
                {
					string filename = Path.Combine(baseFolder, $"text{i+1}.txt");
                    using(FileStream file = File.Open(filename, FileMode.OpenOrCreate, FileAccess.Write))
                    using(StreamWriter writer = new StreamWriter(file, Encoding.Unicode))
                        writer.Write(historyItem.HistoryText);
                }
                
                if(historyItem.HasImage)
                {                    
					string filename = Path.Combine(baseFolder, $"image{i+1}.png");                        
                    historyItem.HistoryImage.Save(filename, ImageFormat.Png);                    
                }
			}
				
			//set the dialog result and close the form
			this.DialogResult = DialogResult.OK;
			this.Close();
		}
		
		private void btnSelectAll_Click(object sender, EventArgs e)
		{
			// select all the items
			foreach(ListViewItem item in this.lvClipboardHistory.Items)			
				item.Selected = true;
		}
		
		private void btnCancel_Click(object sender, EventArgs e)
		{			
			//set the dialog result and close the form
			this.DialogResult = DialogResult.Cancel;
			this.Close();
		}
	}
	
	private class HistoryListViewItem : ListViewItem
	{
        public bool HasImage { get; set; }
        public bool HasText { get; set; }
        public string HistoryText { get; set; }
        public Bitmap HistoryImage { get; set; }
	}
}