Pascal code - newLisp

Q&A's, tips, howto's
Locked
reinier maliepaard
Posts: 6
Joined: Fri Jul 05, 2013 5:34 pm

Pascal code - newLisp

Post by 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). 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.

Code: Select all

 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 help\test2.ppx, invoke MCMsumatrapdf.exe; in other cases like View PDF show the message box *)
   if paramstr(1) = 'help\test2.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

Code: Select all

{$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 help\test2.ppx, invoke MCMsumatrapdf.exe; in other cases like View PDF show the message box *)
   if paramstr(1) = 'help\test2.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
Posts: 607
Joined: Fri Apr 08, 2005 7:13 pm
Location: Front Royal, Virginia

Re: Pascal code - newLisp

Post by rickyboy »

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)

Locked