rename finder item with contents of clipboard

Hi all,

I am wondering if anyone out there can help.

I have created an automator workflow that opens a CSV file and performs a find and replace to find the headder information and replace it wil nothing (essentially stripping it out).
My process is as follows:

  1. Get specified finder items (folder)
  2. Get folder contents
  3. Filter finder items (for files ending .csv)
  4. Open finder items (using TextEdit)
  5. Get Contents of TextEdit Document
    6 Find and Replace in Text (headder fields and replace with " ")
  6. Write text to File (new.csv)

My probelm is that I want to name the new file that is created (with the headder striped out) to be called the same name as the original, essentially overwriting the old file that had the headder in it, with the new file that has the headder striped out. Is there a way using the Copy to clipboard action that I can pass the name from the clipboard to the rename finder items action. Or can this be done with an automator / applescript combination??

Thanks in advance for any help…

I don’t understand why TextEdit is involved. Once you’ve identified the .csv file you want to modify just read it into a script, modify it there and write it back to the file. Then save the file.

If you post an example, I’ll post a script example for dealing with it.

Hi Adam,

For the simple reason that I am not experienced in AppleScript. I have had a bit more success with automator.
I have uploaded an example of the file I am trying to modify here http://www.peninsular.co.uk/00042367.csv

I look forward to your rerponse.

Hi PS;

It’s not entirely clear to me what you consider a “Header”

I’ve shown the top of the file here. Is “1911304201001,00042367,” part of the header or part of a record?

TruprintOrderNo, BatchNo, Cover, Month1Pic, Month1Cal, Month2Pic, Month2Cal, Month3Pic, Month3Cal, Month4Pic, Month4Cal, Month5Pic, Month5Cal, Month6Pic, Month6Cal, Month7Pic, Month7Cal, Month8Pic, Month8Cal, Month9Pic, Month9Cal, Month10Pic, Month10Cal, Month11Pic, Month11Cal, Month12Pic, Month12Cal, BackPage, OrderQty 1911304201001,00042367,200611251630071080.jpg, …

Further, there appear to be blocks that start or end with a 1 & return, followed by more code like that above, then more jpgs starting with 2006.

Is it just the words at the top: “TruprintOrderNo, BatchNo, Cover, Month1Pic, Month1Cal, Month2Pic, Month2Cal, Month3Pic, Month3Cal, Month4Pic, Month4Cal, Month5Pic, Month5Cal, Month6Pic, Month6Cal, Month7Pic, Month7Cal, Month8Pic, Month8Cal, Month9Pic, Month9Cal, Month10Pic, Month10Cal, Month11Pic, Month11Cal, Month12Pic, Month12Cal, BackPage, OrderQty” that you want removed from the file content or do those “1911304201001,00042367” blocks have to go too?

Hi PS,

I don’t know either exactly what you mean.
Here are 2 versions of an AppleScript, maybe one matches your request

The first version asks to choose a folder, reads every .csv file of the folder,
replaces every item of the first paragraph of each file (beginning with TruprintOrderNo, BatchNo, Cover…)
with a space (" " as you described) and writes the file back to disk.

set theFolder to choose folder
tell application "Finder" to set theItems to items of theFolder whose name extension is "csv"
set CRLF to return & (ASCII character 10)
repeat with i in theItems
	set ff to open for access file (i as string) with write permission
	set csvText to read ff as text
	set oldDelims to text item delimiters
	set text item delimiters to CRLF
	set csvText to text items of csvText
	set text item delimiters to {","}
	set csvItems to text items of item 1 of csvText
	repeat with i in csvItems
		set contents of i to " "
	end repeat
	set item 1 of csvText to csvItems as text
	set text item delimiters to CRLF
	set csvText to csvText as text
	set text item delimiters to oldDelims
	set eof of ff to 0
	write csvText to ff
	close access ff
end repeat

The second version does the same except it cuts the whole first paragraph

set theFolder to choose folder
tell application "Finder" to set theItems to items of theFolder whose name extension is "csv"
set CRLF to return & (ASCII character 10)
repeat with i in theItems
	set ff to open for access file (i as string) with write permission
	set csvText to read ff as text
	set oldDelims to text item delimiters
	set text item delimiters to CRLF
	set csvText to text items of csvText
	set csvText to rest of csvText as text
	set text item delimiters to oldDelims
	set eof of ff to 0
	write csvText to ff
	close access ff
end repeat

Hi Adam,

