Skip to content

Instantly share code, notes, and snippets.

@Zodiase
Last active February 26, 2024 09:57
Show Gist options
  • Save Zodiase/76b6f69666604a5e56b8d92f5a61c925 to your computer and use it in GitHub Desktop.
Save Zodiase/76b6f69666604a5e56b8d92f5a61c925 to your computer and use it in GitHub Desktop.
Folder Action: Save Discover Statements to Evernote
# Expect input to be a list of file alias.
on run {aliasList, parameters}
set fileInfoList to {}
repeat with aliasItem in aliasList
set thePath to POSIX path of aliasItem
set theName to name of (info for aliasItem)
set end of fileInfoList to {thePath:thePath, theName:theName}
end repeat
return fileInfoList
end run
function parseFilename(filename) {
var result = {};
var filenameBreakdown = filename.split('-');
if (!(filenameBreakdown[0] === 'Discover' && filenameBreakdown[1] === 'Statement')) {
return null;
}
var dateString = filenameBreakdown[2];
var year = parseInt(dateString.substr(0, 4), 10);
var month = parseInt(dateString.substr(4, 2), 10);
var day = parseInt(dateString.substr(6, 2), 10);
result.date = new Date(year, month - 1, day);
result.noteName = 'Discover ' + dateString;
result.tags = [
'Discover',
];
return result;
}
function run(fileInfoList, parameters) {
var EvernoteScripts = Library("Evernote");
var processedFiles = [];
for (fileInfoItem of fileInfoList) {
var itemInfo = parseFilename(fileInfoItem.theName);
if (itemInfo === null) {
continue;
}
fileInfoItem.isSaved = EvernoteScripts.saveStatementToEvernote(
itemInfo.noteName,
fileInfoItem.thePath,
itemInfo.tags,
itemInfo.date
);
processedFiles.push(fileInfoItem);
}
return processedFiles;
}
on run {fileInfoList, parameters}
set theFiles to {}
repeat with fileInfoItem in fileInfoList
set end of theFiles to POSIX file (thePath of fileInfoItem) as alias
end repeat
return theFiles
end run
# theTitle should be a string.
# theStatementFilePath should be a POSIX path.
# theTags should be an array.
# theDate should be a Date object.
to saveStatementToEvernote(theTitle, theStatementFilePath, theTags, theDate)
set TargetNoteBookName to "Finance"
set theStatementFile to POSIX file theStatementFilePath
tell application "Evernote"
set targetNoteBook to missing value
set targetNote to missing value
repeat until isSynchronizing is false
-- wait until sync is done
end repeat
-- create the target notebook if not found
if (not (notebook named TargetNoteBookName exists)) then
make notebook with properties {name:TargetNoteBookName}
end if
set targetNoteBook to notebook named TargetNoteBookName
set notesWithSameTitle to find notes "notebook:\"" & TargetNoteBookName & "\" \"" & theTitle & "\""
if (length of notesWithSameTitle is not 0) then
return false
end if
set targetNote to create note from file theStatementFile title theTitle notebook targetNoteBook tags theTags created theDate
assign tag "Bank Statement" to targetNote
synchronize
return true
end tell
end saveStatementToEvernote
@anthonybrown
Copy link

I didn’t know Evernote had a scripting language

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment