Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Automator to process new files

Wondering in anyone can help me out here or point me where to look.


I have files that are created by windows (in a VM on my Mac) - which I then email using Apple Mail to folks using Outlook on Windows - in the process the windows CR LF gets converted into a double CR (or something that results in effectively a double CR).


I have a terminal command that works:


for f in *csv; do sed 's/^M//' $f > $f.new && mv $f.new $f; done


and have found apps that can do the same thing - with user intervention.


But what I really want to do is setup an Automator folder action that will run the command for me.


I thought I was close with something along the lines of this:



Find Finder Items - where file extension is .csv

pass as arguments to shell script

with


for f in "$@"; do sed 's/^M//' $f > $f.new && mv $f.new $f; done


but that does not seem to do it. not sure if maybe the ^M is not getting parsed correctly - when typing that in the command line you hvae to type ctrl-V then ctrl-M to get it to show properly. maybe there is some other sed parameters that woudl work.


Overall - I would like any new file csv file created in my working folder to have a conversion run on it such that emailing it doesn't result in extra lines.


Or another option that may work is a folder action that triggers an Applescript to open each new csv in MS Excell on the Mac and then save and close it - that appears to correct the eol issue such that Mail does not "corrupt" the files.

MacBook Pro, Mac OS X (10.7.3), iPhone 4s 64GB iPad 16GB

Posted on Mar 20, 2012 7:34 AM

Reply
8 replies

Mar 20, 2012 7:45 AM in response to Matt Wolanski

Hmm couldn't seem to edit the original post.


ideally - if done via open - save - close (ignore error) - I would prefer it be done entirely in the background - that is - do not open excel such that the user sees it happening.


It would be nice if the record options in Applescript and or Automator worked better - for example - the record function in AS shows the opening of the file - but completely misses the save and close operations - and in Automator it does not seem to be very easy to make generic.


Of course other reocrd fucntions (VBA) are just as bad - though sometimes useful is figuring out a command or a syntax.

Mar 20, 2012 9:03 AM in response to Tony T1

I found part of my issue - need to use "csv" not ".csv" as the file extension to filter the results - the workflow was filtering out all the files before it even got to the shell script.


related issue seems to be that not every new files is caught - seems to be somewhat random as to whether or not new files trigger the action. I am using the included New items alert script to monitor whether or not the new items were detected.


I tried what you had there and it did not seem to work - but I may have interuppted the scrtip.


I just tried this:


for f in "$@"

do

sed s/.$// $f > $f.new && mv $f.new $f

done


and it seems to work


here is a screen shot from automator - "working" is the local folder where the csv files are created. I suppose I could use Any instead of All for the conditions - depends on whether I add more.

User uploaded file


the test for that it is working is to send the converted file from Mac OS Apple Mail to Window MS Outlook and not have extra lines appear.


I mightneed to include .txt files in there as well - I have some txt files that are created on the windows side that when I copy paste into Mac apps end up with extra lines.

Mar 20, 2012 10:44 AM in response to Matt Wolanski

I tried what you had there and it did not seem to work - but I may have interuppted the scrtip.


I had a typo in my post (extraneous '/').


sed s/.$// will remove the last char on the line (any char) while 's/'$'\r''//' will remove the 1st carriage return found, 's/'$'\r''$//g' will remove all carriage returns and 's/'$'\r''$//' will remove a carriage return, if it is the last char on the line.

Mar 20, 2012 11:01 AM in response to Tony T1

Thanks for the update.


the really weird thing now is that it seems to be working on my home machine - but when I replicate it on the work system it does not work.


both systems have the same OS version 10.7.3 - the only (major) difference is that I am using an admin account on the home computer and not admin on the work machine.


not sure that woudl cause the probelm though - becuase I can run the shell script from terminal and it works - but when I included it with the automator it does not work.


even more bizzare - I have run the automator manually (with get finder selected items) on both systems and it works on one and not the other.


on the work system it appears to run properly - I even added an echo $f >> log.txt - and I get output there - but the file itself it unmodified.


the other major difference is Parallels on the home system and VMware Fusion on the work machine - and sometimes when I try to delete files created by the win7 VM I get a file in use message - so it is possible that the sed command is not able to do the swap.

Mar 20, 2012 1:40 PM in response to Matt Wolanski

I think I have solved that mystery.


On my home machine where I was doing the dev and test my folder path has no spaces in the fully qualifies folder path. On my work machine I do have a space in the fully qualified folder path.


So while this works at home:


for f in "$@"

do

tr -d '\r' < $f > $f.new

mv $f.new $f

done



I have to use this at work (and should also work at home):


for f in "$@"

do

tr -d '\r' < "$f" > "$f.new"

mv "$f.new" "$f"

done


might be hard to tell - but the distinction is that I had to enclose the $f and $f.new in quotes

Automator to process new files

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