[REL] RunExternalProgramTest

If you made a script you can offer it to the others here, or ask help to improve it. You can also report here bugs & problems with existing scripts.
Post Reply
morfius
Posts: 26
Joined: 2011-11-10 13:01:06

[REL] RunExternalProgramTest

Post by morfius »

This is just a sample. Practical implementation about the idea of this post

The VBS Script available here (copy to AMC Script dir)

The script RunExternalProgramTest.ifs is available here (copy to AMC Script dir)

Run AMC and add a film. Select RunExternalProgramTest.ifs as script.

The Main Program, with comments

Code: Select all

program RunExternalProgramTest ;

const
  sRunExternalProgram = 'RunExternalProgram importer';

var
	gsBuscado, gsYear: string;
	gbRes: Boolean;
//------------------------------------------------------------------------------------
function findMovieFA(m: string; idFA: string; buscado: string; year: string; precision: string; mostrarAvisos: Boolean): Boolean;
var

command, parameters: string;
n: Integer;
Count: Integer;//debug
fIntercambio, fInicio: string;
aList: TStringList;
bNoFirstTime: Boolean;

begin
	fIntercambio := dirScripts + '_temp.del';
	fInicio := dirScripts + '_inicio.del';
	command := dirScripts + 'findMovieFAIMDb.vbs';
	if (m <> '') then//if m is empty, check DLL version
		parameters := '"' + m + '" ' + year;
	DeleteFile(fIntercambio);
	DeleteFile(fInicio);
	//check for external program
	if (FileExists(command) = False) then
	begin
		ShowError(sRunExternalProgram + ' Error. File not found: ' + command);
		exit;
	end;

	//Check External program Version
  bNoFirstTime := True;//TODO Set a Option or Parameter
	if (bNoFirstTime = False) then
	begin
		bNoFirstTime := True;
		//Do something, like run without parameteres for get Version
		Launch(command, '');
	end;

	Launch(command, parameters);
	// wait a bit
	Sleep(300);
	//Loop, see break comments
	while (True) do
	begin
		if (FileExists(fInicio) = False) then
			exit;//fInicio Dont exist
			
 		//OK continue
		if FileExists(fIntercambio) then
		begin
			DeleteFile(fInicio);
			aList := TStringList.Create;
			aList.LoadFromFile(fIntercambio);

			n := aList.Count;
			if aList.Count > 0 then
			begin
				parameters := aList.Text;
				for n := 0 to (aList.Count - 1) do
				begin
					parameters := aList.GetString(n);
					Count := Length(parameters);
				end;
			end;

			//check number of returned lines
			if aList.Count = 2 then
			begin
				//show controled error message from external program
				ShowError(parameters);
				//1.- External program not instaled
				//2.- No results for serarch string
				//3.- Others
				break;//while
			end;

			if aList.Count = 1 then
			//Versión
			begin
				ShowInformation('DLL Version: '+ parameters);
				break;//while
			end;

			if aList.Count <> 45 then
			begin
				ShowError('Error: ' + aList.Text);
				//Error por ¿otras causas?
				//no se pudo escribir el archivo (se ejecuta desde un medio de solo lectura)
				break;//while
			end;
			
			//All OK here we go
			//now you have aList array with results
			//Call your Main routine here
			//YourMainRoutine(aList);
			//To Work!!
			//To Work!!
			//To Work!!
				ShowInformation(aList.Text);

			//free memory
			aList.Free;
			//Finally del exchange file
			DeleteFile(fIntercambio);
			result := True;
			break;//while
		end;//FileExists(fIntercambio)
		if (result = False) then//Loop waiting external program
    		Sleep(1000);
		//Yo can add a Counter Count := Count + 1;
	end;//while
end;//function

//------------------------------------------------------------------------------------

begin

	gsBuscado := GetField(fieldURL);
	if Length(gsBuscado) > 0 then
	begin
		findMovieFA(gsBuscado, '', '', '', '', False);
		exit;
	end;
	gsBuscado := GetField(fieldTranslatedTitle);
	if gsBuscado = '' then
	begin
		gsBuscado := GetField(fieldOriginalTitle);
		if (GetOption('AutoSelect') = 0) or (gsBuscado = '') then
		begin
			gbRes := Input(sRunExternalProgram, 'Pelicula:', gsBuscado);
			if (gbRes = False) then
				exit;
		end;
	end;
	gsYear := GetField(fieldYear);
	findMovieFA(gsBuscado, '', '', gsYear, '', False);
end.
One more complex implementation is available here

All files are open source and are hosted in SourceForge
That's all!
Post Reply