Sorry this is a bit of a long one but I’m hoping someone will be able to spot where I’ve gone wrong. I am using the script below (which works with choose file) in a AppleScript run applet (which does not even reach the first do shell script command). I think the applet is exiting the routine when trying to coerce fileName to audioWav. I’ve spent over a day trying all kinds of syntax in an effort to get the app to run. I had a very long and inefficient (but working) app which I’ve also replaced the faster code line by line and still couldn’t get it working. Im at a dead end. Hope someone can help
Working AppleScript:
`use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions
property f0 : {}
property fileName : {}
tell application "Finder"
set volume alert volume 0
delete (every item of folder "HD:Users:me:Desktop:conv:" whose name extension is "csv")
delay 0.5
-- choose file, store original file path as property, duplicate file to "passing.wav" in desktop folder "conv" for analysis
set fileName to (choose file with prompt "Please Select a WAV file for Processing")
set audioWav to quoted form of POSIX path of fileName as text
-- generate CSV
do shell script "/Users/me/Library/Python/3.9/bin/crepe filename " & audioWav & space & "-v -V -c 'small' -s '10' -o ~/Desktop/conv/ "
set csv to (the first file of ¬
container (alias "HD:Users:me:Desktop:conv:") of ¬
application "Finder" whose name ends with "csv") as alias
set csv to POSIX path of file csv
set csv to quoted form of csv
set my_data to do shell script "cat " & csv & " | sort -t, --key=3,3 -gr | head -n8" `
`set csvData to paragraphs 2 thru -2 of my_data as list
set freqData to {}
set freqList to {15.9, 16.81, 17.81, 18.81, 19.91, 21.22, 22.48, 23.81, 25.23, 26.72, 28.31, 30.0, 31.79, 33.67, 35.67, 37.79, 40.41, 42.41, 44.94, 47.61, 50.44, 53.44, 56.61, 59.13, 62.66, 66.37, 70.32, 74.5, 78.92, 83.62, 88.59, 93.86, 99.44, 105.35, 111.61, 118.25, 125.28, 132.72, 140.62, 145.98, 157.83, 167.22, 177.16, 187.7, 198.86, 210.68, 223.21, 236.48, 250.54, 265.45, 281.22, 297.94, 315.67, 334.44, 354.32, 375.38, 397.71, 421.35, 446.41, 472.95, 501.07, 530.87, 562.44, 595.88, 631.31, 668.86, 708.63, 750.76, 795.4, 842.7, 892.81, 945.89, 1002.15, 1061.73, 1124.87, 1191.76, 1262.62, 1337.7, 1417.25, 1501.52, 1590.8, 1685.39, 1785.61, 1891.79, 2004.27, 2123.45, 2249.72, 2383.5, 2525.23, 2675.39, 2834.48, 3003.15, 3181.58, 3370.78, 3571.21, 3783.56, 4008.55, 4246.91, 4499.44, 4766.99, 5050.45, 5350.76, 5668.93, 6006.03, 6363.17, 6741.54, 7142.41, 7567.12}
set noteList to {"C0", "C#0", "D0", "D#0", "E0", "F0", "F#0", "G0", "G#0", "A0", "A#0", "B0", "C1", "C#1", "D1", "D#1", "E1", "F1", "F#1", "G1", "G#1", "A1", "A#1", "B1", "C2", "C#2", "D2", "D#2", "E2", "F2", "F#2", "G2", "G#2", "A2", "A#2", "B2", "C3", "C#3", "D3", "D#3", "E3", "F3", "F#3", "G3", "G#3", "A3", "A#3", "B3", "C4", "C#4", "D4", "D#4", "E4", "F4", "F#4", "G4", "G#4", "A4", "A4#", "B4", "C5", "C#5", "D5", "D#5", "E5", "F5", "F#5", "G5", "G#5", "A5", "A#5", "B5", "C6", "C#6", "D6", "D#6", "E6", "F6", "F#6", "G6", "G#6", "A6", "A#6", "B6", "C7", "C#7", "D7", "D#7", "E7", "F7", "F#7", "G7", "G#7", "A7", "A#7", "B7", "C8", "C#8", "D8", "D#8", "E8", "F8", "F#8", "G8", "G#8", "A8", "A#8", "B8"}
set oldDelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to ","
repeat with a_reading in csvData
set y to (text item 2 of a_reading)
set y to y as integer
repeat with xx from 1 to length of freqList
if y is greater than item xx of freqList then
else
set zz to item (xx - 13) of noteList -- for some reason there was an octave jump so had to -13 semitones
exit repeat
end if
end repeat
zz
set y to zz
set y to y as text
set end of freqData to y
end repeat
end tell
set L to freqData
-- find the most popular note in the list L and store it as property f0
set theList to {}
repeat 50 times
set theList to theList & items of L
end repeat`
`mostPopularItemInList(theList)
to mostPopularItemInList(x)
script accellerator
property theList : x
property originalItems : {}
property originalItemsCount : {}
on indexof(theItem, theList)
set text item delimiters to return
set theList to return & theList & return
set text item delimiters to {""}
try
-1 + (count (paragraphs of (text 1 thru (offset of (return & theItem & return) in theList) of theList)))
on error
0
end try
end indexof
to greaterInteger()
set largerItem to 0
set largerItemIndex to 0
repeat with i from 1 to count originalItemsCount
if originalItemsCount's item i > largerItem then
set largerItem to originalItemsCount's item i
set largerItemIndex to i
end if
end repeat
originalItems's item largerItemIndex
end greaterInteger
end script
repeat with i from 1 to count accellerator's theList
considering case
if accellerator's theList's item i is not in accellerator's originalItems then
set accellerator's originalItems's end to accellerator's theList's item i
set accellerator's originalItemsCount's end to 1
else
set ind to accellerator's indexof(accellerator's theList's item i, accellerator's originalItems)
set accellerator's originalItemsCount's item ind to ((accellerator's originalItemsCount's item ind) + 1)
end if
end considering
end repeat
set f0 to paragraphs 2 thru -2 of return & accellerator's greaterInteger() as text
end mostPopularItemInList
-- apply the most popular reading f0 to the original file fileName as a tag
tell application "Finder"
set fileName to fileName as text
set currentFile to (get POSIX path of file fileName)
tell currentFile
my tagCmd(it as text) -- else
end tell
end tell
on tagCmd(f)
do shell script "/usr/local/bin/tag -a " & "'" & f0 & "'" & space & quoted form of POSIX path of fileName -- "(get selection)" -- posix path convert path with colon to use in shell
end tagCmd
`Now here’s the code for the app…
`use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions
property f0 : {}
property fileName : {}
-- property pName : {}
property theFile : {}
property theList : {}
on prepareToProcessFiles(theFiles)
set progress description to "Processing Files"
set cFol to choose folder
tell application "Finder"
set theFiles to files of cFol
end tell
return theFiles
end prepareToProcessFiles
on processAFile(theFile)
tell application "Finder"
-- delete previous files
set volume alert volume 0
delete (every item of folder "HD:Users:me:Desktop:conv:" whose name extension is "csv")
delay 0.5
end tell
-- choose file, store original file path as property, duplicate file to "passing.wav" in desktop folder "conv" for analysis
set fileName to theFile -- (choose file with prompt "Please Select a WAV file for Processing")
tell application "System Events" to set nameOfFn to name of fileName
tell application "Finder"
set f to (the first file of ¬
container of fileName whose name is (nameOfFn) of ¬
application "Finder" whose name extension is "wav")
do shell script "/Users/md/Library/Python/3.9/bin/crepe filename " & (fileName)'s POSIX path's quoted form & space & "-v -V -c 'large' -s '10' -o ~/Desktop/conv/ "
set csv to (the first file of ¬
container (alias "HD:Users:me:Desktop:conv:") of ¬
application "Finder" whose name ends with "csv") as alias
set csv to POSIX path of file csv
set csv to quoted form of csv
set my_data to do shell script "cat " & csv & " | sort -t, --key=3,3 -gr | head -n8" -- set analTop to do shell script "cat ~/Desktop/conv/passing.f0.csv | head -n 34 | tail -n 32 | sort -t, --key=3,3 -gr | head -n8"
-- loop through column 2 of csv results and convert each frequency in hz to the closest f0 note
set csvData to paragraphs 2 thru -2 of my_data as list
set freqData to {}
set freqList to {15.9, 16.81, 17.81, 18.81, 19.91, 21.22, 22.48, 23.81, 25.23, 26.72, 28.31, 30.0, 31.79, 33.67, 35.67, 37.79, 40.41, 42.41, 44.94, 47.61, 50.44, 53.44, 56.61, 59.13, 62.66, 66.37, 70.32, 74.5, 78.92, 83.62, 88.59, 93.86, 99.44, 105.35, 111.61, 118.25, 125.28, 132.72, 140.62, 145.98, 157.83, 167.22, 177.16, 187.7, 198.86, 210.68, 223.21, 236.48, 250.54, 265.45, 281.22, 297.94, 315.67, 334.44, 354.32, 375.38, 397.71, 421.35, 446.41, 472.95, 501.07, 530.87, 562.44, 595.88, 631.31, 668.86, 708.63, 750.76, 795.4, 842.7, 892.81, 945.89, 1002.15, 1061.73, 1124.87, 1191.76, 1262.62, 1337.7, 1417.25, 1501.52, 1590.8, 1685.39, 1785.61, 1891.79, 2004.27, 2123.45, 2249.72, 2383.5, 2525.23, 2675.39, 2834.48, 3003.15, 3181.58, 3370.78, 3571.21, 3783.56, 4008.55, 4246.91, 4499.44, 4766.99, 5050.45, 5350.76, 5668.93, 6006.03, 6363.17, 6741.54, 7142.41, 7567.12}
set noteList to {"C0", "C#0", "D0", "D#0", "E0", "F0", "F#0", "G0", "G#0", "A0", "A#0", "B0", "C1", "C#1", "D1", "D#1", "E1", "F1", "F#1", "G1", "G#1", "A1", "A#1", "B1", "C2", "C#2", "D2", "D#2", "E2", "F2", "F#2", "G2", "G#2", "A2", "A#2", "B2", "C3", "C#3", "D3", "D#3", "E3", "F3", "F#3", "G3", "G#3", "A3", "A#3", "B3", "C4", "C#4", "D4", "D#4", "E4", "F4", "F#4", "G4", "G#4", "A4", "A4#", "B4", "C5", "C#5", "D5", "D#5", "E5", "F5", "F#5", "G5", "G#5", "A5", "A#5", "B5", "C6", "C#6", "D6", "D#6", "E6", "F6", "F#6", "G6", "G#6", "A6", "A#6", "B6", "C7", "C#7", "D7", "D#7", "E7", "F7", "F#7", "G7", "G#7", "A7", "A#7", "B7", "C8", "C#8", "D8", "D#8", "E8", "F8", "F#8", "G8", "G#8", "A8", "A#8", "B8"}
set oldDelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to ","
repeat with a_reading in csvData
set y to (text item 2 of a_reading)
set y to y as integer
repeat with xx from 1 to length of freqList
if y is greater than item xx of freqList then
else
set zz to item (xx - 13) of noteList -- for some reason there was an octave jump so had to -13 semitones
exit repeat
end if
end repeat
zz
set y to zz
set y to y as text
set end of freqData to y
end repeat
set L to freqData
end tell
set theList to {}
repeat 50 times
set theList to theList & items of L
end repeat
return theList
mostPopularItemInList(theList)
end processAFile
to mostPopularItemInList(x)
script accellerator
property theList : x
property originalItems : {}
property originalItemsCount : {}
on indexof(theItem, theList)
set text item delimiters to return
set theList to return & theList & return
set text item delimiters to {""}
try
-1 + (count (paragraphs of (text 1 thru (offset of (return & theItem & return) in theList) of theList)))
on error
0
end try
end indexof
to greaterInteger()
set largerItem to 0
set largerItemIndex to 0
repeat with i from 1 to count originalItemsCount
if originalItemsCount's item i > largerItem then
set largerItem to originalItemsCount's item i
set largerItemIndex to i
end if
end repeat
originalItems's item largerItemIndex
end greaterInteger
end script
repeat with i from 1 to count accellerator's theList
considering case
if accellerator's theList's item i is not in accellerator's originalItems then
set accellerator's originalItems's end to accellerator's theList's item i
set accellerator's originalItemsCount's end to 1
else
set ind to accellerator's indexof(accellerator's theList's item i, accellerator's originalItems)
set accellerator's originalItemsCount's item ind to ((accellerator's originalItemsCount's item ind) + 1)
end if
end considering
end repeat
set f0 to paragraphs 2 thru -2 of return & accellerator's greaterInteger() as text
end mostPopularItemInList
on tagCmd(f)
do shell script "/usr/local/bin/tag -a " & "'" & f0 & "'" & space & quoted form of POSIX path of fileName -- "(get selection)" -- posix path convert path with colon to use in shell
end tagCmd
on cleanup()
-- just clean up
end cleanup
on run theFiles
try
set progress total steps to -1
set progress completed steps to 0
set progress description to ""
set progress additional description to "Preparing files"
set theFiles to prepareToProcessFiles(theFiles)
-- Process Files...
set progress total steps to count of theFiles
set progress completed steps to 0
repeat with aFile in theFiles
try
tell application "System Events" to set theFileName to name of aFile
set progress additional description to "Analyse File “" & theFileName & "”..."
processAFile(contents of aFile)
set progress additional description to "Frequent Frequency “" & theFileName & "”..."
mostPopularItemInList(contents of theList)
tell application "Finder"
try
set fileName to fileName as text
set currentFile to (get POSIX path of file fileName) as list
try
tell currentFile
my tagCmd(it as text)
end tell
end try
end try
end tell
end try
set progress completed steps to progress completed steps + 1
end repeat
-- CLeanup...
set progress total steps to -1
set progress completed steps to 0
set progress additional description to "Cleaning up..."
cleanup()
on error errMsg number errNum from errFrom to errTo partial result errPartialResult
-- Cleanup...
set progress total steps to 0
set progress completed steps to 0
set progress additional description to "Cleaning up..."
cleanup()
error errMsg number errNum from errFrom to errTo partial result errPartialResult -- resignal the error
end try
end run
`
Sorry there’s so much to read, but hope someone can help.