Need add-on features

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
jacky5293
Posts: 5
Joined: 26 Oct 2021, 12:10

Need add-on features

Post by jacky5293 » 26 Oct 2021, 12:18

Hi,

I'm new joiner here. Been research on AHK for my company requirement. Idea from https://www.autohotkey.com/board/topic/102329-scan-barcodeopen-file/. But i would like to add-on if PDF and JPG in same filename, can a GUI prompt to let me select PDF or JPG to open? Instead of both open automatically.

User avatar
mikeyww
Posts: 26600
Joined: 09 Sep 2014, 18:38

Re: Need add-on features

Post by mikeyww » 26 Oct 2021, 12:56

Inside the loop, you can use FileExist to see if a file with the other extension exists. If so, you can use InputBox or the GUI to ask for a selection.

There is no need for "IfExist" before running a file loop, because if the file does not exist, then the loop will not do anything anyway.

jacky5293
Posts: 5
Joined: 26 Oct 2021, 12:10

Re: Need add-on features

Post by jacky5293 » 02 Nov 2021, 09:26

Sorry, i am new to program the script. Can you show me the below script where should i input a script to make it prompt once is detected same filename in PDF and JPG, the GUI will prompt to let me select whether to open up PDF or JPG?

Code: Select all

SetTitleMatchMode 2
Gui, +alwaysontop            ;keeps the gui on top level
Gui, Add, Edit , w200 vpdfname,
Gui, Add, Button, gopenpdf default , OPEN
Gui, show ,, scantopdf            ;give name to gui for later reference

return

openpdf:
Gui,Submit,Nohide
If (pdfname = "" )
{
ControlFocus
return
}


IfExist, C:\Barcode Images\%pdfname%*.pdf
{
Loop, C:\Barcode Images\%pdfname%*.pdf
          Run %A_LoopFileFullPath%
      WinActivate
}

IfExist, C:\Barcode Images\%pdfname%.jpg    ;location and filename of pic
run, C:\Barcode Images\%pdfname%.jpg    ;location and filename of pic


IfExist, C:\Barcode Images\%pdfname%.pdf
run, C:\Barcode Images\%pdfname%.pdf

Guicontrol,,pdfname,            ;makes the edit box blank
sleep 4000                ;wait 4 seconds for pdf to load
WinActivate,scantopdf            ;then reactivate the gui window
return

Exit:
GuiClose:
ExitApp

User avatar
mikeyww
Posts: 26600
Joined: 09 Sep 2014, 18:38

Re: Need add-on features

Post by mikeyww » 02 Nov 2021, 18:59

Code: Select all

dir := StrReplace(A_WinDir, "Windows", "Barcode Images")
Gui, Pick:New, -SysMenu +Hwndpick
Gui, Font, s10
Gui, Add, Radio ,          vchoice , PDF
Gui, Add, Radio ,                  , JPG
Gui, Add, Button, x130 ym+8 Default, OK
Gui, Name:New
Gui, +AlwaysOnTop
Gui, Font, s10
Gui, Add, Edit , w200 vpdfname gCheck
Gui, Add, Button, Default, Open
Gosub, Check
Gosub, F3
F3::Gui, Name:Show, w250, Scan to PDF

Check:
Gui, Name:Submit, NoHide
GuiControl, % pdfname > "" ? "Enable" : "Disable", Open
Return

NameButtonOpen:
Gui, Name:Submit
Loop, %dir%\%pdfname%*.pdf
{ SplitPath, A_LoopFilePath,,,, fnBare
  If FileExist(jpg := dir "\" fnBare ".jpg") {
   Gui, Pick:Show, w350, %fnBare%
   SoundBeep, 1500
   WinWaitClose, ahk_id %pick%
  } Else choice := 1
  Run, % choice = 1 ? A_LoopFilePath : jpg
}
Return

PickButtonOK:
Gui, Pick:Submit
Return

jacky5293
Posts: 5
Joined: 26 Oct 2021, 12:10

Re: Need add-on features

Post by jacky5293 » 03 Nov 2021, 14:37

Hi Mikey,

Thanks for your assist, your script is working perfectly, but after i submit the pdf or jpg open, then i close it, the GUI "Scan to PDF" is disappear, only if i reload the script then its call out again. Can it reload the GUI again after the PDF or JPG shown?

