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!

User Image
UWK-87
166 discussion posts
Hi Guys,

Was creating a small macro, that does the following:

  • Copy Selected Text
  • Opens Notepad.exe
  • Paste the selected text

The issue is if I do the following code, sometime CF pastes before even notepad window gets focus:

Code

using System;
using System.Collections.Generic;

public static class ClipboardFusionHelper
{
    public static string ProcessText(string text)
    {
        
        BFS.Clipboard.CopyText();
        BFS.Application.Start("notepad.exe");
        BFS.Clipboard.Paste();
        return text;
    }
}


I resolved that issue by using ThreadWait in between application start and paste, however, I believe that is not a proper method so I wanted to use the WindowFocus and that is where I get the error. Below is the code and the error:

Code

using System;
using System.Collections.Generic;

public static class ClipboardFusionHelper
{
    public static string ProcessText(string text)
    {
        
        BFS.Clipboard.CopyText();
        BFS.Application.Start("notepad.exe");
        IntPtr mainWindow = BFS.Application.GetAppIDByWindow("Untitled - Notepad"); 
        BFS.Window.Focus(mainWindow);
        //BFS.General.ThreadWait(500);
        BFS.Clipboard.Paste();
        return text;
    }
}

ERROR:

Code

The best overloaded method match for 'BFS.Application.GetAppIDByWindow(System.IntPtr)' has some invalid arguments
 Argument 1: cannot convert from 'string' to 'System.IntPtr'


Can someone kindly help with the above code. Also is there is another Macro that Thomas created "Get Filenames from Clipboard". Is it possible to achieve the following:
  • Check Selected Data, If text proceed to next step. If files then get result from "Get Filenames from Clipbpard Macro"
  • Open Notepad
  • Make sure the focus command makes it active (Is it possible to handle a case where multiple notepad files are open then focus on the newly created process?)
  • Paste either the Filenames or Text

Thanks is advance guys.
Jan 30, 2018 (modified Jan 31, 2018)  • #1
Keith Lammers (BFS)'s profile on WallpaperFusion.com
Let's solve the first problem first (verifying that the Notepad window has focus). Can you give this code a try?

Code

using System;
using System.Collections.Generic;

public static class ClipboardFusionHelper
{
    public static string ProcessText(string text)
    {
        // Copy the selected text
        BFS.Clipboard.CopyText();
        
        // Launch Notepad
        uint appID = BFS.Application.Start("notepad.exe");
        
        // Wait up to 10 seconds for it to open and receive focus
        for (int i = 0; i < 10; i++)
        {
            // If the newly launched Notepad window has focus, paste the text and break out of the loop
            if (BFS.Window.GetFocusedWindow() == BFS.Application.GetMainWindowByAppID(appID))
                {
                    BFS.Clipboard.Paste();
                    break;
                }
            
            // If we got here, the Notepad window doesn't yet have focus, wait 1 second before restarting the loop
            BFS.General.ThreadWait(1000);
        }
        return text;
    }
}
Jan 31, 2018  • #2
User Image
UWK-87
166 discussion posts
Hi Keith,

Thank you so much for helping out. Worked like a charm for the 1st issue.

Thanks,
Usama Waheed
Jan 31, 2018  • #3
Keith Lammers (BFS)'s profile on WallpaperFusion.com
Ok, try this out for the second thing :)

Code

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

public static class ClipboardFusionHelper
{
    public static string ProcessText(string text)
    {
        // Copy the selected text
        text = BFS.Clipboard.CopyText();
        
        // If the copied selection is a file list from Explorer, run the "Get Filenames from Clipboard" macro
        if (Clipboard.ContainsFileDropList())
        {
            BFS.ClipboardFusion.RunMacro("Get Filenames from Clipboard", text, out text);
            BFS.Clipboard.SetText(text);
        }
            
        // Launch Notepad
        uint appID = BFS.Application.Start("notepad.exe");
        
        // Wait up to 10 seconds for it to open and receive focus
        for (int i = 0; i < 10; i++)
        {
            // If the newly launched Notepad window has focus, paste the text and break out of the loop
            if (BFS.Window.GetFocusedWindow() == BFS.Application.GetMainWindowByAppID(appID))
                {
                    BFS.Clipboard.Paste();
                    break;
                }
            
            // If we got here, the Notepad window doesn't yet have focus, wait 1 second before restarting the loop
            BFS.General.ThreadWait(1000);
        }
        return text;
    }
}
Jan 31, 2018  • #4
User Image
UWK-87
166 discussion posts
Thanks Keith worked like a charm :D

Thank you so much for helping out!
Jan 31, 2018  • #5
Keith Lammers (BFS)'s profile on WallpaperFusion.com
No worries, glad to hear it!
Jan 31, 2018  • #6
Subscribe to this discussion topic using RSS
Was this helpful?  Login to Vote(-)  Login to Vote(-)