Replace Default String - Print Dialog

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
wideeyedguy
Posts: 60
Joined: 11 Feb 2016, 11:49

Replace Default String - Print Dialog

16 Aug 2018, 13:04

I'm trying to create a script that will trigger when a Bullzip Print dialog displays.
Print dialog is known as Bullzip PDF Printer 11.7 - Create File from Window Spy.

The script will test for a specific text string and replace with an alternate string in the file naming field.
BZprintdialog1.png
BZprintdialog1.png (23.38 KiB) Viewed 2252 times
If the File Name field (also known as ClassNN: TextBoxU15) contains
"C:\Users\username\Documents\Litigation\temp dump\PDF printouts\public.hcad.org - Print.pdf" ,
it would replace "public.hcad.org - Print.pdf" with "cad.pdf".
I don't have any experience with using IF statements in AHK, so not sure how to set this up.

Any help is appreciated.

Thanks in advance,
weg
I say this optimistically . . . One day I'll understand it.
But today is not that day!
wideeyedguy
Posts: 60
Joined: 11 Feb 2016, 11:49

Re: Replace Default String - Print Dialog

17 Aug 2018, 14:53

Where am I messing up? The procedure seems to simply be replacing the original string with itself back into the dialog Filename field.
Not understanding the StrReplace statement, I suppose.

Code: Select all

#IfWinActive Bullzip PDF Printer 11.7 - Create File
RButton::
ControlGetText, origFilename, TextBoxU15
;MsgBox  %origFilename%
defaultName = public.hcad.org - Print.pdf
newName = cad
IfInString, origFilename, %defaultName%
{
    StringReplace, OutputVar, origFilename, defaultName , newName
    send %OutputVar%
    return
}
#IfWinActive
And how can I have this residing in the background and have it trigger without using the Right mouse button?

Thanks in advance,
weg
I say this optimistically . . . One day I'll understand it.
But today is not that day!
Guest

Re: Replace Default String - Print Dialog

18 Aug 2018, 03:30

It is StringReplace, OutputVar, origFilename, %defaultName%, %newName%
Remember to wrap your variables in %%. You can also use the StrReplace function:
OutputVar:=StrReplace(origFilename, defaultName, newName)
in which case you don't wrap %% around your variable names.

You can use a SetTimer + IfWinActive to check for a Window. Or a shellhook even (google "shellhook new window autohotkey" if you want to know) but a simple WinWaitActive should do it as well like so:

Code: Select all

origFilename=C:\Users\username\Documents\Litigation\temp dump\PDF printouts\public.hcad.org - Print.pdf
WinTitle=Bullzip PDF Printer 11.7 - Create File

defaultName = public.hcad.org - Print
newName = cad

Start:
WinWaitActive, %WinTitle%
Sleep 100 ; just give it a bit of time
ControlGetText, origFilename, TextBoxU15, %WinTitle%
IfInString, origFilename, %defaultName%
	{
    StringReplace, OutputVar, origFilename, %defaultName%, %newName%
    ; ControlSetText [, Control, NewText, WinTitle, WinText, ExcludeTitle, ExcludeText]
    ; ControlSetText, TextBoxU15, %OutputVar%, %WinTitle%
    MsgBox %OutputVar%
	}
Sleep 100 ; just give it a bit of time
; Send {enter} ; to save?
; Gosub, Start
Return
Just so you know: there is a nice function by justme WatchFolder() https://autohotkey.com/boards/viewtopic.php?f=6&t=8384 which can watch a folder :-) that way you could automatically move new files created in the "C:\Users\username\Documents\Litigation\temp dump\PDF printouts\" folder to a new location and at the same time give them a new name.
wideeyedguy
Posts: 60
Joined: 11 Feb 2016, 11:49

Re: Replace Default String - Print Dialog

20 Aug 2018, 09:55

Thank you for the great input on this and I do see where I messed up on the variable references you mentioned.
Now, although the Msgbox pops up with the new %OutputVar% string to demonstrate the StringReplace result, the string in the Print dialog File name field is not being replaced. Nothing seems to be happening to replace the origFilename in the Print file naming field.
Is there another action that needs to happen to paste(?) or input the replace results into the data field?

Thanks in advance,
weg
I say this optimistically . . . One day I'll understand it.
But today is not that day!
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Replace Default String - Print Dialog

20 Aug 2018, 10:02

ControlSetText(this is already commented in the script guest provided), or send it Send % OutputVar, or paste it:

Code: Select all

clipboard := OutputVar
Send ^v
wideeyedguy
Posts: 60
Joined: 11 Feb 2016, 11:49

Re: Replace Default String - Print Dialog

20 Aug 2018, 10:21

So far, with enabling ControlSetText, it works great. Thank you for clarifying swagfag; sorry for not thinking through on why were the commented options listed.

What is the idea of showing Gosub, Start?
Why would I need that?

Thank you,
weg
I say this optimistically . . . One day I'll understand it.
But today is not that day!
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Replace Default String - Print Dialog

20 Aug 2018, 11:21

... create a script that will trigger when a Bullzip Print dialog displays
the gosub will, effectively, restart the script, calling WinWaitActive, which will hang until the window is focused.
wideeyedguy
Posts: 60
Joined: 11 Feb 2016, 11:49

Re: Replace Default String - Print Dialog

20 Aug 2018, 11:45

Since I want to visually confirm that the StringReplace has worked properly, the gosub will cause the script to start/run/restart until I press Enter to close the dialog, correct?

