0

Seems extremely simple, but I can't figure it out...

I want to be able to change to the directory (folder) where the dropped file is located before running my shell script on that file.

So either I change to the directory first with Automator, then pass that argument to Shell Script or Applescript

or I change to the directory inside of Applescript or Shell before running my Shell command. pls help

2
  • 1
    Please post an answer below! Glad you got it working!
    – anki
    Dec 15, 2019 at 6:45
  • As coded, the solution you posted as part of your question, which really should have been posted as a separate answer and not an edit to the question, will fail with e.g. Can’t get POSIX path of {alias "...", alias "..."} if more then one file is dropped. Use item 1 of input to act only of the first file dropped or use a repeat loop. You also need to properly double quote variables to prevent globbing and word splitting. As the current code fails if the dropped file has spaces anywhere in its fully qualified pathname. You also need to use Fold's quoted form for the same reason. Dec 16, 2019 at 18:40

2 Answers 2

1

This is very easy in zsh. You can use the :h option to a variable to refer to the head of its full path:

#!/usr/bin/env zsh -f

PATH="/usr/local/bin:/usr/bin:/usr/sbin:/sbin:/bin"

for i in "$@"
do

    FILE="$i"

    cd "$FILE:h"

    # do other things here

done

In a shell like bash you would need to use the dirname command:

#!/usr/bin/env bash

PATH="/usr/local/bin:/usr/bin:/usr/sbin:/sbin:/bin"

for i in "$@"
do

    FILE="$i"

    DIR=$(dirname "$FILE")

    cd "$DIR"

    # do other things here

done
5
  • Ah. I didn’t think about that. I have made changes to handle multiple files with the for i loop.
    – TJ Luoma
    Dec 15, 2019 at 3:09
  • nice way to do it.
    – ProBowie
    Dec 15, 2019 at 3:14
  • You really do not need the FILE and DIR as i can be used directly, e.g. for i in "$@"; do cd "$i:h"; done for zsh and for i in "$@"; do cd "$(dirname "$i")"; done for bash. I'd also use error handling, e.g." cd "$(dirname "$i")" || exit Dec 15, 2019 at 3:28
  • i can be used directly, but I prefer to use more meaningful variable names, as they make debugging later much easier, especially some months later when I’ve most likely forgotten everything about the script, what it does, and how it does it.
    – TJ Luoma
    Dec 15, 2019 at 17:38
  • RE: "especially some months later when I’ve most likely forgotten everything about the script, what it does, and how it does it." ... Isn't that what commenting code is for? (Rhetorical question.) Dec 16, 2019 at 18:42
0

[Final working code]

Here is the function which takes in the fully qualified file path as an argument, Opens Terminal, and performs a bash script.

The script defines the pathname and folder variables, then changes to the folder directory then calls an external function "fds" and passes the pathname argument to it.

Again, thanks for all the comments, especially user3439894 as his suggestion below was spot on.

on process_fds(fdsFile)
    set Fold to POSIX path of fdsFile
    tell application "Terminal"
        activate
        do script "pathname=" & Fold's quoted form & ";folder=\"$(dirname \"$pathname\")\";cd \"$folder\";fds \"$pathname\""
    end tell
end process_fds
1
  • The code you've posted is incomplete as you only show a handler without showing how it's being called or the rest of the code in the Run AppleScript action. The code is also not properly quoted and should be as e.g. do script "pathname=" & Fold's quoted form & "; folder=\"$(dirname \"$pathname\")\"; cd \"$folder\"; fds \"$pathname\"" Dec 17, 2019 at 11:57

You must log in to answer this question.

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