Search code examples
javascriptautomator

Automator: use the passed file's filename in 'Run JavaScript' action


I am having problems extracting the title of the passed file within the 'Run JavaScript' action.

When I pass the file into the 'Run JavaScript' action, it seems that of the two parameters of the prepared function, run(), - input and parameters, neither contain the file name.

If I add a 'Set Value of Variable' action before the 'Run JavaScript' action, it appears that the variable gets assigned the actual file.

How do I grab the filename of the passed file within the Run JavaScript' action?


Solution

  • I was able to produce an Automator Workflow that will provide the name of a selected file as the input for a Run JavaScript Action. I do not have enough of a StackOverflow reputation to post a screenshot. Here is the list of Actions:

    Ask For Finder Items (I have it set to allow a single file)

    Run AppleScript Action:

    tell application "Finder" to set fileName to (item 1 of input)'s name
    
    return fileName
    

    Run JavaScript

    The result of the Run JavaScript is the name of the file. There may be a more efficient way to do this, but I am not familiar with it.

    Good luck,