Pascal code - newLisp

Started by reinier maliepaard, June 14, 2015, 10:12:59 AM

Previous topic - Next topic

reinier maliepaard

Hello,

 I've made a program in Object Pascal which works ok. I was wondering if the Pascal code below could be done with newLisp. That would for me a more convenient solution for my project, that already uses newlisp.dll (see http://www.mcmusiceditor.com">http://www.mcmusiceditor.com). Here the central part of the Pascal code that does the job (the complete code is at the end of the email). The idea: if there is no default PDF viewer, invoke MCMsumatrapdf.exe only if the loaded file is test2.pdf; otherwise show a message. Of course, if there is a default PDF viewer, then the file will be shown.



SetCurrentDirectory(pchar(ExtractFilePath(paramstr(0))));
 result:=ShellExecute(0, 'open', pchar(paramstr(1)), nil, nil, SW_SHOWNORMAL);

 if result = SE_ERR_NOASSOC
 then
   begin
   (* if no duplicates as in case of calling the help file helptest2.ppx, invoke MCMsumatrapdf.exe; in other cases like View PDF show the message box *)
   if paramstr(1) = 'helptest2.pdf'
   then
    begin
     cmd:='MCMsumatrapdf.exe ';
     cmd:=cmd+'"'+paramstr(1)+'"';
     WinExec(pchar(cmd),SW_SHOWNORMAL);
    end
   else
    MessageBox(0,'There is no application associated with a PDF file. ...','No PDF file association!',MB_OK or MB_ICONINFORMATION)
   end
 except
 end;
end.


Thanks in advance.



Regards,

Reinier



{$H+}
{$R manifest.res}

program pdfview;
uses
 {$IFNDEF FPC}
  ShellApi,
 {$ENDIF}
  Windows;

{$IFNDEF FPC}
const
 AllowDirectorySeparators : set of char = ['','/'];
 AllowDriveSeparators : set of char = [':'];
{$ENDIF}

var
 cmd:string;
 result:thandle;

function ExtractFilePath(const FileName: string): string;
 var
   i : longint;
   EndSep : Set of Char;
 begin
   i := Length(FileName);
   EndSep:=AllowDirectorySeparators+AllowDriveSeparators;
   while (i > 0) and not (FileName[i] in EndSep) do
     Dec(i);
   If I>0 then
     Result := Copy(FileName, 1, i)
   else
     Result:='';
end;

begin
 try

 SetCurrentDirectory(pchar(ExtractFilePath(paramstr(0))));
 result:=ShellExecute(0, 'open', pchar(paramstr(1)), nil, nil, SW_SHOWNORMAL);

 if result = SE_ERR_NOASSOC
 then
   begin
   (* if no duplicates as in case of calling the help file helptest2.ppx, invoke MCMsumatrapdf.exe; in other cases like View PDF show the message box *)
   if paramstr(1) = 'helptest2.pxx'
   then
    begin
     cmd:='MCMsumatrapdf.exe ';
     cmd:=cmd+'"'+paramstr(1)+'"';
     WinExec(pchar(cmd),SW_SHOWNORMAL);
    end
   else
    MessageBox(0,'There is no application associated with a PDF file. Save your composition as PDF via MCMsumatrapdf or associate PDF files with a PDF viewer like Adobe Acrobat Reader or Foxit Reader.','No PDF file association!',MB_OK or MB_ICONINFORMATION)
   end
 else if (result = ERROR_FILE_NOT_FOUND) or (result =  ERROR_PATH_NOT_FOUND)
 then
  MessageBox(0,'The pdf file cannot be found!','Error',MB_OK or MB_ICONERROR)
 else if result < 33
 then
  MessageBox(0,'Cannot display the pdf file!','Error',MB_OK or MB_ICONERROR);

 except
 end;

end.

rickyboy

#1
Conceptually, yes, you can do this in newLISP.  There is, though, the question of how much of the current functionality you want to remain in a newLISP version.  For instance, I don't know what ShellExecute (or open, for that matter) does (in Windoze(?)), but if you need that functionality, and assuming that ShellExecute (and friends, e.g. MessageBox) are some kind of Windoze ecosystem call, then I guess you could punt to ffi.  Things like SetCurrentDirectory, ExtractFilePath and WinExec might be accomplished from newLISP primitives iirc.  But, I'm not a Win guy so I defer to others.  (I think I do know, however, that "shell" means something entirely different in Win versus Unixen. :)



But I also wonder that, if you (presumably) already have Sumatra PDF, why don't you just use that to open *all* PDFs?



In any case, good hacking to ya!
(λx. x x) (λx. x x)