[Solved] Opening image in an external application

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
Igor
Posts: 6
Joined: Mon Mar 29, 2010 11:27 pm

[Solved] Opening image in an external application

Post by Igor »

I am trying to script opening an image stored in the file system with an external application (i.e. executable registered at OS for opening images). The path is stored in my Base database, and I am scripting the form which, among the other things, lets the user open full quality image she can preview as a reduced size thumbnail on the form. The database is about art history, so this is kind of important feature. I am scripting in Basic.
I first tried shell command, something like this:

Code: Select all

Shell("/Applications/Preview.app/Contents/MacOS/Preview", 1, "/Users/apple/Desktop/ooo/Slojevi/Slike/Pavao.JPG")
This thing, with respective paths, works fine in Windows and Linux, but in MacOS X it only opens the Preview application, not the passed image. Besides, as I don't know in advance who is going to use the base, and on what system, I should have to add the dialog asking user where is her picture viewer. What possible answer can I expect? In the icon on a desktop, on the Dock, or somewhere in the Start menu? The other approach was this:

Code: Select all

oSyShell = CreateUnoService("com.sun.star.system.SystemShellExecute")
oSyShell.execute("/Users/apple/Desktop/ooo/Slojevi/Slike/Pavao.JPG", "", 0)
This works in Windows. In Linux I am getting the SystemShellExecuteException with message "Resource temporary unavailable". As I am not a Linux expert, I have no clue what does this mean. In MacOS X the same exception with message "No such file or directory". To avoid mistyping the paths, I copied them from OS' file managers and pasted right into the code. I also tried "/Volumes/HDD/Users/apple..." with the same result. I checked the paths in respective shells with start, open, and xdg-open commands and these were all successful. So, does anyone know what goes wrong?
Last edited by Igor on Tue Jun 15, 2010 9:52 pm, edited 3 times in total.
OOo 3.2, Windows 2000, MacOS 10.4
rudolfo
Volunteer
Posts: 1488
Joined: Wed Mar 19, 2008 11:34 am
Location: Germany

Re: Opening image in an external application

Post by rudolfo »

Hm, I think I remember someone mentioning here in this forum that the built-in Shell() of the Basic interpreter, works more reliably when you pass the filename url-encoded: convertToURL("/OS/Path/.."). A search for convertToURL and shell should give your more details.
As far as I know that doesn't make a difference for the SystemShellExecute UNO service, because it passes its parameters directly to the underlying OS shell. For MS Windows this is the explorer which use the file associations in the registry. In a Linux you would probably need a Desktop Environment like KDE or Gnome. And for Mac OSX I have no clue. But I see that you have a capital extension .JPG. And UNIX derivates are case sensitive. They may not know anything about .JPG by default. What about using a different different file with a well known extension like .txt or .wav.
But actually I have to admit if you say that it works on the OS shells with start, open and xdg-open it doesn't look like the specific extension or file is the problem.
OpenOffice 3.1.1 (2.4.3 until October 2009) and LibreOffice 3.3.2 on Windows 2000, AOO 3.4.1 on Windows 7
There are several macro languages in OOo, but none of them is called Visual Basic or VB(A)! Please call it OOo Basic, Star Basic or simply Basic.
Igor
Posts: 6
Joined: Mon Mar 29, 2010 11:27 pm

Re: Opening image in an external application

Post by Igor »

Thank you rudolfo, you were really fast. Unfortunately none of your hints helps at the moment. Yes, I tried other files buried shallow in the file system, like /HDD/trythis.pdf, or /Users/apple/vanilla.jpg. Of course, files were there, and I checked it by ls-ing in Terminal, rather than getting info in Finder. I tried with lowercase .jpg too, though both, Finder and ls say it is uppercase, just the way Sony compact camera names it. As for URL conversion, shell function actually opens Preview and returns no error (it seems that it almost always returns 0), only with no image or window whatsoever. So I figure if it understands the first path, it should understand the second one as well.
Perhaps the third argument is not supposed to be a path, but proper parameter, but why on the earth does everything work perfectly in Windows (to my utmost surprise), and in Linux as well (I tested in Gnome environment)?
By the way, I searched suggested keywords, but most of the topics are about Windows that are posing no problems to me (Vista as well as 2K), and nothing at all about MacOS X that is main stopper at the moment.
OOo 3.2, Windows 2000, MacOS 10.4
hanya
Volunteer
Posts: 885
Joined: Fri Nov 23, 2007 9:27 am
Location: Japan

Re: Opening image in an external application

Post by hanya »

Have you tried with an URL for execute method like the following:

Code: Select all

oSyShell.execute(ConvertToURL("/Users/apple/Desktop/ooo/Slojevi/Slike/Pavao.JPG"), "", 0)
Internally, open command is called for Mac OS X in the execute method.
Please, edit this thread's initial post and add "[Solved]" to the subject line if your problem has been solved.
Apache OpenOffice 4-dev on Xubuntu 14.04
Igor
Posts: 6
Joined: Mon Mar 29, 2010 11:27 pm

Re: Opening image in an external application

Post by Igor »

I am citing api documentation project regarding the first parameter of the execute() method of the interface XSystemShellExecute (com.sun.star.system.XSystemShellExecute):
If the command specifies a path to an executable, etc, this has to be a system specific path.
There is an interesting post in this forum where the issue appears on the margin of the main thread of the discussion: http://user.services.openoffice.org/en/ ... nvertToURL. Having quoted all that, yessss hanya, bingo! You re right, convertingToURL() of my paths solves the case, on all three platforms equally. :bravo: You guys are great, thank you.
Still I wonder what's wrong about the shell() function. Although the use of the SystemShellExecute service fits my intentions perfectly, one can never tell when will I need the other solution.
By the way rudolfo, regarding the behavior of the start command in Windows cmd.exe and the way paths are passed to it, that you mentioned in this topic: http://user.services.openoffice.org/en/ ... RL#p123666 I found the approach with "short" filenames (you know, C:\Mydocu~1\...) quite satisfactory. True, you can imagine a folder filled with pictures whose filenames begin with exactly the same six or more characters, like DSC061510..., but this is not really common, and can probably be worked around.
OOo 3.2, Windows 2000, MacOS 10.4
Post Reply