Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Run a program or switch to an already running instance


  • Please log in to reply
18 replies to this topic
brently
  • Members
  • 5 posts
  • Last active: Feb 17 2012 05:44 AM
  • Joined: 19 Jan 2012
Pardon me for bumping a dead topic - but doesn't IfWinExist and WinActivate do all this?? Was this original thread started before those commands were available or am I missing the point entirely?

rkayd
  • Members
  • 26 posts
  • Last active: Feb 02 2017 05:41 PM
  • Joined: 13 Jun 2012
I am wanting something like this but a different application. I edited the registry (Win7) to add an "Open in CorelDRAW" context menu item so that I can easily open pdf's in CorelDRAW instead of Acrobat. The problem with that, though, is it opens a new instance of CorelDRAW rather than use the existing CorelDRAW. Maybe that is an issue with CorelDRAW but was wondering if instead of having the pdf open directly in CorelDRAW it would run through a script like this to see if CorelDRAW was actually open and if it is then send the pdf to the open CorelDRAW. Is that possible? Or can't AHK send files to a program that is already open?

ibbignerd
  • Guests
  • Last active:
  • Joined: --

Thanks so much for the helpful suggestions! Thanks to your pointers I am figuring this out.

I have a couple questions I haven't sorted out yet:

1. When a program is running and shows an icon in the notification area but doesn't have a window, my script doesn't open up a window for it. (This is in Windows, in case that wasn't obvious.) For example, Skype and Truecrypt spend much of their time running without windows, just with notification icons. Is there a way to make my script open up a window in this case?

2. I would like some of my shortcuts to open or switch to a windows explorer window for a specific folder path. What do I need to learn to add this?

3. My script seems to break the default behavior of Windows Key-L, to lock the screen. I haven't set Win-L to be something else in my script; if I turn off autohotkey I get Win-L working to lock the screen again. Any suggestions?

Here's my modified code, which seems to mostly work. I made a change to do the switching based on the ClassID because using the PID alone doesn't work (when there are multiple processes of the same program running).

RunActivateOrSwitch(Target, WinTitle = "")
{
   ; Get the filename without a path
   SplitPath, Target, TargetNameOnly

   ; Process returns the PID of a matching process exists, or 0 otherwise
   Process, Exist, %TargetNameOnly%
   ; Get the PID and the class if the process is already running
   If ErrorLevel > 0
   {
      PID = %ErrorLevel%
      WinGetClass, ClassID, ahk_pid %PID%
   }
   ; Run the program if the process is not already running
   Else
      Run, %Target%, , , PID

   ; At least one app  wouldn't always become the active
   ; window after using Run, so we always force a window activate.
   ; Activate by title if given, otherwise use class ID. Activating by class ID 
   ; appears more robust for switching than using PID.
   If WinTitle <>
   {
      SetTitleMatchMode, 2
      WinWait, %WinTitle%, , 3
      IfWinActive, %WinTitle%
	WinActivateBottom, %WinTitle%
      Else
	WinActivate, %WinTitle%
   }
   Else
   {
      WinWait, ahk_class %ClassID%, , 3
      IfWinActive, ahk_class %ClassID%
           WinActivateBottom, ahk_class %ClassID%
      Else
	WinActivate, ahk_class %ClassID%
   }
}


I think I can answer your last two questions here....

2. I use a script like this to check if the program exists, if it does, activate it, if not, run it.

#j::if WinExist(DevicePrep)
{
	WinActivate
}
Run, "C:\Windows\DevicePrep"
return

In the first line, the "DevicePrep" is the name of the window. (folder that you are viewing)

3. It's best to use the dllcall function when doing things like locking.

#j::Run, C:\Windows\System32\PhotoScreensaver.scr /s
WinWaitActive, Screen Saver
WinWaitNotActive, Screen Saver
DllCall("LockWorkStation")
return

This code starts the screensaver PhotoScreensaver when you press Win + j. It waits till it's disabled, then locks the workstation. I use this so I can have the screensaver and have my computer secure when I walk away from it.

I hope this helped. Let me know if you have any questions

ibbignerd
  • Guests
  • Last active:
  • Joined: --

Pardon me for bumping a dead topic - but doesn't IfWinExist and WinActivate do all this?? Was this original thread started before those commands were available or am I missing the point entirely?



Yea. I feel like you can just use something like this code. All it does is checks to see if Chrome is open, if it is, it activates and maximizes it, if not, then it runs it and maximizes it. I don't see why you need so much code...

#2::
if WinExist("ahk_class Chrome_WidgetWin_1")
{
	WinActivate
	WinMaximize
}
else
    Run Chrome.exe
return