Hiding Error Dialogs

Anyone, especially newbies, asking for help with Photoshop Scripting and Photoshop Automation - as opposed to those contributing to discussion about an aspect of Photoshop Scripting

Moderators: Tom, Kukurykus

Paul MR

Hiding Error Dialogs

Post by Paul MR »

I have had a go with C# to monitor the Photoshop CPU usage.

I found that if there was modal window it needed more that just enter sending to the application, as when Photoshop is braught to the front the modal window was not in focus and it needed selecting with the mouse.
So the following does a check to see if Photoshop CPU usage is zero the checks a few seconds later, if it still zero Photoshop is selected, the mouse cursor it moved to the center of the screen and left mouse click simulated to bring focus to the window then an Enter is sent. I use File - File Info and Panel Options in the Layers Palette to do the testing.
Maybe it could work for you?
It's a console app and will terminate with ether control/c or if Photoshop is closed.

Code: Select allusing System;
using System.Diagnostics;
using System.Threading;
using System.Collections;
using System.Windows.Forms;
using System.Runtime.InteropServices;
/*
References
----------
system
System.Core
System.Data
System.Drawing
System.Windows.Forms
 */
namespace PhotoshopMonitor
{
public class Driver
{
[DllImport("user32.dll")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

[DllImport("User32")]
public static extern int SetForegroundWindow(IntPtr hwnd);
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint cButtons, uint dwExtraInfo);

private const int MOUSEEVENTF_ABSOLUTE = 0x8000;
    private const int MOUSEEVENTF_LEFTDOWN = 0x0002;
    private const int MOUSEEVENTF_LEFTUP = 0x0004;
    private const int MOUSEEVENTF_MIDDLEDOWN = 0x0020;
    private const int MOUSEEVENTF_MIDDLEUP = 0x0040;
    private const int MOUSEEVENTF_MOVE = 0x0001;
    private const int MOUSEEVENTF_RIGHTDOWN = 0x0008;
    private const int MOUSEEVENTF_RIGHTUP = 0x0010;
    private const int MOUSEEVENTF_WHEEL = 0x0800;
    private const int MOUSEEVENTF_XDOWN = 0x0080;
    private const int MOUSEEVENTF_XUP = 0x0100;

        static void Main(string[] args)
        {
            killProcess();
        }

        private static void killProcess()
        {
            Console.WriteLine("Monitoring Photoshop CPU usage...\n\rControl/c to exit");
            int intInterval = 2500;
            string procName = "Photoshop";

            while (true)
            {
                Process[] pname = Process.GetProcessesByName("Photoshop");
                if (pname.Length == 0) break;

                Process[] runningNow = Process.GetProcesses();
                foreach (Process process in runningNow)
                {
                    using (PerformanceCounter pcProcess = new PerformanceCounter("Process", "% Processor Time", process.ProcessName))
                    {
                        if (process.ProcessName == procName)
                        {
                            pcProcess.NextValue();
                            System.Threading.Thread.Sleep(intInterval);
                           
                           // Console.WriteLine("Process:{0} CPU% {1}", process.ProcessName, pcProcess.NextValue());
                            //if Photoshop CPU has been idle for 5 seconds
                            //Center mouse, send a left mouse click then an enter.
                            IntPtr iHandle = FindWindow("Photoshop", null);

                            if (pcProcess.NextValue() == float.Parse("0"))
                            {       
                                SetForegroundWindow(iHandle);
                                Cursor.Position = new System.Drawing.Point(Screen.PrimaryScreen.Bounds.Width / 2,
                            Screen.PrimaryScreen.Bounds.Height / 2);
                                uint X = (uint)Cursor.Position.X;
                                uint Y = (uint)Cursor.Position.Y;
                                mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, X, Y, 0, 0);
                                SendKeys.SendWait("{ENTER}");
                                // return;

                            }
                        }
                    }
                }

                // Sleep till the next loop
                Thread.Sleep(intInterval);

            }

        }

    }

}




If you want to try just the binary..