Search code examples
javascriptapplescript

How to pass arguments to javascript file using applescript


I’m trying to send arguments to a JavaScript file using AppleScript.

But it requires application support to run the “do javascript” condition.

I have found a code online but I don’t know how to use the code.

Please help me through this situation

Here is the online code:

do shell script “/usr/bin/osascript -l JavaScript /path/to/your.js arg1 arg2 arg3”

my javascript file path: ~/desktop/A/testingFile.jsx

arguments {“A”, “B”, “C”} (for testing purposes)

thank you


Solution

  • JavaScript

    Here is a sample JXA (JavaScript for Automation) script file that will take three arguments, display them on the command line, and then return them reversed.

    //pass arguments to a JXA file
    //return them reversed
    
    function run(argv) {
        if (argv.length < 3) {
            console.log("Three arguments are required.");
            return;
        }
    
        console.log(argv.length, "arguments provided.");
    
        argumentOne = argv[0];
        argumentTwo = argv[1];
        argumentThree = argv[2];
    
        console.log("First argument is", argumentOne);
        console.log("Second argument is", argumentTwo);
        console.log("Third argument is", argumentThree);
    
        return argv.reverse();
    }
    

    You can run this script on the command line (i.e., in Terminal) using something like

    osascript -l JavaScript ~/Desktop/A/testingFile.jsx One Two Four
    

    The result, on the command line, will be:

    3 arguments provided.
    First argument is One
    Second argument is Two
    Third argument is Four
    Four, Two, One

    AppleScript

    To use this from an AppleScript, you could open Script Editor and use code that looks like this:

    set scriptResults to do shell script "osascript -l JavaScript ~/Desktop/A/testingFile.jsx One Two Four"
    display dialog scriptResults
    

    The dialog (and the Result box if you have that opened) will display “One Two Four” reversed, as “Four, Two, One.”

    AppleScript calling a JXA JavaScript and displaying the result.

    Granting Disk Access

    When running this in Script Editor, you will need to provide Script Editor with “Full Disk Access” or access to specific “Files and Folders”. To see the list of apps granted access, go to your Security & Privacy preferences and choose the Privacy pane. In the list of access types, scroll to (for example) Files and Folders and choose it. A list of apps provided with access, and the files and folders they have access to, will appear.

    You can click the lock to add apps. If Script Editor is not in that list, for example, you can “click the lock to make changes” and then drag Script Editor into that list.

    Files and Folders access list.

    In this example, Script Editor has access to removable volumes, my Documents folder, and my Desktop folder.

    If you save the script as an application, or if it is run under the auspices of another application (such as via the Scripts Menu), that application will need to be given the appropriate access. It is usually easier to grant an AppleScript app Full Disk Access rather than Files and Folders, but it may depend on the OS version you’re using.