How to execute a google/yahoo/duckduck search from command line, or Harbour?

General Help regarding HMG, Compilation, Linking, Samples

Moderator: Rathinagiri

User avatar
serge_girard
Posts: 3176
Joined: Sun Nov 25, 2012 2:44 pm
DBs Used: 1 MySQL - MariaDB
2 DBF
Location: Belgium
Contact:

Re: How to execute a google/yahoo/duckduck search from command line, or Harbour?

Post by serge_girard »

HGAut:

this works:

Code: Select all

cAPP := 'c:\Progra~1\Google\Chrome\Application\chrome.exe ' 
EXECUTE FILE "&cAPP "  PARAMETERS 'www.google.com/search?q=what+the'   
There's nothing you can do that can't be done...
PeteWG
Posts: 176
Joined: Sun Mar 21, 2010 5:45 pm

Re: How to execute a google/yahoo/duckduck search from command line, or Harbour?

Post by PeteWG »

Hello guys,

You might want to try the below code. It's portable and UI independent (can be used on both console and GUI appls).

Code: Select all

/*
gsearch.prg
compile : hbmk2 -w3 -es2 gsearch
*/
PROC Main( cSearchFor )
	LOCAL nResult := GoogleSearch( hb_DefaultValue( cSearchFor, "Harbour Minigui -facebook" ) )

	IF nResult == 0
		? "Search string has been sent to your browser!"
	ELSE
	   ? "Cannot execute search command! Error code:" + hb_ntos(nResult)
	ENDIF
		
	RETURN
	
FUNCTION GoogleSearch( cSearchFor )
	LOCAL cCmd, nResult
	
	IF ! HB_ISSTRING( cSearchFor ) .OR. Empty( cSearchFor )
		RETURN -1
	ENDIF
	cSearchFor := hb_StrReplace( cSearchFor, {'"'=>"", " "=>"+"}  )
	cCmd       := " /Q /C start https://www.google.com/search"+'"?client=gtx&q=' + cSearchFor + '"'
	nResult    := hb_run( hb_GetEnv( "ComSpec" ) + cCmd )
	
	RETURN nResult
regards,
Pete
User avatar
serge_girard
Posts: 3176
Joined: Sun Nov 25, 2012 2:44 pm
DBs Used: 1 MySQL - MariaDB
2 DBF
Location: Belgium
Contact:

Re: How to execute a google/yahoo/duckduck search from command line, or Harbour?

Post by serge_girard »

Thans Pete!
There's nothing you can do that can't be done...
hansmarc
Posts: 40
Joined: Thu Jun 23, 2016 5:38 am
Location: Belgium

Re: How to execute a google/yahoo/duckduck search from command line, or Harbour?

Post by hansmarc »

Hi Pete,

also a nice solution, no temporary html file has to be created but
in your sample a cmd command window appears a fraction of a second before opening the webbrowser.

Suggestion to prevent this with hb_run replace that line with :

ShellExecute(0, "open", "rundll32.exe","url.dll,FileProtocolHandler " + "https://www.google.com/search?q=" + cSearchfor, , 1)


Regards
Hans
HGAutomator
Posts: 195
Joined: Thu Jul 16, 2020 5:42 pm
DBs Used: DBF

Re: How to execute a google/yahoo/duckduck search from command line, or Harbour?

Post by HGAutomator »

Thanks Dragancesu,

That's a good reference, but there's no problem with the search string itself, at this point.

Now, the problem is with HB_Run. It isn't interpreting the quote around the program path correctly in conjunction with the parameters. Also, it waits until the called program is finished before continuing it's process.

ShellExecute/EXECUTE FILE is the obvious alternative. Unfortunately, it doesn't seem possible to mix text mode, with ShellExecute. The link works, but then nothing is displayed in the application. You can't even display the menu.

I'm going to play with putting most of the command line in a batch file, and calling the batch file from HB_Run, maybe with cmd.exe /c.


HGAutomator
Posts: 195
Joined: Thu Jul 16, 2020 5:42 pm
DBs Used: DBF