Oops! sorry I meant to write that in my original post! What I call the Header are the items called “TruprintOrderNo, BatchNo, Cover, Month1Pic, Month1Cal, Month2Pic, Month2Cal, Month3Pic, Month3Cal, Month4Pic, Month4Cal, Month5Pic, Month5Cal, Month6Pic, Month6Cal, Month7Pic, Month7Cal, Month8Pic, Month8Cal, Month9Pic, Month9Cal, Month10Pic, Month10Cal, Month11Pic, Month11Cal, Month12Pic, Month12Cal, BackPage, OrderQty” the entries with numbers are the data that I want keep

Sorry.

In that case the innards of Stefan’s script will to the stripping for you (I’ve cut off a big chunk at the bottom)


set csvText to "TruprintOrderNo, BatchNo, Cover, Month1Pic, Month1Cal, Month2Pic, Month2Cal, Month3Pic, Month3Cal, Month4Pic, Month4Cal, Month5Pic, Month5Cal, Month6Pic, Month6Cal, Month7Pic, Month7Cal, Month8Pic, Month8Cal,  Month9Pic, Month9Cal, Month10Pic, Month10Cal, Month11Pic, Month11Cal, Month12Pic, Month12Cal, BackPage, OrderQty
1911304201001,00042367,200611251630071080.jpg,200611251630071081.jpg,200611251630071082.jpg,200611251630071083.jpg,200611251630071084.jpg,200611251630071085.jpg,200611251630071086.jpg,200611251630071087.jpg,200611251630071088.jpg,200611251630071089.jpg,200611251630071090.jpg,200611251630071091.jpg,200611251630071092.jpg,200611251630071093.jpg,200611251630071094.jpg,200611251630071095.jpg,200611251630071096.jpg,200611251630071097.jpg,200611251630071098.jpg,200611251630071099.jpg,200611251630071100.jpg,200611251630071101.jpg,200611251630071102.jpg,200611251630071103.jpg,200611251630071104.jpg,200611251630071105.jpg,1
1911304501002,00042367,200611251630140000.jpg,200611251630140001.jpg,200611251630140002.jpg,200611251630140003.jpg,200611251630140004.jpg,200611251630140005.jpg,200611251630140006.jpg,200611251630140007.jpg,200611251630140008.jpg,200611251630140009.jpg,200611251630140010.jpg,200611251630140011.jpg,200611251630140012.jpg,200611251630140013.jpg,200611251630140014.jpg,200611251630140015.jpg,200611251630140016.jpg,200611251630140017.jpg,200611251630140018.jpg,200611251630140019.jpg,200611251630140020.jpg,200611251630140021.jpg,200611251630140022.jpg,200611251630140023.jpg,200611251630140024.jpg,200611251630140025.jpg,1
1911307901003,00042367,200611251631350000.jpg,200611251631350001.jpg,200611251631350002.jpg,200611251631350003.jpg,200611251631350004.jpg,200611251631350005.jpg,200611251631350006.jpg,200611251631350007.jpg,200611251631350008.jpg,200611251631350009.jpg,200611251631350010.jpg,200611251631350011.jpg,200611251631350012.jpg,200611251631350013.jpg,200611251631350014.jpg,200611251631350015.jpg,200611251631350016.jpg,200611251631350017.jpg,200611251631350018.jpg,200611251631350019.jpg,200611251631350020.jpg,200611251631350021.jpg,200611251631350022.jpg,200611251631350023.jpg,200611251631350024.jpg,200611251631350025.jpg,1
"
set oldDelims to text item delimiters
set text item delimiters to return
set csvText to text items of csvText
set csvText to rest of csvText as text
set text item delimiters to oldDelims

