Sunday, January 23, 2022

#1 2022-01-05 07:23:51 am

dooglesnak
Member
Registered: 2021-11-27
Posts: 55

Can't do shell script with a repeat with i from 1 to n loop

With multiple selections allowed, say i choose "3" options from the list, the value of the variable im trying to loop, instead of returning the value of the 3 items  its just returning the number three.. then the shell script is looking for all file names with the number 3 in it.

Ive tried so many different things and have been stuck n this for about a week and cant work it out.

Hopefully someone here might be able to offer some advice.

Thank you in advance.

Doug


Applescript:



use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions
use script "Metadata Lib" version "2.0.0"
use mdLib : script "Metadata Lib" version "2.0"
property twelfth_root_2 : (2 ^ (1 / 12))


tell application "Finder"
   delete (every item of folder "Mojave:Users:dh:Music:Samples:EXS24:ESX242:autocrop:Term:")
end tell




display dialog "Choose fundemental frequency in Hertz" default answer ""

set fRoot to text returned of result

set presets to {"b9", "9th", "m3", "3rd", "4th", "Dim5", "5th", "Aug5", "6th", "7th", "m7", "Oct"}





set theMode to (choose from list presets with prompt "Choose a character:" with multiple selections allowed)
if (theMode is false) then return -- user cancelled

set frequencies to {}
repeat with semitones from 1 to (count presets)
   if (theMode contains {item semitones of presets}) then
       set frequency to (round (fRoot * (twelfth_root_2 ^ semitones)))
       set end of frequencies to ¬
           {((round ((frequency / 256) / 5)) * 5), ((round ((frequency / 128) / 5)) * 5), ((round ((frequency / 64) / 5)) * 5), ((round ((frequency / 32) / 5)) * 5), ((round ((frequency / 16) / 5)) * 5), ((round ((frequency / 8) / 5)) * 5), ((round ((frequency / 4) / 5)) * 5), ((round ((frequency / 2) / 5)) * 5), ((round (frequency / 5)) * 5), ((round ((frequency * 2) / 5)) * 5), ((round ((frequency * 4) / 5)) * 5), ((round ((frequency * 8) / 5)) * 5), ((round ((frequency * 16) / 5)) * 5), ((round ((frequency * 32) / 5)) * 5), ((round ((frequency * 64) / 5)) * 5), ((round ((frequency * 128) / 5)) * 5), ((round ((frequency * 256) / 5)) * 5), ((round ((frequency * 512) / 5)) * 5)}
       
       
       
   end if
end repeat





set TheSearchPath to quoted form of POSIX path of "/Users/dh/Music/Samples/EXS24/ESX242/autocrop/Data"
set TheDestPath to quoted form of POSIX path of "/Users/dh/Music/Samples/EXS24/ESX242/autocrop/Term"

