2

In an automator app, there is a Copy to Clipboard and a Run Apple Script. If I assign this app to a certain file extension, by double clicking in Finder, the Copy to Clipboard function gets the filename properly, but how do I use file name and/or file folder in the apple script section below that? Both $1 and $@ seem to be empty.

Update:

Here is a new formulation of my question: I just need the name of the file inside the apple script. This should be passed to the block by Finder, but how can I access it?

enter image description here

In the above picture, I need to replace XXXXXX with a variable that let's me use the passed on filename inside the AppleScript.

8
  • The do script command doesn’t return the results of the script (that is what the Terminal does), but you would need to use the input to the Run AppleScript action. What exactly are you trying to do?
    – red_menace
    Jul 22, 2022 at 23:54
  • I like to replace echo with cd, that is change directory to the folder in which the file is located. Then I open that file in its own folder using another script. For that I need filename and folder. I am sure this variables are passed down as Application Receives files and folders indicates, and as Copy To Clipboard works, but I don't know how to catch the same variables in apple script.
    – xaratustra
    Jul 23, 2022 at 8:32
  • Here is an example on StackOverflow on how to get the POSIX path of the selected file in AppleScript. Jul 23, 2022 at 13:18
  • 1
    If the other script is also a shell script, there is the Run Shell Script action, which skips AppleScript and uses regular shell scripting and parameter expansion.
    – red_menace
    Jul 23, 2022 at 15:17
  • 1
    see apple.stackexchange.com/questions/270159
    – red_menace
    Jul 23, 2022 at 20:06

1 Answer 1

3

Thanks for the hint. Here is the final solution with explanation:

When double clicking on Finder, the input parameter will contain the alias of the file not its full path + name. The key is to convert it using

set fileName to (the POSIX path of input)

This new string, can then be used in the AppleScript section, but needs to be concatenated using & with other strings.

So, for example you can cd change directory to the directory that contains the file, like this:

on run {input, parameters}
    set fileName to (the POSIX path of input)
    tell application "Terminal"
        activate
        do script with command "cd $(dirname " & fileName & ")"
    end tell
end run

enter image description here

1
  • 1
    Also note that input is a list, and you should also quote paths in case they contain spaces or other characters special to the shell.
    – red_menace
    Jul 24, 2022 at 14:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .