Skip to content

Instantly share code, notes, and snippets.

@JMichaelTX
Last active December 21, 2020 15:02
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JMichaelTX/b27c829d94797462a597913d6e1e195f to your computer and use it in GitHub Desktop.
Save JMichaelTX/b27c829d94797462a597913d6e1e195f to your computer and use it in GitHub Desktop.
• Import Finder Selected Files to Evernote Mac • User will be prompted for Tags
property ptyScriptName : "Import Selected Files Selected in Finder into Evernote with Tags"
property ptyScriptVer : "3.1" -- Add support for Tag Input
property ptyScriptDate : "2018-10-12"
property ptyScriptAuthor : "JMichaelTX"
property version : ptyScriptVer
property LF : linefeed
property ptyENicon : path to resource "Evernote.icns" in bundle (path to application "Evernote")
(*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
PURPOSE/METHOD:
• Import Finder Selected Files to Evernote
• User will be prompted for Tags
REQUIRED:
1. macOS 10.11.6+
2. Mac Applications
INSTALLATION: See http://tinyurl.com/install-as
EXAMPLE RESULTS:
• https://www.dropbox.com/s/i02cro2b0zder96/EN-Import-File-Results.png?dl=0
REF: The following were used in some way in the writing of this script.
1. http://veritrope.com/tech/evernote-desktop-folder/
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*)
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
(*
======================================
// USER SWITCHES (YOU CAN CHANGE THESE!)
======================================
*)
-- ASK FOR TAGS --
-- "ON" - Prompt User for Tags
-- "OFF" - Do NOT prompt for Tags. Use Default Tags below, if any.
property ptyAskForTags : "ON"
-- MOVE TO MAC TRASH AFTER IMPORT
-- Set to "OFF" to keep in Finder Source folder
property ptyMoveToTrash : "OFF"
--- Display Detailed Notifications ---
property ptyNotify : false
-- BODY OF NOTE IN HTML CODE **ADD [JMU.1] ***
property theBody : ""
--- Evernote Notebook ---
property ptyEN_NB_Name : "" -- Empty NB means file to be imported to your EN Default NB
--- DEFAULT Tag List ---
-- Tag will be created if it does NOT exist
property ptyEN_TagList : {"EN.Import"} -- Add default Tags here inside the curly brackets, separated by commas.
--- HEADER FOR RESULTS REPORT ---
set scriptResults to "
SOURCE DATE --- FILE NAME ---
"
-------------------------------
-- GET FINDER FILE SELECTION --
-------------------------------
set {fnames, fexts, fdates, fppaths} to {{}, {}, {}, {}}
tell application "Finder"
set fileList to (get selection)
repeat with oFile in fileList
tell oFile
set end of fnames to name
set end of fexts to name extension
set end of fdates to creation date
end tell
set end of fppaths to POSIX path of (oFile as alias)
end repeat
end tell
set numFiles to count of fileList
set filePural to " file"
if (numFiles > 1) then set filePural to " files"
set nbName to ptyEN_NB_Name
if (nbName = "") then set nbName to "<Your Default NB>"
set msgStr to "BEGIN Import to Evernote for " & numFiles & filePural & ¬
return & "into Notebook: " & nbName
if ptyNotify then
tell application "System Events"
say msgStr
display notification (msgStr) with title ptyScriptName
end tell
end if
-------------------------------------------------
--————— PROCESS SELECTED FILES ——————
-------------------------------------------------
tell application "Evernote"
if ptyAskForTags is "ON" then set ptyEN_TagList to my getTagsFromUser(ptyEN_TagList, msgStr)
-------------------------------------------------
repeat with iFile from 1 to numFiles
-----------------------------------------------
-- GET FILE INFO FROM LISTS --
-- fnames, fexts, fdates, fppaths
set creationDate to item iFile of fdates
set file_name to item iFile of fnames
set file_ext to item iFile of fexts
set posixFile to item iFile of fppaths
set lsNoteTitle to file_name
if (file_ext "") then set lsNoteTitle to text 1 thru -((count file_ext) + 2) of file_name
set strImportDate to text 1 thru 10 of ((current date) as «class isot» as string)
set strCreateDate to text 1 thru 10 of ((creationDate) as «class isot» as string)
-- BUILD HTML CODE FOR TOP OF NOTE --
set theBody to "<div><B>File: " & file_name & "</B></div>
<div>Source Date:&nbsp;&nbsp;" & strCreateDate & "&nbsp;&nbsp;&nbsp;&nbsp;Imported:&nbsp;&nbsp;" & strImportDate & "</div><div><hr></div><div><br></div>"
-------------------------------------------------
-- CREATE EVERNOTE NOTE WITH FILE ATTACHED --
-------------------------------------------------
-- if/else Tree Required to Handle Different Data being used with create note
--- NO NB OR TAGS ---
if ((ptyEN_NB_Name is "") and (ptyEN_TagList is {})) then -- NO NB or Tags
create note title lsNoteTitle with html theBody created creationDate attachments posixFile
--- TAGS ONLY ---
else if ((ptyEN_NB_Name is "") and (ptyEN_TagList is not {})) then -- Tags only
create note title lsNoteTitle with html theBody tags ptyEN_TagList created creationDate attachments posixFile
--- NB ONLY ---
else if ((ptyEN_NB_Name is not "") and (ptyEN_TagList is {})) then -- NB only
create note title lsNoteTitle with html theBody notebook ptyEN_NB_Name created creationDate attachments posixFile
--- NB & TAGS ---
else if ((ptyEN_NB_Name is not "") and (ptyEN_TagList is not {})) then -- NB & Tags
create note title lsNoteTitle with html theBody notebook ptyEN_NB_Name tags ptyEN_TagList created creationDate attachments posixFile
end if -- create note based on NB and Tags
---------------- END CREATE NOTE SECTION -----------------------
set scriptResults to scriptResults & strCreateDate & tab & file_name & LF
-- MOVE IMPORTED FILE TO MAC TRASH --
if ptyMoveToTrash is "ON" then
set fileAlias to (POSIX file posixFile) as alias
tell application "Finder" to move fileAlias to trash
end if
end repeat -- iFile in fileList
activate
set msgStr to "Import to Evernote COMPLETED for " & numFiles & filePural
display notification (msgStr) with title ptyScriptName sound name "default"
display dialog scriptResults with title "Files Imported to Evernote" buttons {"OK"} default button "OK" with icon ptyENicon
end tell -- "Evernote"
return scriptResults
--~~~~~~~~~~~~~~~~~~~ END OF MAIN SCRIPT ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
on getTagsFromUser(pDefaultTagList, pMsgHeader)
set oldDelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to ","
set textTagList to pDefaultTagList as text
try
set dialogresult to display dialog (pMsgHeader & return & return & ¬
"Please Enter Your Tags Below:
• Multiple Tags Separated By Commas
• Tags Will be Created if they do NOT Exist") with title ptyScriptName default answer textTagList buttons {"Cancel", "OK"} default button "OK" cancel button ¬
"Cancel" with icon ptyENicon
set textTagList to text returned of dialogresult
set ButtonSel to button returned of dialogresult
on error number -128
error number -128
end try
--- Convert Text Tag List to AS Tag List ---
set oldDelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to ","
set tagList to text items of textTagList
set AppleScript's text item delimiters to oldDelims
return tagList
end getTagsFromUser
-- SORT SUBROUTINE
on simple_sort(my_list)
set the index_list to {}
set the sorted_list to {}
repeat (the number of items in my_list) times
set the low_item to ""
repeat with i from 1 to (number of items in my_list)
if i is not in the index_list then
set this_item to item i of my_list as text
if the low_item is "" then
set the low_item to this_item
set the low_item_index to i
else if this_item comes before the low_item then
set the low_item to this_item
set the low_item_index to i
end if
end if
end repeat
set the end of sorted_list to the low_item
set the end of the index_list to the low_item_index
end repeat
return the sorted_list
end simple_sort
@JMichaelTX
Copy link
Author

Please post all comments in the below Evernote Forum thread. The Github Gist does NOT provide any notification of comments that are posted.

EN Mac Import Files Using AppleScript

@JMichaelTX
Copy link
Author

TEST for new Gist Notifications as of 2019-05-08

@ApolloBiz
Copy link

TEST Comment #2 by another user. 2019-05-08 14:51 GMT-5

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