User avatar
mikeyww
Posts: 26600
Joined: 09 Sep 2014, 18:38

Re: Need add-on features

Post by mikeyww » 03 Nov 2021, 15:29

Three ways to show your GUI:

1. Add the NoHide option to your Gui, Submit (keeps the GUI visible).
2. Press the F3 key manually (displays the GUI).
3. Add Gosub, F3 to the script, wherever you want the GUI to be shown.

jacky5293
Posts: 5
Joined: 26 Oct 2021, 12:10

Re: Need add-on features

Post by jacky5293 » 13 Nov 2021, 08:08

Hi Mikey,

It's worked great. But I still need 2 extra steps for my script, can you assist me on this?

Previous script:

Code: Select all

dir := StrReplace(A_WinDir, "Windows", "Barcode Images")
Gui, Pick:New, -SysMenu +Hwndpick
Gui, Font, s10
Gui, Add, Radio ,          vchoice , PDF
Gui, Add, Radio ,                  , JPG
Gui, Add, Button, x130 ym+8 Default, OK
Gui, Name:New
Gui, +AlwaysOnTop
Gui, Font, s10
Gui, Add, Edit , w200 vpdfname gCheck
Gui, Add, Button, Default, Open
Gosub, Check
Gosub, F3
F3::Gui, Name:Show, w250, Scan to PDF

Check:
Gui, Name:Submit, NoHide
GuiControl, % pdfname > "" ? "Enable" : "Disable", Open
Return

NameButtonOpen:
Gui, Name:Submit
Loop, %dir%\%pdfname%*.pdf
{ SplitPath, A_LoopFilePath,,,, fnBare
  If FileExist(jpg := dir "\" fnBare ".jpg") {
   Gui, Pick:Show, w350, %fnBare%
   SoundBeep, 1500
   WinWaitClose, ahk_id %pick%
  } Else choice := 1
  Run, % choice = 1 ? A_LoopFilePath : jpg
}
Return

PickButtonOK:
Gui, Pick:Submit
Return
Based on this above, i still need 2 more feature to make it perfect:

1) If the folder doesn't have pdf file, just a jpg alone, can it set to open straight as well. In current script, the jpg only able to open if the pdf is exist, then i can choose jpg to open it. If without PDF, the jpg won't open.

2) example: my current directory path is (Z:\drawings), and i have multiple folders in (Z:\drawings), e.g: Folder A, Folder B and Folder C. For this script, if i input a filename like (drawing123), can i let STReplace to search all my sub-folder (Folder A,B,C) which included this file (drawing123)? And, if 2 of the folder like (folder A and B) also found the same filename (drawing123) no matter is pdf or jpg, can it prompt a GUI of those files and show the directory path right besides it's filename?

Please correct the above script that you sent me that day with my requirement, appreciated.

User avatar
mikeyww
Posts: 26600
Joined: 09 Sep 2014, 18:38

Re: Need add-on features

Post by mikeyww » 13 Nov 2021, 09:30

I leave the newest change to you. You have the tools here. Loop, Files can also identify files recursively. Example

I noticed that my earlier script did not use Loop, Files, but I would recommend that. Best wishes.

jacky5293
Posts: 5
Joined: 26 Oct 2021, 12:10

Re: Need add-on features

Post by jacky5293 » 13 Nov 2021, 11:51

Hi Mikey,

File, loop is really new to me. i have read all the example and always receive error. Can you example it, so i can learn from it. Thanks. Sorry to trouble again. And, how i can make the GUI prompt if the file is exist but difference directory, so i can easily choose from the GUI and based on the correct path. Thanks.

User avatar
mikeyww
Posts: 26600
Joined: 09 Sep 2014, 18:38

Re: Need add-on features

Post by mikeyww » 13 Nov 2021, 13:14

Sorry I cannot write the new script for you. It is more involved than the original. Nonetheless, this thread contains examples of both GUI and Loop, Files. You are welcome to post any revisions, so that readers can examine them and provide you with feedback. My suggestion is to write a detailed, plain-language description of every step. This will be more detailed than the description that you have provided, because there will be comparisons of file names, decisions about multiple files in subdirectories, etc. You would then go line by line, converting your description into a new script.

Post Reply

Return to “Ask for Help (v1)”