Re: How to execute a google/yahoo/duckduck search from command line, or Harbour?

Post by HGAutomator »

Peter,

Yes, this might work, thanks.
PeteWG wrote: Wed Aug 31, 2022 8:20 am Hello guys,

You might want to try the below code. It's portable and UI independent (can be used on both console and GUI appls).

Code: Select all

/*
gsearch.prg
compile : hbmk2 -w3 -es2 gsearch
*/
PROC Main( cSearchFor )
	LOCAL nResult := GoogleSearch( hb_DefaultValue( cSearchFor, "Harbour Minigui -facebook" ) )

	IF nResult == 0
		? "Search string has been sent to your browser!"
	ELSE
	   ? "Cannot execute search command! Error code:" + hb_ntos(nResult)
	ENDIF
		
	RETURN
	
FUNCTION GoogleSearch( cSearchFor )
	LOCAL cCmd, nResult
	
	IF ! HB_ISSTRING( cSearchFor ) .OR. Empty( cSearchFor )
		RETURN -1
	ENDIF
	cSearchFor := hb_StrReplace( cSearchFor, {'"'=>"", " "=>"+"}  )
	cCmd       := " /Q /C start https://www.google.com/search"+'"?client=gtx&q=' + cSearchFor + '"'
	nResult    := hb_run( hb_GetEnv( "ComSpec" ) + cCmd )
	
	RETURN nResult
regards,
Pete
HGAutomator
Posts: 195
Joined: Thu Jul 16, 2020 5:42 pm
DBs Used: DBF

Re: How to execute a google/yahoo/duckduck search from command line, or Harbour?

Post by HGAutomator »

Hans,

Thanks, but ShellExecute and EXECUTE FILE isn't working. It's possible to link it in, but then the text mode application itself doesn't display anything.
hansmarc wrote: Wed Aug 31, 2022 10:37 am Hi Pete,

also a nice solution, no temporary html file has to be created but
in your sample a cmd command window appears a fraction of a second before opening the webbrowser.

Suggestion to prevent this with hb_run replace that line with :

ShellExecute(0, "open", "rundll32.exe","url.dll,FileProtocolHandler " + "https://www.google.com/search?q=" + cSearchfor, , 1)


Regards
Hans
HGAutomator
Posts: 195
Joined: Thu Jul 16, 2020 5:42 pm
DBs Used: DBF

Re: How to execute a google/yahoo/duckduck search from command line, or Harbour?

Post by HGAutomator »

Serge,

Thanks, but EXECUTE FILE doesn't work with this text-mode app. The program runs, but there are no menus visible with which to select the menu item to run EXECUTE FILE or ShellExecute.

serge_girard wrote: Wed Aug 31, 2022 6:29 am HGAut:

this works:

Code: Select all

cAPP := 'c:\Progra~1\Google\Chrome\Application\chrome.exe ' 
EXECUTE FILE "&cAPP "  PARAMETERS 'www.google.com/search?q=what+the'   
HGAutomator
Posts: 195
Joined: Thu Jul 16, 2020 5:42 pm
DBs Used: DBF

Re: How to execute a google/yahoo/duckduck search from command line, or Harbour?

Post by HGAutomator »

Thanks, Pete. Everything's perfect.


https://drive.google.com/file/d/126Z-Kf ... sp=sharing

(If Google Drive says the video is still processing, ignore that message and just download it.)
User avatar
serge_girard
Posts: 3176
Joined: Sun Nov 25, 2012 2:44 pm
DBs Used: 1 MySQL - MariaDB
2 DBF
Location: Belgium
Contact:

Re: How to execute a google/yahoo/duckduck search from command line, or Harbour?

Post by serge_girard »

HGAut

What is text-mode app? You mean DOS screen? Then this works:

Code: Select all

FUNCTION Main
REQUEST HB_GT_WIN_DEFAULT
cAPP := 'c:\Progra~1\Google\Chrome\Application\chrome.exe ' 
EXECUTE FILE "&cAPP "  PARAMETERS 'www.google.com/search?q=what+the'   
return
There's nothing you can do that can't be done...
Post Reply