0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Xojoからアプリの起動方法

有る程度以上の規模のコードを書いていると、ヘルパーやサポートアプリを
呼び出したりする場合があります。

私はいくつかのアプリでCLIのアプリを起動して利用していますし、
大規模なアプリになるほど、小型のヘルパーアプリを内包する
方法が便利な事が多いです。

CLIのコマンドであれば

Var theShell As Shell = New Shell
theShell.Execute( "/usr/sbin/xxx" )

の様に起動出来ますが、GUIを持つアプリはどうでしょうか

theShell.Execute( "/Application/TextEdit.app" )

では、起動出来ません。
何故でしょうか?

Var theExecFile As FolderItem = New FolderItem( "/Application/TextEdit.app" )

If theExecFile.Exists Then
  MessageBox( "Found" )
Else
  MessageBox( "Not Found " )
End If

ここではNot Foundとなり、アプリが存在しない事が判ります。

macOS付属のアプリは/Applicationフォルダには実体がありません。
本体は/System/Applicationフォルダにあります。

では

theShell.Execute( "/System/Applications/TextEdit.app" )

では、起動するでしょうか?
bash: /System/Applications/TextEdit.app: is a directory
とそいつはディレクトリだからと拒否されてしまいます。

macOSのアプリは特殊な形式のディレクトリ構造を持っており
見かけ上は.appなファイルですが、実際は階層構造を持つ
ディレクトリ(アプリケーションバンドル)です。

このアプリケーションバンドルを起動するには2つの方法があります。

theShell.Execute( "open" , "/System/Applications/TextEdit.app" )

とopenコマンドを経由する方法と
アプリケーションバンドル内の実行ファイルを直接起動する方法です。

theShell.Execute(  "/System/Applications/TextEdit.app/Contents/MacOS/TextEdit" )

複数のアプリを起動するような場合は以下のような Methodを
作っておくと便利でしょう。

Public Sub LaunchApp(inApp As FolderItem)
  //
  //  ApplicationのフルパスからAppを起動する
  //
  
  Var theExecName As String
  
  If Not inApp.Exists Then Return
  
  theExecName = inApp.Name.Trim( ".app", ".App")
  
  Var theApp As FolderItem = inApp.Child( "Contents" ).Child( "MacOS" ).Child( theExecName )
  
  gAsyncShell.Execute( theApp.ShellPath )

End Sub

Shellを終了すると起動したアプリも終了しますので、グローバル化するなどの
工夫が必要です。
ローカル変数などで処理するとスコープを抜けた瞬間に終了します。
通常はOpenコマンドの併用がお勧めですが、起動したプロセスIDの取得が一手間掛かります。

でわでわ Happy programming with Xojo

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?