-
All replies
-
Helpful answers
-
-
May 21, 2014 3:17 PM in response to Amosikoby Tony T1,If you want, you can give it a try in Applescript.
This assumes that the image and csv file are on the Desktop.
Open Applescript, paste this into it and change SND.csv to the name of your csv file:
do shell script " cd ~/Desktop while read line do OldImageName=${line%,*} NewImageName=${line#*,} mv \"$OldImageName\" \"$NewImageName\" done < \"SND.csv\" "Then in Applescript, click Run
-
May 22, 2014 6:46 AM in response to Tony T1by Amosiko,Thanks Tony,
For whatever reason I can't seem to get this to work.
I have tried the Applescript and this is what I got.
For whatever reason it doesn't change the file names. Thank you for all you efforts.
tell current application
do shell script "
cd ~/Desktop
while read line
do
OldImageName=${line%,*}
NewImageName=${line#*,}
mv \"$OldImageName\" \"$NewImageName\"
done < \"ImageName.csv\"
"
--> ""
end tell
Result:
""
-
May 22, 2014 8:33 AM in response to Amosikoby Mark Jalbert,For whatever reason I can't seem to get this to work.
Your csv file needs to be plain text (ascii text). Plain text may have old style mac line endings (CR), windows line endings (CRLF), or unix line endings (LF).
Cocoa GUI applications can read and write to all those variations of plain text files but Unix (posix) applications need LF line endings in order to work properly.
Excel on a Mac saves csv files with the old style mac line endings (CR- carrage return) unless you select the Windows csv option. In each option the saved csv file does not have the last line terminated. This can sometimes be an issue. Apple's Numbers application saves csv files with Windows line endings (CRLF).
Your csv file more than likely needs to be converted to unix line endings (LF- line feed).
-
May 22, 2014 8:48 AM in response to Amosikoby Tony T1,This will fix the line endings per Mark's comment:
NewImageName=$( echo "$NewImageName" | tr -d '\r' )
So, try this:
cd "$1"
while read line
do
OldImageName=${line%,*}
NewImageName=${line#*,}
NewImageName=$( echo "$NewImageName" | tr -d '\r' )
if [ -e "$OldImageName" ] ; then
mv "$OldImageName" "$NewImageName"
else
echo "$OldImageName" >> ~/Desktop/Errors.txt
fi
done <"$2"
-
May 22, 2014 9:12 AM in response to Tony T1by Amosiko,When I changed the line endings it was able to change just the first image and non of the rest using Applescript.
Tony,
Using your I am getting this in the Error txt
Old Image Name
IMG_0032.JPG
However non of the files change names. When I select allow multiple selections is when I am getting the jiberish.
-
May 22, 2014 9:46 AM in response to Tony T1by Mark Jalbert,This will fix the line endings per Mark's comment:
Well it doesn't. The following csv file was produced from excel on a mac:
cat -tve oldmac.csv 0031.jpg,31-foo bar.jpg^M0048.jpg,48-bar of foo.jpg^M0053.jpg,53-bar of foo.jpg[KSH_93u+] $ #Line isn't terminated wc -l oldmac.csv 0 oldmac.csv while read line; do echo ${line%,*} done < oldmac.csv #No resultsA csv file saved as Window csv from excel on a mac:
cat -tve windows.csv 0031.jpg,31-foo bar.jpg^M$ 0048.jpg,48-bar of foo.jpg^M$ 0053.jpg,53-bar of foo.jpg[KSH_93u+] $ #The last line isn't terminated wc -l windows.csv 2 windows.csv while read line; do echo ${line%,*} done < windows.csv 0031.jpg 0048.jpg #Last line isn't readIn order to make your script work seamlessly, you are going to have to test the csv file for it's line endings and change them to unix line endings and terminate the last line.
-
May 23, 2014 6:09 AM in response to Mark Jalbertby Tony T1,Just pipe to a tmp file and add a LF to the eof, then convert CR to LF:
cd "$1" tFile=$(mktemp -t tmp) cat "$2" | sed 'a\' | tr '\r' '\n' > "$tFile" while read line do OldImageName=${line%,*} NewImageName=${line#*,} if [ -e "$OldImageName" ] ; then mv "$OldImageName" "$NewImageName" else echo "$OldImageName" >> ~/Desktop/Errors.txt fi done < "$tFile" rm "$tFile"[Note: This works for both CR and CRLF endings as the extra LF in a csv file saved as Window csv from excel on a mac is ignored as we test for non-existant files. However, this can be adapted by just changing tr '\r' '\n' to tr -d '\r' ]
This bash script is written to be used in this Automator Workflow:
-
May 23, 2014 6:26 AM in response to Tony T1by Mark Jalbert,This is the approach that I came up with. I didn't test it in Automator. Feel free to borrow any of it.
# Attempt to remove the tmp file if the script terminates early trap 'rm -f /tmp/csv$$; exit' 1 2 3 15 # Sanity check and line endings conversion IS_ASCII=$(file FILE | sed 's/.*: //; s/, .*//') if [ "$IS_ASCII" = "ASCII text" ]; then # strings changes all line endings to LF # regardless of the current line endings # and terminates the last line strings FILE > /tmp/csv$$ else echo "Aborting script- problem with csv file" exit 1 fi while read line; do . . . . . . . done < /tmp/csv$$ rm -f /tmp/csv$$
-
May 23, 2014 6:58 AM in response to Mark Jalbertby Tony T1,Basically, your testing that the user seleted a valid csv file and used strings instead of
cat FILE | sed 'a\' | tr '\r' '\n'(Interesting use of strings, I'll have to remember that). Not sure if strings is part of Mavericks, I think that the command line developer tools are now needed. Also good use of file to determine line endings -
May 24, 2014 6:16 AM in response to Tony T1by Tony T1,I like Mark's suggestion of testing with file:
cd "$1"
tFile=$(mktemp -t tmp)
fType=$(file -b "$2")
if [ "$fType" = "ASCII text, with CR line terminators" ]; then
cat "$2" | sed 'a\' | tr '\r' '\n' > "$tFile"
elif [ "$fType" = "ASCII text, with CRLF line terminators" ]; then
cat "$2" | sed 'a\' | tr -d '\r' > "$tFile"
else
echo "Problem with CSV file ($fType)" > ~/Desktop/Errors.txt
rm "$tFile"
exit 1
fi
while read line
do
OldImageName=${line%,*}
NewImageName=${line#*,}
if [ -e "$OldImageName" ] ; then
mv "$OldImageName" "$NewImageName"
else
echo "$OldImageName" >> ~/Desktop/Errors.txt
fi
done < "$tFile"
rm "$tFile"
-
May 24, 2014 6:26 AM in response to Tony T1by Tony T1,Edit: Need to do no conversion if LF's:
cd "$1"
tFile=$(mktemp -t tmp)
fType=$(file -b "$2")
if [ "$fType" = "ASCII text, with CR line terminators" ]; then
cat "$2" | sed 'a\' | tr '\r' '\n' > "$tFile"
elif [ "$fType" = "ASCII text, with CRLF line terminators" ]; then
cat "$2" | sed 'a\' | tr -d '\r' > "$tFile"
elif [ "$fType" = "ASCII text" ]; then
cat "$2" | sed 'a\' > "$tFile"
else
echo "Problem with CSV file ($fType)" > ~/Desktop/Errors.txt
rm "$tFile"
exit 1
fi
while read line
do
OldImageName=${line%,*}
NewImageName=${line#*,}
if [ -e "$OldImageName" ] ; then
mv "$OldImageName" "$NewImageName"
else
echo "$OldImageName" >> ~/Desktop/Errors.txt
fi
done < "$tFile"
rm "$tFile"
-
May 24, 2014 6:27 AM in response to Tony T1by Mark Jalbert,Yah, I just tested for "ASCII text" not the line endings because I knew that strings would take care of the rest. Bummer that strings is missing from a standard install. Personally, I'll attempt to find another solution than use multiple pipes. Here's what I'll offer-
L_END=$(file -b FILE | sed 's/.*with //; s/ .*//') if [ "$L_END" = "CR" ]; then awk '{sub(/\r/,"\n"); print $0}' FILE > /tmp/csv$$ #or use perl #perl -lpe 's/\r/\n/g' FILE > /tmp/csv$$ elif [ "$L_END" = "CRLF" -o "$L_END" = "ASCII" ]; then awk '{gsub(/\r/,""); print $0}' FILE > /tmp/csv$$ #or in perl #perl -lpe 's/\r//g' FILE > /tmp/csv$$ else echo "Aborting script- problem with csv file" exit 1 fi -
May 24, 2014 6:37 AM in response to Mark Jalbertby Tony T1,Personally, I'll attempt to find another solution than use multiple pipes.
Why? Nothing wrong with pipes (multiple or otherwise, and you're using one with sed)
I prefer tr over awk or perl over for a line-feed conversion.
Also prefer to use mktemp for temp file creation.
-
May 24, 2014 6:52 AM in response to Tony T1by Mark Jalbert,I said multiple pipes not a single pipe. With multiple pipes you are forking to subshells in order to run each command. If possible I try to avoid them. The advantage to using awk or perl is that either will change the line endings and terminate the last line of the csv file if it isn't.

