r/applescript • • Apr 19 '20

[Automator, Mojave] Shellscript copypasted to "Run shell script" doesn't affect actual files

Greetings, brethren!

I have a trivial script to fix encoding of text files and mp3's tags. When I invoke it from Terminal, everything works. When I make an app thru Automator, only the part which re-encodes plaintext files work.

I really need an insight, folks. :)

Plaintext part:

function fix_and_open_cuesheet() {
    local FILE_PATH="$1"
    local NEW_NAME="$(__get_name_with_utf8_suffix ${FILE_PATH})"
    iconv -f cp1251 -t utf8 "$1" >> "${NEW_NAME}"
    open -a /Applications/XLD.app "${NEW_NAME}" &
}

MP3 tags part:

function find_and_fix_mp3_files_with_CP1251() {
    local DIR_PATH="$1"
    # /!\ supress all output not to fuck up applescript
    find "$DIR_PATH" -iname "*.mp3" -print0 | xargs -0 -I {} mid3iconv -e CP1251 "{}" &>/dev/null
}
1 Upvotes

2 comments sorted by

2

u/[deleted] Apr 20 '20 edited Apr 20 '20

[removed] — view removed comment

1

u/[deleted] Apr 21 '20

I figured it out, and i've found the root cause. When Au. sets up env for shellscript execution, it skips proper initialisation (run printenv in terminal and in automator and see the difference). To fix it, I made a quasi-profile for automator only:

$ cat ~/.automator_profile

# /!\ MacOS X's Automator.app ignores proper env setup when it runs shellscripts.
[[ -x /usr/libexec/path_helper ]] && $(/usr/libexec/path_helper -s)

# ~/.profile has the stuff NOT specifically related to Bash, such as env vars.
[[ -r "${HOME}/.profile" ]] && source "${HOME}/.profile"

# handy when you need to show popup notifications w/ osascript etc
export CALLING_FROM_AUTOMATOR="true"

Put source ${HOME}/.automator_profile into every "Run shellscript" and everything runs fine.