--> "1911304201001,00042367,200611251630071080.jpg,200611251630071081.jpg,200611251630071082.jpg,200611251630071083.jpg,200611251630071084.jpg,200611251630071085.jpg,200611251630071086.jpg,200611251630071087.jpg,200611251630071088.jpg,200611251630071089.jpg,200611251630071090.jpg,200611251630071091.jpg,200611251630071092.jpg,200611251630071093.jpg,200611251630071094.jpg,200611251630071095.jpg,200611251630071096.jpg,200611251630071097.jpg,200611251630071098.jpg,200611251630071099.jpg,200611251630071100.jpg,200611251630071101.jpg,200611251630071102.jpg,200611251630071103.jpg,200611251630071104.jpg,200611251630071105.jpg,1
1911304501002,00042367,200611251630140000.jpg,200611251630140001.jpg,200611251630140002.jpg,200611251630140003.jpg,200611251630140004.jpg,200611251630140005.jpg,200611251630140006.jpg,200611251630140007.jpg,200611251630140008.jpg,200611251630140009.jpg,200611251630140010.jpg,200611251630140011.jpg,200611251630140012.jpg,200611251630140013.jpg,200611251630140014.jpg,200611251630140015.jpg,200611251630140016.jpg,200611251630140017.jpg,200611251630140018.jpg,200611251630140019.jpg,200611251630140020.jpg,200611251630140021.jpg,200611251630140022.jpg,200611251630140023.jpg,200611251630140024.jpg,200611251630140025.jpg,1
1911307901003,00042367,200611251631350000.jpg,200611251631350001.jpg,200611251631350002.jpg,200611251631350003.jpg,200611251631350004.jpg,200611251631350005.jpg,200611251631350006.jpg,200611251631350007.jpg,200611251631350008.jpg,200611251631350009.jpg,200611251631350010.jpg,200611251631350011.jpg,200611251631350012.jpg,200611251631350013.jpg,200611251631350014.jpg,200611251631350015.jpg,200611251631350016.jpg,200611251631350017.jpg,200611251631350018.jpg,200611251631350019.jpg,200611251631350020.jpg,200611251631350021.jpg,200611251631350022.jpg,200611251631350023.jpg,200611251631350024.jpg,200611251631350025.jpg,1

The next issue is to determine how the “paragraphs” of your csvText are terminated. Stephan used Return & LineFeed which is the standard Microsoft Windows (but would depend on the server that sent the data file to me), but when I copied the text above from the source of the page for your link, return by itself worked, and CRLF didn’t. Except for that, Stefan’s script will read each of the CSV files in the chosen folder, strip the first “paragraph” off, and write the data back to the original file. I think that’s exactly what you want.

Stefan, this script is perfect, cutting the first paragraph is exactly what I wanted to achieve, however is there anyway that instead of the script asking for a folder, that it could process a file passed to it in automator?
That way I could encorporate it into an automator workflow I am working on…

Immediately following your automator action that delivers a folder selection, you put Stefan’s script minus the first line in a “Run AppleScript” action item. Stefan’s script has no output - it does things to the folder’s contents, but if you want error messages or a “done” message you’d put them in the script and feed them out to your dialog.

Hi Adam,

I tried that, but for some reason when I use that code and try to run it I get a syntax error. The following is the exact code I used:


tell application "Finder" to set theItems to items of theFolder whose name extension is "csv"
set CRLF to return & (ASCII character 10)
repeat with i in theItems
   set ff to open for access file (i as string) with write permission
   set csvText to read ff as text
   set oldDelims to text item delimiters
   set text item delimiters to CRLF
   set csvText to text items of csvText
   set csvText to rest of csvText as text
   set text item delimiters to oldDelims
   set eof of ff to 0
   write csvText to ff
   close access ff
end repeat

The syntax error it returns is “[ Expected end of line but found “(”. ]” and it highlights the fourth line of the script where it says “(i as string)”
Any ideas, the above code compiles perfectly in AppleScript script editor but not when used in a Run Applescript Automator action.
In addition, I will be using a combination of get folder contents and filter finder items for filenames containging .csv to pass the csv files to the action script
to be processed, will this still work on multiple files being passed to the actionscript action?

The problem is probably with the name of one of the csv file names - it works as advertised for me.

Add this line: “set theFolder to (choose folder)” ahead of the script and run it from the script editor. What happens?

Adam, I’ve noticed the same problem. In Script Editor and - Debugger everything works fine,
but not in Automator. The crucial line is:

set ff to open for access file (i as string) with write permission

I have no idea, why

And I can’t help, Stefan – I’m a dunce about Automator; I’ve never used it - abandonned it immediately when I saw that, like QuicKeys, it has no branching instructions.

One vague possibility: replace set ff to open for access file (i as string) with write permission, with set ff to open for access file (contents of i as string) with write permission

A little further testing shows that this simple script works in an Automator workflow, so it isn’t opening the file that’s stopping it.


on run
	set f to (open for access ((path to desktop as text) & "AutoTest.txt") with write permission)
	write "Hello World" to f
	close access f
end run

so am I . Everything you can do with Automator, you can also do with AppleScript.

I beg your pardon :wink: QuicKeys has branching instructions (but very rudimental)

If you consider skipping an instruction “branching”. :confused: