ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
* check installed program using Lua
@ 2021-11-01 13:26 Pablo Rodriguez via ntg-context
  2021-11-01 14:10 ` Taco Hoekwater via ntg-context
  2021-11-01 16:13 ` Hans Hagen via ntg-context
  0 siblings, 2 replies; 5+ messages in thread
From: Pablo Rodriguez via ntg-context @ 2021-11-01 13:26 UTC (permalink / raw)
  To: mailing list for ConTeXt users; +Cc: Pablo Rodriguez

Dear list,

is there a way if a program is installed on the computer using Lua.

I have the following sample:

  \starttext
  \startluacode
  filename = tex.jobname .. ".pdf"
  os.exec("dir " .. filename)
  \stopluacode
  \stoptext

Is there a way to wrap os.exec() in the sample so that it only runs if
"dir" is available?

I know that os.name would be an option here, but not in my real world
document.

Many thanks for your help,

Pablo
--
http://www.ousia.tk
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: check installed program using Lua
  2021-11-01 13:26 check installed program using Lua Pablo Rodriguez via ntg-context
@ 2021-11-01 14:10 ` Taco Hoekwater via ntg-context
  2021-11-02 18:59   ` Pablo Rodriguez via ntg-context
  2021-11-01 16:13 ` Hans Hagen via ntg-context
  1 sibling, 1 reply; 5+ messages in thread
From: Taco Hoekwater via ntg-context @ 2021-11-01 14:10 UTC (permalink / raw)
  To: mailing list for ConTeXt users; +Cc: Taco Hoekwater, Pablo Rodriguez



> On 1 Nov 2021, at 14:26, Pablo Rodriguez via ntg-context <ntg-context@ntg.nl> wrote:
> 
> Dear list,
> 
> is there a way if a program is installed on the computer using Lua.
> 
> I have the following sample:
> 
>  \starttext
>  \startluacode
>  filename = tex.jobname .. ".pdf"
>  os.exec("dir " .. filename)
>  \stopluacode
>  \stoptext
> 
> Is there a way to wrap os.exec() in the sample so that it only runs if
> "dir" is available?

if os.which(‘dir’) then
  ...
end

But note that os.which() may be unreliable in various cases (like it will fail for shell/command interpreter builtins, in cron jobs, in special scripted environments, and may incorrectly succeed for disabled/forbidden commands), as it just runs through the PATH environment variable to check for executable file existence. 

Often times, it is better to just try to run the command to see if that produces satisfactory results.

Best wishes,
taco

— 
Taco Hoekwater              E: taco@bittext.nl
genderfluid (all pronouns)



___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: check installed program using Lua
  2021-11-01 13:26 check installed program using Lua Pablo Rodriguez via ntg-context
  2021-11-01 14:10 ` Taco Hoekwater via ntg-context
@ 2021-11-01 16:13 ` Hans Hagen via ntg-context
  2021-11-02 19:01   ` Pablo Rodriguez via ntg-context
  1 sibling, 1 reply; 5+ messages in thread
From: Hans Hagen via ntg-context @ 2021-11-01 16:13 UTC (permalink / raw)
  To: mailing list for ConTeXt users; +Cc: Hans Hagen, Pablo Rodriguez

On 11/1/2021 2:26 PM, Pablo Rodriguez via ntg-context wrote:
> Dear list,
> 
> is there a way if a program is installed on the computer using Lua.
> 
> I have the following sample:
> 
>    \starttext
>    \startluacode
>    filename = tex.jobname .. ".pdf"
>    os.exec("dir " .. filename)
>    \stopluacode
>    \stoptext
> 
> Is there a way to wrap os.exec() in the sample so that it only runs if
> "dir" is available?
> 
> I know that os.name would be an option here, but not in my real world
> document.

\starttext
   \startluacode
     if lfs.isfile(tex.jobname .. ".pdf") then
         context("YES")
     end
     if lfs.isfile(file.addsuffix(environment.outputfilename,"pdf")) then
         context("YES")
     end
     if #dir.glob(file.addsuffix(environment.outputfilename,"pdf")) > 0 then
         context("YES")
     end
   \stopluacode
\stoptext

-----------------------------------------------------------------
                                           Hans Hagen | PRAGMA ADE
               Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
        tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl
-----------------------------------------------------------------
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: check installed program using Lua
  2021-11-01 14:10 ` Taco Hoekwater via ntg-context
@ 2021-11-02 18:59   ` Pablo Rodriguez via ntg-context
  0 siblings, 0 replies; 5+ messages in thread
From: Pablo Rodriguez via ntg-context @ 2021-11-02 18:59 UTC (permalink / raw)
  To: ntg-context; +Cc: Pablo Rodriguez

On 11/1/21 3:10 PM, Taco Hoekwater via ntg-context wrote:
>> [...]
>> Is there a way to wrap os.exec() in the sample so that it only runs if
>> "dir" is available?
>
> if os.which(‘dir’) then
>   ...
> end
>
> But note that os.which() may be unreliable in various cases (like it
> will fail for shell/command interpreter builtins, in cron jobs, in
> special scripted environments, and may incorrectly succeed for
> disabled/forbidden commands), as it just runs through the PATH
> environment variable to check for executable file existence.
Many thanks for your reply, Taco.

This is exactly what I needed.

> Often times, it is better to just try to run the command to see if
> that produces satisfactory results.
It makes sense, but I cannot do that on foreign computers.

Many thanks for your help again,

Pablo

--
http://www.ousia.tk
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: check installed program using Lua
  2021-11-01 16:13 ` Hans Hagen via ntg-context
@ 2021-11-02 19:01   ` Pablo Rodriguez via ntg-context
  0 siblings, 0 replies; 5+ messages in thread
From: Pablo Rodriguez via ntg-context @ 2021-11-02 19:01 UTC (permalink / raw)
  To: Hans Hagen via ntg-context; +Cc: Pablo Rodriguez

On 11/1/21 5:13 PM, Hans Hagen via ntg-context wrote:
> On 11/1/2021 2:26 PM, Pablo Rodriguez via ntg-context wrote:
>> [...]
>> Is there a way to wrap os.exec() in the sample so that it only runs if
>> "dir" is available?
>>
>> I know that os.name would be an option here, but not in my real world
>> document.
>
> \starttext
>    \startluacode
>      if lfs.isfile(tex.jobname .. ".pdf") then
>          context("YES")
>      end
>      if lfs.isfile(file.addsuffix(environment.outputfilename,"pdf")) then
>          context("YES")
>      end
>      if #dir.glob(file.addsuffix(environment.outputfilename,"pdf")) > 0 then
>          context("YES")
>      end
>    \stopluacode
> \stoptext

Many thanks for your reply, Hans.

I can see that I explained my question poorly, but your code fits me in
other case.

Many thanks for your help,

Pablo
--
http://www.ousia.tk
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2021-11-02 19:01 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-11-01 13:26 check installed program using Lua Pablo Rodriguez via ntg-context
2021-11-01 14:10 ` Taco Hoekwater via ntg-context
2021-11-02 18:59   ` Pablo Rodriguez via ntg-context
2021-11-01 16:13 ` Hans Hagen via ntg-context
2021-11-02 19:01   ` Pablo Rodriguez via ntg-context

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).