Applescript How to get the filename from open -a "xx.app" [file] --args "someargs" in the applescript

119 views Asked by At

I created a macos application launcher who is launching a apple script as the exec file. The problem is that I can not intercept the file of the open command in the script or do not know or find somewhere on the net. The Launcher self does work ok. To demonstrate I set it on my github :

CvrAppBash.app

when I run it out off terminal with open command for example: test.txt is just a simple text file.

open -a "CvrAppBash.app" "test.txt" --args "--some-arg-1" "--some-arg-2"

I can have all info needed in the launched apple script to work further with it. also the args. But how to get the file name here "test.txt" in the script ?

At this time my display dialog out gives:

The Macos Hfs path is:

'MACOSHSIERRA:Applications:MacPorts:CvrAppBash.app:'

The Posix path is:

'/Applications/MacPorts/CvrAppBash.app'

The Application name is:

'CvrAppBash'

The args are:

'--some-arg-1' '--some-arg-2'

What I wan't also to have is the filename here "test.txt" to work further with.

1

There are 1 answers

3
Christophe Van Reusel On

I did not had a lot of response. Now found a solution to the problem. Once You now it is very simple. It is only that you needed two sections in the apple script file. One section on run --- end run. Another on open -- end open. The applescript needs to be saved out of applescript editor using convert save as application. What also should work but does not is using osacompile -o Newapplication.app thescript.apple. At least it does not work on macos High Sierra 10.13.6. when using osacompile. Below the way how to do it in applescript.

use framework "Foundation"
use scripting additions

    on run
    -- The on run used when using open -a command without a given
    -- file to open.
    -- also used when clicking on the application out of finder.
    -- Set here the code needed to execute you're selected program.
    end run
    
    on open
    -- On open used when you are using open -a ".app" "a existing
    -- file.
    -- Also used when you select files in finder and open them
    -- with this application.
    -- Set the code You wan't further here.
    end open

To look more examples on how to use this way of application launcher see the mentions code examples below. I set them my github. Quit possible that depending on You're macos version You need to rebuild the application by just saving the script in site as application with apple script editor.

I set the application bunch self on my git hub :

The TestLaunchCvr.app on my git hub

I Added an example of practical use of this script to my git repo Gedit3 launcher

You can see the plain apple script code on : Gedit 3 launcher script

The macports installed gedit can now be used with commands :

open -a "Gedit3" "file1" "file2" --args "--new-window"

or

By clicking on the application self out of finder.

or

By selecting files in finder and then select Gedit3 out of applications list.