set aValue to ((current application's class "NSArray"'s arrayWithArray:(frequencies))'s valueForKeyPath:("@unionOfArrays.self")) as list


repeat with aValue from 1 to (count of frequencies) as text
   
   set theSearch to aValue
   
   set theResult to paragraphs of (do shell script "mdfind -onlyin " & TheSearchPath & ¬
       " 'kMDItemFSName == \"*" & theSearch & "." & "0" & "Hz" & "*\"'") ¬
       
end repeat
repeat with i from 1 to count of theResult
   if (count of theResult) is 1 and item 1 of theResult is "" then exit repeat --no matches
   
   set the PosixFile to item i of theResult
   try
       do shell script "cp -f " & quoted form of PosixFile & space & TheDestPath
   end try
   
end repeat

Offline

 

#2 2022-01-06 07:05:40 am

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 5474

Re: Can't do shell script with a repeat with i from 1 to n loop

Hi Doug. The script's a bit of a mess.  hmm

It contains two, consecutive commands to use Shane's "Metadata Lib" library, but has nothing which requires it.

Then halfway down:

Applescript:

set TheSearchPath to quoted form of POSIX path of "/Users/dh/Music/Samples/EXS24/ESX242/autocrop/Data"
set TheDestPath to quoted form of POSIX path of "/Users/dh/Music/Samples/EXS24/ESX242/autocrop/Term"

set aValue to ((current application's class "NSArray"'s arrayWithArray:(frequencies))'s valueForKeyPath:("@unionOfArrays.self")) as list


repeat with aValue from 1 to (count of frequencies) as text
   
   set theSearch to aValue
   
   set theResult to paragraphs of (do shell script "mdfind -onlyin " & TheSearchPath & ¬
       " 'kMDItemFSName == \"*" & theSearch & "." & "0" & "Hz" & "*\"'") ¬
       
end repeat


The two paths above are already POSIX paths.

'aValue' is set to the list result of the "unionOfArrays" line and is then immediately changed to the loop counter for the repeat which follows. In that repeat, 'theSearch' is set to the loop counter and is used as part of the search term in the shell script.

There's no point in coercing (count of frequencies) to text. The count is a number and that's what's needed.

Each time round the repeat, 'theResult' is set to the paragraphs of the shell script result. But then when the repeat's finished, there's another repeat which loops through 'theResult':

Applescript:

repeat with i from 1 to count of theResult
   if (count of theResult) is 1 and item 1 of theResult is "" then exit repeat --no matches
   
   set the PosixFile to item i of theResult
   try
       do shell script "cp -f " & quoted form of PosixFile & space & TheDestPath
   end try
   
end repeat


Since this is placed after the 'aValue' repeat, it only loops through the value 'theResult' received on the last iteration of the 'aValue' repeat.


NG

Offline

 

#3 2022-01-06 11:26:44 am

dooglesnak
Member
Registered: 2021-11-27
Posts: 55

Re: Can't do shell script with a repeat with i from 1 to n loop

Thank you for your response Nigel, (I was using the meta lib before and didnt take it out.)

Would it be better to do something like this (using the line "shell script "echo '.f0" & space & "- " & space & (item i of argv)" to cross reference the CSV search path using the mdfind command whose filenames containing frequency readings all start with ".f0 - " 520 ".0" Hz?


Applescript:


set theMode to (choose from list presets with prompt "Choose a character:" with multiple selections allowed)
if (theMode is false) then return -- user cancelled

set frequencies to {}
repeat with semitones from 1 to (count presets)
   if (theMode contains {item semitones of presets}) then
       set frequency to (round (fRoot * (twelfth_root_2 ^ semitones)))
       set end of frequencies to ¬
           {((round ((frequency / 256) / 5)) * 5), ((round ((frequency / 128) / 5)) * 5), ((round ((frequency / 64) / 5)) * 5), ((round ((frequency / 32) / 5)) * 5), ((round ((frequency / 16) / 5)) * 5), ((round ((frequency / 8) / 5)) * 5), ((round ((frequency / 4) / 5)) * 5), ((round ((frequency / 2) / 5)) * 5), ((round (frequency / 5)) * 5), ((round ((frequency * 2) / 5)) * 5), ((round ((frequency * 4) / 5)) * 5), ((round ((frequency * 8) / 5)) * 5), ((round ((frequency * 16) / 5)) * 5), ((round ((frequency * 32) / 5)) * 5), ((round ((frequency * 64) / 5)) * 5), ((round ((frequency * 128) / 5)) * 5), ((round ((frequency * 256) / 5)) * 5), ((round ((frequency * 512) / 5)) * 5)}
       
       
       
   end if
end repeat





set TheSearchPath to quoted form of "/Users/dh/Music/Samples/EXS24/ESX242/autocrop/Data"
set TheDestPath to quoted form of "/Users/dh/Music/Samples/EXS24/ESX242/autocrop/Term"

set argv to ((current application's class "NSArray"'s arrayWithArray:(frequencies))'s valueForKeyPath:("@unionOfArrays.self")) as list


set accumulator to do shell script "mdfind " & {argv} & " arguments" without altering line endings
repeat with i from 1 to (count argv)
   set ln to do shell script "echo '.f0" & space & "- " & space & (item i of argv) & "." & "'" without altering line endings
   set accumulator to accumulator & ln
end repeat
return accumulator

Last edited by dooglesnak (2022-01-06 11:28:12 am)

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)