Sunday 5 June 2011

Tip: Opening a file with a non-default application

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 in RealBasic 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.

No comments:

Post a Comment