rename a csv file

Hi there,

GUI scripting aside, which ive been employing to Automate logic pro, this is my first attempt at writing well, anything really. Im working on an app that analyses short monophonic audio samples from a directory with NLP then returns a list of harmonically compatible samples from the user database.
The audio sample analysis produces a CSV file which i need to parse a column from and calculate an average, which ive achieved in the code below. At this stage I have two issues.

  1. I cannot work out how to simply change the name of the selected file, (which is stored in a variable) use the original prefix of “the_file” set as USERS/home/music/sample1… ie sample1 (then add) & 2ndVariable & “.” & csv - the second variable is the result from the average frequency reading earlier in the script, so yeah im just trying to add the result from the frequency average sum, after the original file name and before the extension,

2.Problem, I need to code it to batch the process below.

I really hope someone can help, ive been stuck here for over 24 hours and decided rather than get more depressed… reach out for help,

Thanks in advance


use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions

on run
	tell application "Finder"
		set the_file to choose file with prompt "Select a file for processing"
		-- set the_file to choose file with multiple selections allowed (wont accept batch processing)
	end tell
	
	set my_data to read the_file
	set my_list to paragraphs 2 thru -2 of my_data as list -- theres a text row i want to be ignored
	
	set new_list to {}
	
	set oldDelims to AppleScript's text item delimiters
	set AppleScript's text item delimiters to ","
	
	
	repeat with an_item in my_list
		
		try
			set x to text item 1 of an_item    -- timestamp from CSV
			set y to text item 2 of an_item    -- frequency readings from CSV
			set z to text item 3 of an_item    -- confidence readings from CSV
			set component_list to {y}
			set end of new_list to component_list
		end try
	end repeat
	-- set AppleScript's text item delimiters to oldDelimsr
	
	
	set A to 0
	
	repeat with this_item in new_list
		set A to A + this_item
	end repeat
	set theAvg to (A / (count new_list))
	A / (count new_list)
	set rndAvg to (round (theAvg))
	round (theAvg)
	
	
	
end run

So i have the result rndAvg to be added to the filename

Model: 2019 2.9 or something xeon core 16gb
AppleScript: 2.4
Browser: Safari 605.1.15
Operating System: macOS 10.15

I removed some things that the current script does not need, and added one comment.
File renaming code is added at the end. It’s written to be extension-agnostic, i.e. it works with any extension, or no extension at all.

tell application "Finder"
	set the_file to choose file with prompt "Select a file for processing"
	-- set the_file to choose file with multiple selections allowed (wont accept batch processing)
end tell

set my_data to read the_file
set my_list to paragraphs 2 thru -2 of my_data as list -- theres a text row i want to be ignored

set new_list to {}

set oldDelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to ","

repeat with an_item in my_list
	--try > ADDED don't use this until you're sure any errors are harmless
	set x to text item 1 of an_item -- timestamp from CSV
	set y to text item 2 of an_item -- frequency readings from CSV
	set z to text item 3 of an_item -- confidence readings from CSV
	set end of new_list to y
	--end try
end repeat

set A to 0

repeat with this_item in new_list
	set A to A + this_item
end repeat
set theAvg to (A / (count new_list))
set rndAvg to (round (theAvg))

tell application id "MACS"
	set name_ to name of the_file
	set ext to name extension of the_file
end tell

if ext is not "" then -- drop extension
	set AppleScript's text item delimiters to "." & ext
	set name_ to text item 1 of name_
	set name_ to name_ & space & rndAvg & "." & ext
else
	set name_ to name_ & space & rndAvg
end if

set AppleScript's text item delimiters to oldDelims

tell application id "MACS" to set name of the_file to name_

that works perfectly, thank you so much!

Have you any idea how I might make the script work with batch processing?
I need to run that task 100,000 times!

Well, that’s no small beer, to use a flemish expression.
AppleScript (and Finder) do not cope well with that kind of numbers.
But it can be done; below is the basic structure for an applet/droplet.

- comments are ‘pseudocode’, they must be replaced with actual code.

--------------------------------------------------------
-- This handler steps thru the files,
-- and filters out any that are not csv
-- parameter:	[list]	list of aliases
--------------------------------------------------------
on main(listInput)
	repeat with cAlias in listInput
		# is it csv?
		# yes , get its name > go to work
		# no, do nothing (skips to the next one)
	end repeat
end main
--------------------------------------------------------
-- This handler deals with one CSV file
-- parameter:	[alias]	file to work on
--				[text]	its name
--------------------------------------------------------
on goToWork(cFile, cName)
	# this contains most of the code you already have
end goToWork
######################################################
on run -- when double-clicked
	local sel
	tell application id "MACS" to set sel to get selection as alias list
	main(sel)
end run
######################################################
on open drop -- when dropped-on
	main(drop)
end open

Save this as an application. You can select files, then run it, or drop files on its icon.
It should work when run as a script as well.

When you decide to go this route, begin with small numbers, 50 or so. Then increase.
It wil likely fail at some point, but I cannot predict when, or how.

Thats amazing, thank you so much!
Have you any idea if its possible to split the files into smaller groups and use multi threading to process smaller batches faster?



use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

set subShell1 to "osascript ~/desktop/mThread1.scpt"
set subShell2 to "osascript ~/desktop/mThread2.scpt"
set subShell3 to "osascript ~/desktop/mThread3.scpt"
set subShell4 to "osascript ~/desktop/mThread4.scpt"
set yourScript to subShell1 & " & " & subShell2 & " & " & subShell3 & " & " & subShell4
do shell script yourScript

I don’t think that will work. AppleScript itself is single threaded, so it’ll probably run one script after another, with no guarantee that any internal storage is reset between scripts. So I’d expect the same outcome: after some maximum number of files is processed it gets an error because it has run out of storage, and stops.

BTW, the 1st two lines of that little script are not needed in this context; they are only useful when running ASOjbC code.

I thought that might happen too, then running this took about 4 times longer


set aLocation to alias "Mojave:Users:dh:Documents:script.as:" 
run script aLocation
set bLocation to alias "Mojave:Users:dh:desktop:Kick.scpt:" 
run script bLocation
set cLocation to alias "Mojave:Users:dh:desktop:FX.scpt:" 
run script cLocation
set dLocation to alias "Mojave:Users:dh:desktop:Oerc.scpt" 
run script dLocation

4x run script vs. 1x do shell explains the difference for me.

The obvious bottleneck is looping over the files. If you can find a way to do that outside AppleScript you can still use the code you already have to deal with one file. I can’t help you there, sorry.
You might start by googling MacScripter for stack overflow, and look at threads of the last few years.

It still might be more practical to find out what the maximum number of files is, and then have the script process, say, 90% of that number in one run.

Thank you very much for your help Alastor :slight_smile: