Multipliers not allowing Multiple Selection

I have a script that requires multiple selections.Which were working until I wrote an instruction for the end results to be returned with multipliers. Since the new line was added the script will only process a single selection from the list.

I’d be extremely grateful for any input.



property twelfth_root_2 : (2 ^ (1 / 12))
set fRoot to text returned of (display dialog "Choose fundemental frequency in Hertz" default answer "")
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 PresetNumber to 1
repeat with EachPreset in presets
	if theMode as string = EachPreset as string then
		exit repeat
	else
		set PresetNumber to PresetNumber
	end if
end repeat


set semitones to PresetNumber 

set frequencies to {}

set frequency to fRoot * (twelfth_root_2 ^ semitones)
set frequency to (round (frequency * 100)) / 100
set end of frequencies to frequency -- or to frequency as text.

set frequencies to {frequencies & frequencies / 2, frequencies * 2, frequencies * 4, frequencies * 8, frequencies * 16, frequencies * 32, frequencies * 64}

return frequencies

dooglesnak. The repeat loop in your script has two issues of note.

First, as written, the repeat loop doesn’t appear to do anything. If a match is found, the repeat loop is exited and PresetNumber remains set to 1. On the other hand, if a match is not found, PresetNumber is set to 1. Perhaps the first line below from the repeat loop should be the second line below:

set PresetNumber to PresetNumber
set PresetNumber to (PresetNumber + 1)

Second, a choose-from-list dialog returns a list. Coercing that list to a string will work in the context of your script only if one dialog item is selected but not if two or more items are selected. Thus, coercing {“b9”} to a string returns “b9” but coercing {“b9”, “9th”} to a string returns “b99th”. If multiple selections are allowed in the choose-from-list dialog, an additional repeat loop may be necessary (depending on how you want it to work).

Hi Peavine,

Thanks for taking the time to have a look. Adding +1 didnt seem do anything apart from change the count of semitones (of which only 12 exist) from 1 to 13. Any idea where i’d apply a repeat loop and how id do it? Perhaps someone could suggest a good resources where I could learn. Im very eager to complete this project so i can apply it to my workflow, but I’ve only been learning to code for a few weeks and have lots to learn

dooglesnak. If the user selects, say, b9 and m3 from the dialog, what do you want PresetNumber to be set to? The alternatives would appear to be 1 (because b9 is the first preset), or 3 (because m3 is the third preset), or {1,3} (which returns the postion of both of the selected items.

With the above information, the repeat loop can easily be edited to work properly and then any additional issues can be dealt with.

yep they go up in value from left to right (1-12). I can select multiple items if i dont have the multipliers on the penultimate line.

If the selection were b9, m3, & 5 i would like 1, 3, 7 to be returned

dooglesnak. I’ve modified the repeat loop to do what you want. There appeared to be no need for PresetNumber, so I set semitones directly in the repeat loop. Using your example, the script will set semitones to {1, 3, 7}, which as you probably know is a list of three integers.

property twelfth_root_2 : (2 ^ (1 / 12))
set fRoot to text returned of (display dialog "Choose fundemental frequency in Hertz" default answer "")
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 semitones to {}
repeat with anItem in theMode
	repeat with i from 1 to (count presets)
		if (item i of presets) = (contents of anItem) then
			set end of semitones to i
			exit repeat
		end if
	end repeat
end repeat

set frequencies to {}
set frequency to fRoot * (twelfth_root_2 ^ semitones)
set frequency to (round (frequency * 100)) / 100
set end of frequencies to frequency -- or to frequency as text.
set frequencies to {frequencies & frequencies / 2, frequencies * 2, frequencies * 4, frequencies * 8, frequencies * 16, frequencies * 32, frequencies * 64}
return frequencies

Since semitones is a list, the following line will error, and you need to decide how to handle this. If you need assistance with this, please provide specific examples (as you did above).

set frequency to fRoot * (twelfth_root_2 ^ semitones)

Hey Peavine,

Thanks very much for your help. It works with one item from the list selected , but with more than one script debugger returns the error: Can’t make {2, 5, 8} into type number.

Hi.

If the idea’s to get the selected intervals’ pitch frequencies with a range of octaves for each, my guess would be something like this:

property twelfth_root_2 : (2 ^ (1 / 12))
set fRoot to text returned of (display dialog "Choose fundemental frequency in Hertz" default answer "")
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 semitoneList to {}
repeat with anItem in theMode
	repeat with i from 1 to (count presets)
		if (item i of presets) = (contents of anItem) then
			set end of semitoneList to i
			exit repeat
		end if
	end repeat
end repeat

set frequencies to {}
repeat with semitones in semitoneList
	set frequency to fRoot * (twelfth_root_2 ^ semitones)
	set frequency to (round (frequency * 100)) / 100
	set end of frequencies to {frequency, frequency / 2, frequency * 2, frequency * 4, frequency * 8, frequency * 16, frequency * 32, frequency * 64}
end repeat

return frequencies

… which can be reduced to:

property twelfth_root_2 : (2 ^ (1 / 12))

set fRoot to text returned of (display dialog "Choose fundamental frequency in Hertz" default answer "")
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) * 100)) / 100
		set end of frequencies to ¬
			{frequency, frequency / 2, frequency * 2, frequency * 4, frequency * 8, frequency * 16, frequency * 32, frequency * 64}
	end if
end repeat

return frequencies

Spot on, thats amazing thank you so much. Now I can start building the database. Thank you very much for your help…

Im finding it tough with applescript, (unless ive overlooked a bunch of resources) it seems theres not a great deal of info out there. So when you hit a dead end, there arent many options for finding a solution, it gets exasperating when you have to resort to hour/dayss of just guessing .

Thank you