Real Software Forums

The forum for Real Studio and other Real Software products.
[ REAL Software Website | Board Index ]
It is currently Mon Sep 16, 2019 11:14 pm
xojo

All times are UTC - 5 hours




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: Opening a file with a non-default application
PostPosted: Sat Jun 04, 2011 12:33 pm 
Offline
User avatar

Joined: Fri Mar 03, 2006 5:57 pm
Posts: 61
Location: Elgin, Scotland
I have worked on a couple of projects which have required me to designate an application to open a file and in some circumstances, this is not the default application. Therefore, I came up with a small routine to do this (only Mac and Windows I'm afraid)

Sub OpenFileWith(DocumentPath As FolderItem, ApplicationPath As FolderItem)
Dim ShellString As String
Dim sh As Shell=New Shell
#if TargetMacOS
ShellString="open -a '" + ApplicationPath.ShellPath + "' '" + DocumentPath.ShellPath + "'"
#elseif TargetWin32
ShellString=ApplicationPath.AbsolutePath + " " + DocumentPath.AbsolutePath
#endif

sh.Execute(ShellString)
End Sub


On a Mac, it also appears that you can open the application by just using its name, so this can also be addressed by the overriding the subroutine with the following:

Sub OpenFileWith(DocumentPath As FolderItem, ApplicationName As String)
#if TargetMacOS
Dim ShellString As String
Dim sh As Shell=New Shell
ShellString="open -a '" + ApplicationName + "' '" + DocumentPath.ShellPath + "'"
sh.Execute(ShellString)
#endif
End Sub


Hope this helps anyone else who needs to do something like this.

_________________
Mark
[Real Studio 2011r4 Pro on OSX 10.8.2 targeting OSX]
MarkSpotCode - My trials and tribulations in the world of computer programming.


Top
 Profile  
Reply with quote  
 Post subject: Re: Opening a file with a non-default application
PostPosted: Mon Jun 06, 2011 10:07 pm 
Offline
User avatar

Joined: Fri Sep 30, 2005 11:48 am
Posts: 3554
Location: Lenexa, KS
That's very cool. Thank you for documenting it.

It would be an awesome addition to the source code repository on the Association of REALbasic Professionals (ARPB) website at http://www.arbp.org.

_________________
Bob K.

A blog about being a Real Studio/Xojo developer at http://www.bkeeneybriefs.com


Top
 Profile  
Reply with quote  
 Post subject: Re: Opening a file with a non-default application
PostPosted: Tue Jun 07, 2011 2:15 am 
Offline
User avatar

Joined: Fri Mar 03, 2006 5:57 pm
Posts: 61
Location: Elgin, Scotland
Bob Keeney wrote:
That's very cool. Thank you for documenting it.

It would be an awesome addition to the source code repository on the Association of REALbasic Professionals (ARPB) website at http://www.arbp.org.


Thanks Bob,

I'm not a member of ARBP, so please feel free to add it to the code repository.

_________________
Mark
[Real Studio 2011r4 Pro on OSX 10.8.2 targeting OSX]
MarkSpotCode - My trials and tribulations in the world of computer programming.


Top
 Profile  
Reply with quote  
 Post subject: Re: Opening a file with a non-default application
PostPosted: Tue Jun 07, 2011 2:20 am 
Offline
User avatar

Joined: Fri Mar 03, 2006 5:57 pm
Posts: 61
Location: Elgin, Scotland
I meant to say, if anyone wants to modify the routines to include the Linux processing, please do post the result.

_________________
Mark
[Real Studio 2011r4 Pro on OSX 10.8.2 targeting OSX]
MarkSpotCode - My trials and tribulations in the world of computer programming.


Top
 Profile  
Reply with quote  
 Post subject: Re: Opening a file with a non-default application
PostPosted: Tue Jun 07, 2011 8:42 am 
Offline
User avatar

Joined: Fri Sep 30, 2005 11:48 am
Posts: 3554
Location: Lenexa, KS
Quote:
I'm not a member of ARBP, so please feel free to add it to the code repository.


You don't need to be a paid member to add it to the source code repository. The only requirement is that you make an account (which is free).

_________________
Bob K.

A blog about being a Real Studio/Xojo developer at http://www.bkeeneybriefs.com


Top
 Profile  
Reply with quote  
 Post subject: Re: Opening a file with a non-default application
PostPosted: Tue Jun 21, 2011 3:51 am 
Offline
User avatar

Joined: Fri Mar 03, 2006 5:57 pm
Posts: 61
Location: Elgin, Scotland
Looking back at my original post, I just realised that the Mac version might not work properly. The shell path of a folder item in RealBasic is an 'escaped string'. This means that a file path such as /Users/markoxley/Desktop/Untitled Folder would end up as /Users/markoxley/Desktop/Untitled\ Folder (note the back slash before the space in 'Untitled Folder').
To fix this, we can implement a very simple function:

Sub UnEscape(value As String) As String
Dim s As String
Dim index as integer=0
while index<Len(value)
index=index+1
if mid(value,index,1)="\" then
if index<>Len(value) then
index=index+1
s=s+mid(value,index,1)
end if
else
s=s+mid(value,index,1)
end if
wend
Return s
End Sub


We would then replace the original subroutine with this:

Sub OpenFileWith(DocumentPath As FolderItem, ApplicationPath As FolderItem)
Dim ShellString As String
Dim sh As Shell=New Shell
#if TargetMacOS
ShellString="open -a '" + UnEscape(ApplicationPath.ShellPath) + "' '" + UnEscape(DocumentPath.ShellPath) + "'"
#elseif TargetWin32
ShellString=ApplicationPath.AbsolutePath + " " + DocumentPath.AbsolutePath
#endif

sh.Execute(ShellString)
End Sub

_________________
Mark
[Real Studio 2011r4 Pro on OSX 10.8.2 targeting OSX]
MarkSpotCode - My trials and tribulations in the world of computer programming.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 5 hours


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group