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 up to 50% on our desktop apps during our Year End Sale!Save up to 50% on our desktop apps during our Year End Sale, including DisplayFusion, ClipboardFusion, FileSeek, LogFusion, TrayStatus, and VoiceBot!Save up to 50% on our desktop apps during our Year End Sale!

Search for URL and Make Anchor

Description
This macro will replace the first URL in the selected text with the URL converted to an HTML link.
Language
C#.net
Minimum Version
Created By
Thomas Malloch (BFS)
Contributors
-
Date Created
Jan 26, 2021
Date Last Modified
Jan 26, 2021

Macro Code

using System;
using System.Web;
using System.Collections.Generic;

// The 'text' parameter will contain the text from the:
//   - Current Clipboard when run by HotKey
//   - History Item when run from the History Menu
// The returned string will be:
//   - Placed directly on the Clipboard when run as a Macro
//   - Ignored by ClipboardFusion if it is 'null'
//   - Passed along to the next action in a Trigger (null changed to an empty string)
public static class ClipboardFusionHelper
{
	public static string ProcessText(string text)
	{
        // Get the highlighted text
        text = BFS.Clipboard.CopyText();
	
        // Search the Clipboard History for a URL
		for(int i = 0; i < BFS.ClipboardFusion.GetHistoryItemCount(); i++)
		{
            // If there's no text, it can't be a url
            if(!BFS.ClipboardFusion.GetHistoryIndexContainsText(i))
                continue;
                
            // See if Uri.TryCreate can succeed
            string history = BFS.ClipboardFusion.GetHistoryText(i);
            Uri result;
            if((history.IndexOf("http") != 0) || (!Uri.TryCreate(history, UriKind.Absolute, out result)))
                continue;
                              
            // Paste what we found
            BFS.Clipboard.PasteText("<a href=\"" + history + "\">" + HttpUtility.HtmlEncode(text) + "</a>");
            break;
		}
		
		
		return null;
	}
}