Interaction - how to run a program with arguments?

Discuss the presentation application
Post Reply
User avatar
martin_g
Posts: 30
Joined: Tue Oct 21, 2014 8:48 pm
Location: Germany

Interaction - how to run a program with arguments?

Post by martin_g »

Does anybody know how to set up an interaction that starts an external program with command line arguments? (e. g. my_prog.exe my_file.txt)
Everything in the input field "program" seems to be processed as the program name itself.
OpenOffice 4.1 on Windows 8
User avatar
Villeroy
Volunteer
Posts: 31345
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Interaction - how to run a program with arguments?

Post by Villeroy »

menu:Insert>Hyperlink...
When the hyperlink points to a text file (*.txt), it will be loaded into the default program for text files.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
User avatar
martin_g
Posts: 30
Joined: Tue Oct 21, 2014 8:48 pm
Location: Germany

Re: Interaction - how to run a program with arguments?

Post by martin_g »

Villeroy wrote:When the hyperlink points to a text file (*.txt), it will be loaded into the default program for text files.
The file "my_file.txt" was just an example to explain what I mean. But actually I want to start a certain program that needs a path name as command line argument -- and I definitely don't want to set a "default program" for path names!
My current solution is that I created a separate batch file (my_prog.bat) that starts the EXE file with the command line argument. But since I have several "interactions" in my presetation which all shall run the same program but with different arguments I had to create one separate batch file for each call of the external program. So I hope that there is a more simple way.
OpenOffice 4.1 on Windows 8
User avatar
Villeroy
Volunteer
Posts: 31345
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Interaction - how to run a program with arguments?

Post by Villeroy »

OpenOffice (but not LibreOffice) can trigger a Basic routine by means of a hyperlink. The routine gets the full URL passed as argument.
Protocol is vnd.sun.star.script:
Location is Library.Module.RoutineName
Mandatory arguments are ?language=Basic&location=document (for code that is embedded in the calling document)
Optional arguments can be any pairs &name=value
So your hyperlink URL may be like
vnd.sun.star.script:Standard.Module1.ExecuteProg?language=Basic&location=document&prog=my_prog.bat&arg1=foo

The following (very simplistic) Basic code extracts a named value from an URL:

Code: Select all

REM VERY simple string function, extracting values from pairs of name=value out of an URL
REM NO URL-escaping in Basic! Only alphanumerics and a few special chars. Good enough for many purposes.
REM returns "" if no value was found for name.
Function getArgumentFromURL(sURL$,sName$) as String
on error goto exitErr:
Dim iStart%, i%, l%, sArgs$, a()
	iStart = instr(sURL, "?")
	l = len(sName)
	if (iStart = 0) or (l = 0) then exit function
	' sArgs behind "?":
	sArgs = mid(sURL, iStart +1)
	a() = split(sArgs, "&")
	for i = 0 to uBound(a())
		' not case sensitive:
		if instr(1, a(i), sName &"=", 1) = 1 then
			getArgumentFromURL = mid(a(i), l +2)
			exit for
		endif
	next
exitErr:
' return ""
End Function

Code: Select all

Sub ExecuteProg(sURL)
  prog = getArgumentFromURL(sURL, "prog", 0)
  arg = getArgumentFromURL(sURL, "arg1", 0)
Shell(prog, 0, arg)
End Sub
Look up the "Shell function" in the Basic section of the F1-help.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
User avatar
martin_g
Posts: 30
Joined: Tue Oct 21, 2014 8:48 pm
Location: Germany

Re: Interaction - how to run a program with arguments?

Post by martin_g »

