Automator script getting syntax error on line 31

link to original thread:


everything in Automator script works, then syntax error in line 31


!/bin/zsh

#

: <<'COMMENT'

Select nth files from the SRCDIR and copy them into the DSTDIR.

COMMENT


typeset -gi mod=0

typeset -ga ary=()


function usage () {

printf '%s %s\n' "${ZSH_ARGZERO}" sourcefolder destfolder nth_value"

printf '%s %s\n' "${ZSH_ARGZERO}" "~/Desktop/TestZ ~/Desktop/BAR 3"

return

}


# three arguments or bust

((${#} == 3)) || ( usage;exit 1 )


# the nth value

mod=$3

# force relative path references to absolute paths

SRCDIR="${1:a:s/\~\//}"

DSTDIR="${2:a:s/\~\//}"


# do these folders exist?

[[ -d "${SRCDIR}" ]] || ( usage;exit 1 )

[[ -d "${DSTDIR}" ]] || ( mkdir "${DSTDIR}" )


# put every nth file into the array

ary=( $(printf '%s\n' "${SRCDIR}"/*.*(.Non) | awk -v m="${mod}" 'NR>1 && NR%m == 1') )


# sort array items in (o) ascending name order

for f in ${(o)ary[@]};

do

# if file exists in destination folder, don't process it

[[ -s "${DSTDIR}/${f:a:t}" ]] && continue

# copy the file from the source folder to the destination folder

printf '%s -> %s\n' "${f}" "${DSTDIR}/${f:a:t}"

cp -p "${f}" "${DSTDIR}/${f:a:t}"

done

exit 0

Mac Studio, macOS 13.2

Posted on Mar 4, 2023 11:37 AM

Reply
Question marked as Top-ranking reply

Posted on Mar 5, 2023 5:32 AM

The above script was written to be executed (and tested) from the command line, not Automator. The following Automator solution uses one Ask for Finder Items action so one can choose the source folder and destination folder in one action using the command key.


It then prompts for the n-th value of modulo files to copy between folders and then presents these three values in the order necessary for successful Run Shell Script execution. The following was tested on Monterey 12.6.3.



Code for the Run Shell Script contents:


#!/bin/zsh

: <<"COMMENT"
Copy nth file from the SRCDIR with modulo value to the DSTDIR.
Argument 1: Source Directory
Argument 2: Destination Directory
Argument 3: n-th file number for modulo
COMMENT

# not visible in Automator
function usage () {
    printf '%s %s\n' "${ZSH_ARGZERO}" "sourcefolder destfolder nth_value"
    printf '%s %s\n' "${ZSH_ARGZERO}" "~/Desktop/TestZ ~/Desktop/BAR 3"
    return
}

# script must have three arguments or bust
((${#} == 3)) || ( usage;exit 1 )

# nth file value is third argument
typeset -gi mod=$3
typeset -ga ary=()

# force relative path references to absolute paths
SRCDIR="${1:a:s/\~\//}"
DSTDIR="${2:a:s/\~\//}"

# file extension pattern to match
FILE_EXT="*.txt"

# put plain text files into an array sorted by version
ary=( $(print "${SRCDIR}"/${~FILE_EXT}(.N) | sort -V) )
# print -l $ary

# the range is 1 to the count of files in the array
for n in {1..${#ary[@]}};
do
    # if modulo is met, then copy file and if not get next array index
    (( $n % $mod == 0 )) || continue
    # printf '%s -> %s\n' "${ary[$n]}" "${DSTDIR}/${${ary[$n]}:a:t}"
    cp -a "${ary[$n]}" "${DSTDIR}/${${ary[$n]}:a:t}"
done
unset ary mod
exit 0

2 replies
Question marked as Top-ranking reply

Mar 5, 2023 5:32 AM in response to VikingOSX

The above script was written to be executed (and tested) from the command line, not Automator. The following Automator solution uses one Ask for Finder Items action so one can choose the source folder and destination folder in one action using the command key.


It then prompts for the n-th value of modulo files to copy between folders and then presents these three values in the order necessary for successful Run Shell Script execution. The following was tested on Monterey 12.6.3.



Code for the Run Shell Script contents:


#!/bin/zsh

: <<"COMMENT"
Copy nth file from the SRCDIR with modulo value to the DSTDIR.
Argument 1: Source Directory
Argument 2: Destination Directory
Argument 3: n-th file number for modulo
COMMENT

# not visible in Automator
function usage () {
    printf '%s %s\n' "${ZSH_ARGZERO}" "sourcefolder destfolder nth_value"
    printf '%s %s\n' "${ZSH_ARGZERO}" "~/Desktop/TestZ ~/Desktop/BAR 3"
    return
}

# script must have three arguments or bust
((${#} == 3)) || ( usage;exit 1 )

# nth file value is third argument
typeset -gi mod=$3
typeset -ga ary=()

# force relative path references to absolute paths
SRCDIR="${1:a:s/\~\//}"
DSTDIR="${2:a:s/\~\//}"

# file extension pattern to match
FILE_EXT="*.txt"

# put plain text files into an array sorted by version
ary=( $(print "${SRCDIR}"/${~FILE_EXT}(.N) | sort -V) )
# print -l $ary

# the range is 1 to the count of files in the array
for n in {1..${#ary[@]}};
do
    # if modulo is met, then copy file and if not get next array index
    (( $n % $mod == 0 )) || continue
    # printf '%s -> %s\n' "${ary[$n]}" "${DSTDIR}/${${ary[$n]}:a:t}"
    cp -a "${ary[$n]}" "${DSTDIR}/${${ary[$n]}:a:t}"
done
unset ary mod
exit 0

Mar 4, 2023 2:07 PM in response to pschiller59

Sometimes, one gets to look at previous posted code and realize that its functionality is also wrong. The original script was not selecting the matching modulo files in the array and was more complicated then it needed to be. I have fixed some of the code for accuracy and performance. The following was tested on Ventura 13.2.1.


#!/bin/zsh

: <<"COMMENT"
Copy nth file from the SRCDIR with modulo and copy them to the DSTDIR.
COMMENT

typeset -gi mod=$3
typeset -ga ary=()

# not visible in Automator
function usage () {
    printf '%s %s\n' "${ZSH_ARGZERO}" "sourcefolder destfolder nth_value"
    printf '%s %s\n' "${ZSH_ARGZERO}" "~/Desktop/TestZ ~/Desktop/BAR 3"
    return
}

# script must have three arguments or bust
((${#} == 3)) || ( usage;exit 1 )

# file extension to match
FILE_EXT="*.txt"

# force relative path references to absolute paths
SRCDIR="${1:a:s/\~\//}"
DSTDIR="${2:a:s/\~\//}"

# put plain text files into an array sorted by version
ary=( $(ls -1 "${SRCDIR}"/${~FILE_EXT}(.N) | sort -V) )
# print -l $ary

# the range is 1 to the count of files in the array
for n in {1..${#ary[@]}};
do
    # if modulo is met, then copy file and if not get next array index
    (( $n % $mod == 0 )) || continue
    # printf '%s -> %s\n' "${ary[$n]}" "${DSTDIR}/${${ary[$n]}:a:t}"
    cp -a "${ary[$n]}" "${DSTDIR}/${${ary[$n]}:a:t}"
done
unset ary mod
exit 0


This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Automator script getting syntax error on line 31

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.