Usually, I stack some of my everyday type scripts in a AHKstartup.ahk file using #Include statements to avoid having multiple AHK icons in the Task Tray and they all work just fine. They sit in the background, basically, until activated by a key press or mouse click. Since I have yet to master AHK, I'm not sure what it is about this new script that seems to prevent it from activating when the Print dialog displays if the script is #Include'd in the startup script. I have to Run this new script outside of the startup to get it to work. And it is a one-time use without enabling the gosub.

I did do a bit of reading on the "shellhook new window" to see if that was the direction I need to go to make this script resident(?) and simply watching for the BZ print dialog to become active in order to trigger. However, several of the discussions went deep-dive too quickly for me to follow.

What am I not understanding about #Include'ing this script?

Thank you,
weg
I say this optimistically . . . One day I'll understand it.
But today is not that day!
Guest

Re: Replace Default String - Print Dialog

20 Aug 2018, 13:18

You can run it as a standalone script (it consumes very little memory) - if you want it to be part of your main script you can use SetTimer like so

Code: Select all

; add this at the top of your main script

origFilename=C:\Users\username\Documents\Litigation\temp dump\PDF printouts\public.hcad.org - Print.pdf
WinTitle=Bullzip PDF Printer 11.7 - Create File
defaultName = public.hcad.org - Print
newName = cad

SetTimer, CheckBullZip, 1000 ; check for window each second - 

; --------------------------------------------


;;; your main script code here


; --------------------------------------------
; add this at the bottom of your main script

CheckBullZip:
IfWinNotActive, %WinTitle%
	Return ; window is not active so return (wait a second and try again)
Sleep 100 ; just give it a bit of time
ControlGetText, origFilename, TextBoxU15, %WinTitle%
IfInString, origFilename, %defaultName%
	{
    StringReplace, OutputVar, origFilename, %defaultName%, %newName%
    ; ControlSetText, TextBoxU15, %OutputVar%, %WinTitle%
    MsgBox %OutputVar%
	}
Sleep 100 ; just give it a bit of time
; Send {enter} ; to save?
Return

; /bottom of your main script
wideeyedguy
Posts: 60
Joined: 11 Feb 2016, 11:49

Re: Replace Default String - Print Dialog

20 Aug 2018, 13:59

Thank you for your input. I'm giving it a try for a time to see how it behaves.
To be clear, the CheckBullZip script procedure will be activated every second, in order to watch for the dialog window, even though a user may go several hours between usage of Bullzip?

Thank you,
weg
I say this optimistically . . . One day I'll understand it.
But today is not that day!
Guest

Re: Replace Default String - Print Dialog

20 Aug 2018, 14:15

Yes that is correct, it will take minimal resources so there is nothing to worry about I think. The WinWaitActive from the first script just waits for the window to popup and therefore stall any other part of the script as you have noticed. So SetTimer is the easiest solution if you want to have it in your main script.
wideeyedguy
Posts: 60
Joined: 11 Feb 2016, 11:49

Re: Replace Default String - Print Dialog

20 Aug 2018, 15:11

Here's what I did with some back 'n forth testing and seems to work as needed and eliminates the need for the Timer setup. Let me know where I may be not thinking this through correctly. I pulled the variable declarations out of the auto-exec section and located within the body of the #IfWinActive script itself.

The only difference I was hoping to accomplish is not needing to do a Mouse, or such, trigger; simply activates when the Print dialog pops up. Was a bit uncomfortable with a Timer running a script in the background when the script might not be needed for several hours at a time, depending on circumstances of the workflow.
I have this script in its own AHK file called "BullZip NameFile CAD myversion.ahk" and #Include'ing from the startup file like:

Code: Select all

#Include %A_Scriptdir%\~ myNew AHKs\BullZip NameFile CAD myversion.ahk
Here is the script which is triggered with Ctrl-RButton when the Print dialog appears:

Code: Select all

#IfWinActive, Bullzip PDF Printer 11.7 - Create File 
^RButton::
origFilename=C:\Users\username\Documents\Litigation\temp dump\PDF printouts\public.hcad.org - Print.pdf
WinTitle=Bullzip PDF Printer 11.7 - Create File
defaultName = public.hcad.org - Print
newName = cad
Sleep 100 ; just give it a bit of time
ControlGetText, origFilename, TextBoxU15, %WinTitle%
IfInString, origFilename, %defaultName%
	{
    StringReplace, OutputVar, origFilename, %defaultName%, %newName%
    ; ControlSetText [, Control, NewText, WinTitle, WinText, ExcludeTitle, ExcludeText]
    ControlSetText, TextBoxU15, %OutputVar%, %WinTitle%
    ;MsgBox %OutputVar%
	}
Sleep 100 ; just give it a bit of time
Send {End}  ;Send cursor to end of string so I can see if the StringReplace worked correctly.
; Send {enter} ; to save?
;Gosub, Start
;Return
#IfWinActive
I guess we're done with this.
It isn't so much that I "want it to be" part of my main startup script; that is simply my neophyte way of avoiding numerous tray icons from scripts that have been initiated. I've still got some misunderstandings about the why-nots on this, but seems like we've beat on this one pretty well.

Thank you for all the good help,
weg
I say this optimistically . . . One day I'll understand it.
But today is not that day!

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], imstupidpleshelp, JoeWinograd, mikeyww, ositoMalvado, Rohwedder, usser and 179 guests