Insert evernote notebook list into HTML dropdown field

I’ve created an HTML prompt to help create evernote notes and I’d like to choose the notebook to save to with a dropdown input field.

I’ve got the list of notebooks pulled, but I’m not sure how to alphabetize or whether the format is right for passing to an HTML prompt. Does anyone have advice on how to either format or alphabetize?

set allNotebooks to every notebook
repeat with currentNotebook in allNotebooks

	log name of currentNotebook
end repeat
allNotebooks
tell application "Evernote"
	set theNotebooks to name of notebooks

	-- Sort the items

	set AppleScript's text item delimiters to {ASCII character 10}
	do shell script "echo " & quoted form of (theNotebooks as text) & " | /usr/bin/sort"
	set sortedList to paragraphs of result


	-- create html option list

	set optionList to ""

	repeat with n from 1 to (count of sortedList)
	
		set optionList to optionList & "<option value=\"" & item n of sortedList & "\">" & item n of sortedList & "</option>" & (return)
	
	end repeat
	-- Return the results, save this to a variable
		return optionList

end tell

The above would then be included in the HTML select tag to give you the drop down, via the usual KM methods.

This can be done somewhat more efficiently using JXA (no need to go back to the shell to sort), but this should get the job done. Only issues that you might see is if you have special characters in your notebook names, or characters reserved for HTML, they may cause trouble in your markup. If so, you’ll need to encode/escape those characters as part of this process.

This is great. Thanks! I’m not sure what the usual KM methods are for inserting the result into an HTML select tag. Would I get the variable with javascript?

Exactly.

window.KeyboardMaestro.GetVariable( ‘KMVariableName’ )

That did the trick :slight_smile: