In 1 script: launch a program & execute a command Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
DutchPete

In 1 script: launch a program & execute a command

22 Jan 2018, 12:00

I am trying to write a script to do the above 2 things. So here is where I am at:

to launch a program this is the code
!x::
run, C:\Program Files (x86)\CherryTree\bin\cherrytree.exe

to collapse the hierarchy tree in that program this is the code
+^L (i.e. Shift+CTRL+L)

I am stuck on how to merge those lines of code in 1 script so that they are executed consecutively when the script is activated.
garry
Posts: 3758
Joined: 22 Dec 2013, 12:50

Re: In 1 script: launch a program & execute a command

22 Jan 2018, 17:22

example here for notepad
alt+x start once notepad and send text / otherwise just activate it and send text

Code: Select all

pr=notepad
sc=ahk_exe Notepad.exe
return

!x::
IfWinNotExist,%sc%
 {
 Run, %pr%,,,pid1
 WinWait,%SC%
 }
IfWinNotActive ,%SC%,,WinActivate,%SC%
    WinWaitActive,%SC%
Send,Send to notepad if active`r`n
return
A_AhkUser
Posts: 1147
Joined: 06 Mar 2017, 16:18
Location: France
Contact:

Re: In 1 script: launch a program & execute a command  Topic is solved

22 Jan 2018, 17:23

Hi,

Try the following:

Code: Select all

!x::
run, C:\Program Files (x86)\CherryTree\bin\cherrytree.exe,,, OutputVarPID ; run the program and store its process ID in 'OutputVarPID'
WinWait, ahk_pid %OutputVarPID% ; wait for the first window belonging to the process to appear
WinActivate ; activate the window (actually, if all parameters are omitted, the 'last found' window - set by the WinWait command above - is used instead)
WinWaitActive ; same remark as above
sleep, 100
Send, {Shift Down}{Ctrl Down}l{Shift Up}{Ctrl Up}
return
my scripts
DutchPete

Re: In 1 script: launch a program & execute a command

23 Jan 2018, 00:41

Hi Garry, thanks for replying. I was not sure exactly what you meant, so I tried AhkUser's suggestion. That worked.
DutchPete

Re: In 1 script: launch a program & execute a command

23 Jan 2018, 00:45

Hi AhkUser, thanks for helping out. Yes, your script works, so I am a happy bunny :), esp. because it is a workaround for a feature that is not working in the Windows version of Cherrytree, which is a good note-taking app.
I see your location is France, so I say: je vous remercie encore une fois, et peut-être à la prochaine. Salut !
garry
Posts: 3758
Joined: 22 Dec 2013, 12:50

Re: In 1 script: launch a program & execute a command

23 Jan 2018, 08:40

I'm not sure if this works ( didn't tried the program cherrytree.exe )
just an idea , if use hotkey alt+x again , shouldn't start program cherrytree again
EDIT : modified used PID

Code: Select all

pr=calc.exe
sc=ahk_exe calc.exe
;---------------------
;pr=notepad.exe
;sc=ahk_exe notepad.exe
;---------------------
;pr=C:\Program Files (x86)\CherryTree\bin\cherrytree.exe
;sc=ahk_exe cherrytree.exe
return
;-------------------------------------------------------

!x::
IfWinNotExist,%sc%
 {
 Run, %pr%,,,pid1
 scx:= "ahk_pid " . PID1
 WinWait,%scx%
 }
IfWinNotActive ,%scx%,,WinActivate,%scx%
    WinWaitActive,%scx%
;-Shift+CTRL+L =
msgbox,TEST = Send`, {Shift Down}{Ctrl Down}l{Shift Up}{Ctrl Up}
return
;========================================================
Last edited by garry on 23 Jan 2018, 09:04, edited 1 time in total.
DutchPete

Re: In 1 script: launch a program & execute a command

23 Jan 2018, 08:55

garry wrote:I'm not sure if this works ( didn't tried the program cherrytree.exe )
just an idea , if use hotkey alt+x again , shouldn't start program cherrytree again

Code: Select all

pr=C:\Program Files (x86)\CherryTree\bin\cherrytree.exe
sc=ahk_exe cherrytree.exe
return

!x::
IfWinNotExist,%sc%
 {
 Run, %pr%,,,pid     ;- pid not used in this script
 WinWait,%sc%
 }
IfWinNotActive ,%sc%,,WinActivate,%sc%
    WinWaitActive,%sc%
;msgbox,after WinWaitActive = Send`, {Shift Down}{Ctrl Down}l{Shift Up}{Ctrl Up}
;-Shift+CTRL+L =
Send, {Shift Down}{Ctrl Down}l{Shift Up}{Ctrl Up}
return
Garry, what does this code do? Right now I am able to launch CT and collapse its tree with just 1 keyboard combination.
garry
Posts: 3758
Joined: 22 Dec 2013, 12:50

Re: In 1 script: launch a program & execute a command

23 Jan 2018, 09:02

I was thinking , the program starts again if use alt+x again
I modified script above

Code: Select all

pr=calc.exe
sc=ahk_exe calc.exe
;---------------------
;pr=notepad.exe
;sc=ahk_exe notepad.exe
;---------------------
;pr=C:\Program Files (x86)\CherryTree\bin\cherrytree.exe
;sc=ahk_exe cherrytree.exe
return
;-------------------------------------------------------

!x::
IfWinNotExist,%sc%      ;- shoul'd start only once
 {
 Run, %pr%,,,pid1
 scx:= "ahk_pid " . PID1
 WinWait,%scx%
 }
IfWinNotActive ,%scx%,,WinActivate,%scx%
    WinWaitActive,%scx%
;-Shift+CTRL+L =
;Send, {Shift Down}{Ctrl Down}l{Shift Up}{Ctrl Up}
return
;========================================================
bent U van Nederland ?
http://jabod.studyit.nl/boudewijn/
DutchPete

Re: In 1 script: launch a program & execute a command

23 Jan 2018, 09:15

Why would I use Alt+X again? I only use it once to launch a number of programs, incl. CT, and after that I stay away from Alt+X.
So what does your code do? I don't understand the calc.exe and the notepad.exe parts.

Ja, ik ben Nederlander. Waarvoor zijn de mp3 files in de link?
garry
Posts: 3758
Joined: 22 Dec 2013, 12:50

Re: In 1 script: launch a program & execute a command

23 Jan 2018, 09:38

Why would I use Alt+X again? I only use it once to launch a number of programs, incl. CT, and after that I stay away from Alt+X.
just if accidentally pressed again ...
So what does your code do? I don't understand the calc.exe and the notepad.exe parts.
this was for test
Ja, ik ben Nederlander. Waarvoor zijn de mp3 files in de link?
oude muziek om te luisteren als je het alleen leuk vindt
(Ik ben oud ... maar ik denk dat je niet ...)
DutchPete

Re: In 1 script: launch a program & execute a command

23 Jan 2018, 11:33

Thanks Garry, but I would not press Alt+X again accidentally.
Dank voor de muziek, ik ben een klein beetje oud :-)

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: haomingchen1998, MrDoge and 242 guests