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?

Save Text or Image to Disk

Description
Prompts for a folder and a filename and saves the selected item (or clipboard contents) to the disk. Only works for text or images.
Language
C#.net
Minimum Version
Created By
Keith Lammers (BFS)
Contributors
-
Date Created
Apr 3, 2019
Date Last Modified
Apr 5, 2019

Macro Code

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

public static class ClipboardFusionHelper
{
	public static string ProcessText(string text)
	{
        // Get the selected item index if applicable
        int selectedIndex = BFS.ClipboardFusion.GetClipboardManagerSelectedIndex();
	
        // Prompt for the folder path
        string folderPath = BFS.Dialog.GetUserInput("Please enter a folder path to save to:", @"%USERPROFILE%\Documents\ClipboardFusion Saved Items\");
        folderPath = Environment.ExpandEnvironmentVariables(folderPath);
        if (!(folderPath.EndsWith(@"\")))
            folderPath += @"\";
            
        // Create the folder if needed
        if (!(Directory.Exists(folderPath)))
            Directory.CreateDirectory(folderPath);           
        
        // If run by right-clicking a Clipboard Manager item, check if it's text
        if (BFS.ClipboardFusion.GetSelectedListItemContainsText(selectedIndex))
        {
            // Prompt for the filename
            string fileName = BFS.Dialog.GetUserInput("Please enter the file name for this text:", @"File.txt");
            
            // Check if the file exists and update the filename if needed
            fileName = CheckIfFileExists(folderPath, fileName);
            
            // Write the file
            File.WriteAllText(folderPath + fileName, BFS.ClipboardFusion.GetSelectedListText(selectedIndex));
        }
        
        // If run by right-clicking a Clipboard Manager item, check if it's an image
        else if (BFS.ClipboardFusion.GetSelectedListItemContainsImage(selectedIndex))
        {
            // Prompt for the filename
            string fileName = BFS.Dialog.GetUserInput("Please enter the file name for this image:", @"File.jpg");
            
            // Check if the file exists and update the filename if needed
            fileName = CheckIfFileExists(folderPath, fileName);
            
            // Write the file
            BFS.ClipboardFusion.GetSelectedListImage(selectedIndex).Save(folderPath + fileName, System.Drawing.Imaging.ImageFormat.Jpeg);         
        }
        
        // If it's not run by right-clicking a Clipboard Manager item, check if the clipboard contains text
        else if (Clipboard.ContainsText())
        {
            // Prompt for the filename
            string fileName = BFS.Dialog.GetUserInput("Please enter the file name for this text:", @"File.txt");
            
            // Check if the file exists and update the filename if needed
            fileName = CheckIfFileExists(folderPath, fileName);
            
            // Write the file
            File.WriteAllText(folderPath + fileName, Clipboard.GetText());
        }
        
        // If it's not run by right-clicking a Clipboard Manager item, check if the clipboard contains an image
        else if (Clipboard.ContainsImage())
        {
            // Prompt for the filename
            string fileName = BFS.Dialog.GetUserInput("Please enter the file name for this image:", @"File.jpg");
            
            // Check if the file exists and update the filename if needed
            fileName = CheckIfFileExists(folderPath, fileName);
            
            // Write the file
            Clipboard.GetImage().Save(folderPath + fileName, System.Drawing.Imaging.ImageFormat.Jpeg);         
        }
        
        // Exit the macro
		return null;
	}
	
	public static string CheckIfFileExists(string folderPath, string fileName)
	{
        if (File.Exists(folderPath + fileName))
        {
            int i = 1;
            string extension = Path.GetExtension(folderPath + fileName);
            string fileNameNoExtension = Path.GetFileNameWithoutExtension(fileName);
            string newFileName = fileNameNoExtension + "0" + i + extension;
            while (File.Exists(folderPath + newFileName))
            {
                if (i < 10)
                    newFileName = fileNameNoExtension + "0" + i + extension;
                else if (i > 10)
                    newFileName = fileNameNoExtension + i + extension;
                i++;
            }
            return newFileName;
        }
        else
        {
            return fileName;
        }
	}
}