How to add “”sort" and "create text file“” to an AppleScript creating a list of records

Hello,

Working in Mojave 10.14.6 latest update

The script below lists all my Evernote notebooks and sends the results to the clipboard.

How could I end up with:

  • a sorted list of notebooks ? I can’t give you the exact number of notebooks (in case you want to use some kind of counter) because it varies.
  • a text file in my ~/Downloads folder called Evernote Notebook list date+time ( ie date+time stamp).txt containing the sorted list.

thanks in advance for your time and help !


tell application "Evernote"
	set NotebookList to (name of every notebook)
	set AppleScript's text item delimiters to return
	set the clipboard to NotebookList as text
end tell

I’ve always used the sort routine from here.

https://www.macosxautomation.com/applescript/sbrt/sbrt-05.html

1 Like

thank you.
Would this template work if the notebook names are strings.
Example of one notebook name ‘forums automation AppleScript’

set the composer_list to {"Ellington", "Copland", "Bach", "Mozart"}
simple_sort(the composer_list)
--> returns: {"Bach", "Copland", "Ellington", "Mozart"}

Would you have a routine for create text file ? thanks

Although I generally find it easier to write in AppleScript than JavaScript for Automation (JXA), this is a situation where I think JavaScript is simpler, mainly because it has a built-in sort. The most tedious part of this script is creating the name for output file, because JavaScript (like AppleScript) doesn’t have a decent general-purpose date formatting function.

var currentApp = Application.currentApplication()
currentApp.includeStandardAdditions = true

// Change this to where you want the file saved
var folder = '/Users/username/Desktop/'

// Get the notebook names and sort them
var myNotebooks = Application("Evernote").notebooks.name()
myNotebooks.sort()
var outString = myNotebooks.join("\n")

// Make a file name in YYYY-MM-DD-hh-mm-ss format from the current date and time
var today = new Date()
var dateParts = [ today.getFullYear(),  today.getMonth(),  today.getDay(), today.getHours(), today.getMinutes(),  today.getSeconds() ]
dateParts = dateParts.map( n => n.toString() )
dateParts = dateParts.map( s => s.padStart(2, '0') )
var filePath = folder + dateParts.join('-') + '.txt'

// Write the notebook names to the file
var outFile = currentApp.openForAccess(filePath, {writePermission: true})
currentApp.write(outString, {to: outFile})
2 Likes

A beautiful script. Works Perfectly.

I just wanted to add 'Evernote Notebook List ’ to the time and date stamp, but JavaScript looks so cryptic that I don’t know where and how to insert the string.

thank you so much for all the work !!! It is a great help !!

1 Like

Just insert that text into the expression for filePath.

Also, I just realized JavaScript months are zero-based, not one-based, so I had to change one other part. Here’s the full script with both changes:

var currentApp = Application.currentApplication()
currentApp.includeStandardAdditions = true

// Change this to where you want the file saved
var folder = '/Users/username/Desktop/'

// Get the notebook names and sort them
var myNotebooks = Application("Evernote").notebooks.name()
myNotebooks.sort()
var outString = myNotebooks.join("\n")

// Make a file name from the date and time
var today = new Date()
var dateParts = [ today.getFullYear(), today.getMonth() + 1, today.getDay(), today.getHours(),  today.getMinutes(), today.getSeconds() ]
dateParts = dateParts.map( n => n.toString() )
dateParts = dateParts.map( s => s.padStart(2, '0') )
var filePath = folder + 'Evernote Notebook List ' + dateParts.join('-') + '.txt'

// Write the notebook names to the file
var outFile = currentApp.openForAccess(filePath, {writePermission: true})
currentApp.write(outString, {to: outFile})
2 Likes

Thanks very much for your help and your patience ! very kind of you !

Sorry to bother you again.
I just realized that the large caps are sorted first, then the small caps.
I would like the sorting to ignore caps
thank you

Usual way is to use localeCompare to drive a case insensitive sort function in JavaScript. See the sort section on this page in the Mozilla docs.

I am sorry: I am a beginner and it is difficult to understand.

Do you mean:

replace this

myNotebooks.sort()
var outString = myNotebooks.join("\n")

with this ?

myNotebooks.sort()
var outString = myNotebooks.join("\n")
var outString = ['réservé', 'Premier', 'Cliché', 'communiqué', 'café', 'Adieu'];
items.sort((a, b) => a.localeCompare(b, 'fr', {ignorePunctuation: true})); // ['Adieu', 'café', 'Cliché', 'communiqué', 'Premier', 'réservé']

thank you

What @sylumer means is you should use localeCompare in the argument to the sort in my code. Something like

myNotebooks.sort( (a, b) => a.localeCompare(b, 'en', {sensitivity: 'base'}) )

instead of the elementary

myNotebooks.sort()

You’ll have to choose the appropriate locale based on the language you use to name your notebooks.

1 Like

Now it’s clear. Thanks again very much !