Villeroy wrote:OpenOffice (but not LibreOffice) can trigger a Basic routine by means of a hyperlink. The routine gets the full URL passed as argument.
Protocol is vnd.sun.star.script:
Location is Library.Module.RoutineName
I have to admit that I have no idea what you mean!
Please find attached a screenshot of the dialog box for a hyperlink (the German version -- sorry). So what shall I choose here and where shall I type in the "location" or "protocol" you mentioned???
And where shall I place the code lines you posted?
Villeroy wrote:Look up the "Shell function" in the Basic section of the F1-help.
When I searched for "Shell" in the Open Office help I got only one match: a topic about the Open Office command line parameters.
Attachments
Screenshot of the Hyperlink dialog box
Screenshot of the Hyperlink dialog box
OpenOffice 4.1 on Windows 8
User avatar
Villeroy
Volunteer
Posts: 31345
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Interaction - how to run a program with arguments?

Post by Villeroy »

Choose the "OpenOffice Basic" section in the top-left corner of the online help. Then you will find the Shell function.
I mean something very similar to the attached spreadsheet where each hyperlink calls the same code with different arguments s,r,c depending on the sheet, row and column of the respective cell.

Anyhow, I thing there is something slightly easier if you want to use hyperlink push buttons.
The macro to be called should look like this:

Code: Select all

Sub execButton(e)
arg = e.Source.Model.Tag
Shell("my_prog.bat", 0, arg)
End Sub
Get toolbar or "Form controls", turn on edit mode (toolbar button #2), draw a button and get its control properties (toolbar button #3).
Scroll down to property "Additional info" and paste your argument there.
On the events tab, choose the macro to execute.
The additional argument will be read from the calling button's additional info (e.Source.Model.Tag)
Attachments
HyperlinkButtons.ods
(11.8 KiB) Downloaded 680 times
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
User avatar
martin_g
Posts: 30
Joined: Tue Oct 21, 2014 8:48 pm
Location: Germany

Re: Interaction - how to run a program with arguments?

Post by martin_g »

Thanks for your hints and the code samples!

However, it seems to me that the BASIC command shell() does not really start a shell ("cmd.exe" on Windows) because the shell built-in commands (like "pause" or "cd") are not supported and no console window appears -- which makes the shell() command useless for console applications. :(
I've tried "0" and "1" for the the second parameter of shell() (i. e. the "Windowstyle") but unfortunately this had no effect when running console applications.

The second problem is that the button does not work in presentation mode! When I started the screen presentation mode and tried to click the button Impress just switched to the next slide!
OpenOffice 4.1 on Windows 8
User avatar
Villeroy
Volunteer
Posts: 31345
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Interaction - how to run a program with arguments?

Post by Villeroy »

I see the problem with terminals and but I can't find a solution.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
User avatar
RoryOF
Moderator
Posts: 35064
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: Interaction - how to run a program with arguments?

Post by RoryOF »

I normally run the system video player by using a hyperlink to the video player with the desired video file as parameter and any other settings (e.g., full screen mode) also as parameters.
Apache OpenOffice 4.1.15 on Xubuntu 22.04.5 LTS
User avatar
martin_g
Posts: 30
Joined: Tue Oct 21, 2014 8:48 pm
Location: Germany

Re: Interaction - how to run a program with arguments?

Post by martin_g »

RoryOF wrote:I normally run the system video player by using a hyperlink to the video player
The hyperlink dialog supports "Web", "FTP" and "Telnet" (see screenshot above). So what type of protocol do you use for running a local video player program???
OpenOffice 4.1 on Windows 8
User avatar
RoryOF
Moderator
Posts: 35064
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: Interaction - how to run a program with arguments?

Post by RoryOF »

Move down in the side panel and select "Document".
Apache OpenOffice 4.1.15 on Xubuntu 22.04.5 LTS
User avatar
Villeroy
Volunteer
Posts: 31345
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Interaction - how to run a program with arguments?

Post by Villeroy »

The following macro stored in Impress-Document.Standard.Module1 works for me in presentation mode:

Code: Select all

Sub vlc_Bitter_Lake()
shell "vlc /home/andreas/Videos/Bitter_Lake-Adam_Curtis.mp4"
End Sub
vlc is a video player (also availlable for WIndows)
/home/andreas/Videos/Bitter_Lake-Adam_Curtis.mp4 is the full path to a Video. I removed all special characters and spaces from the path-name.

The calling text hyperlink is:
vnd.sun.star.script:Standard.Module1.vlc_Bitter_Lake?language=Basic&location=document

I can call one program with one argument and only one space between the program name and the argument. No spaces in the argument.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
User avatar
martin_g
Posts: 30
Joined: Tue Oct 21, 2014 8:48 pm
Location: Germany

Re: Interaction - how to run a program with arguments?

Post by martin_g »

RoryOF wrote:I normally run the system video player by using a hyperlink to the video player with the desired video file as parameter and any other settings (e.g., full screen mode) also as parameters.
Indeed, using a hyperlink to a "document" which actually is a program works. But I still was not able to add a command line argument. Please tell me (or post a screenshot of the hyperlink dialog panel) where you put the arguments.
If I add a command line argument behind the program name (here: "cd_run.exe") Impress takes the entire field as a program name (including the argument):
Attachments
screenshot hyperlink dialog panel
screenshot hyperlink dialog panel
OpenOffice 4.1 on Windows 8
User avatar
RoryOF
Moderator
Posts: 35064
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: Interaction - how to run a program with arguments?

Post by RoryOF »

Will try to do this tomorrow - other matters to do now.
Apache OpenOffice 4.1.15 on Xubuntu 22.04.5 LTS
User avatar
RoryOF
Moderator
Posts: 35064
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: Interaction - how to run a program with arguments?

Post by RoryOF »

I have spent half the morning fiddling with this and cannot now get it to work as it used. Perhaps there has been some change between OO 2.4 and OO 4 which affects this. I cannot find my copy of the original note and the site that contained it is now gone. For information, the URL for the note was
http://cdriga.kfacts.com/open-source-world/openofficeorg/remote-images-in-your-openofficeorg-impress-presentations/2007/10/31/

However, in the hope that it may be of assistance, the following method works: I set my videoplayer to use the defaults I require (usually full screen). Now, entering a video filename in the Hyperlink path for a Document will cause the system videoplayer to open in its default setting and play the video. Note that if there are spaces in the path names you may have to use double quotes surrounding the full path. I have also found that changing the default program for a video file may require a system restart to take reliably - it may work once, but not reliably until after a restart.


If you are intending to use this for a presentation, I urge you to try it privately, with repeated system restarts (from power down) to be certain it is working reliably. There is nothing worse than having to try and debug in front of an audience.

If I find my printout of the original file I'll post it to this thread, but don't hold your breath!
Apache OpenOffice 4.1.15 on Xubuntu 22.04.5 LTS
User avatar
RoryOF
Moderator
Posts: 35064
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: Interaction - how to run a program with arguments?

Post by RoryOF »

Another way to do this, albeit clunkier, is to make a batch file to run the program with required arguments, and hyperlink to the batch file.
Apache OpenOffice 4.1.15 on Xubuntu 22.04.5 LTS
User avatar
martin_g
Posts: 30
Joined: Tue Oct 21, 2014 8:48 pm
Location: Germany

Re: Interaction - how to run a program with arguments?

Post by martin_g »

Thank you very much for your effort! (Sometimes I have the same problem that I remember an issue that I already solved years ago but because of a system update or the replacement of my computer I cannot find the solution again.)
RoryOF wrote:Another way to do this, albeit clunkier, is to make a batch file to run the program with required arguments, and hyperlink to the batch file.
This is exactly my current solution as I worte in my second post on this topic. However, this means a separate batch file for each different set of parameters. To simplify this was the reason of posting this topic. ;)
Using the default program for a certain kind of file is not a suitable solution for me, as wrote in my second post, too.
Nevertheless, thank you for your support.
OpenOffice 4.1 on Windows 8
Post Reply