AppleScript in Rule Not Working Consistently

Get help. Get answers. Let others lend you a hand.

Moderator: Mr_Noodle

AppleScript in Rule Not Working Consistently Sat May 16, 2020 11:25 am • by Ender2k
I have a rule that monitors my downloads folder for continuing education certificates and appends them to an Evernote note for that event. It was working the other day, and now it keeps failing with error:

Can’t get item 1 of {}.


The rule is:
Image

The file is named correctly:
Image

The script that is running is:
Code: Select all
(*
This version works from a Hazel rule.

Pass serial number from Hazel to AppleScript. Comes from here: https://www.noodlesoft.com/forums/viewtopic.php?t=1770&p=7294
*)

tell application "Finder"
   set myHazelTokenDelimiters to "|"
   set theListOfCustomTokens to name of theFile
   set AppleScript's text item delimiters to {myHazelTokenDelimiters}
   -- Now, the tokens are available in theListOfCustomTokens as items 1,2,3 etc...
   set noteSerial to text item 2 of theListOfCustomTokens
   
   -- set newFilename to "CE Certificate " & noteSerial & ".pdf"
   -- set name of theFile to newFilename
   set fileURL to POSIX path of theFile
end tell

(*
Based on: https://colterreed.com/how-to-append-to-evernote-notes-with-applescript/
Appends an entry to today’s Daily Record of Events in Evernote.
For best results, invoke the script from Keyboard Maestro or LaunchBar.
*)

tell application "Evernote"
   set myNotebook to "\"Work Notes\"" -- set which notebook to search here
   set searchString to "notebook:" & myNotebook & " " & noteSerial -- create an Evernote query string. See here: https://dev.evernote.com/doc/articles/search_grammar.php#Search_Terms
   set matches to find notes searchString -- find note that matches both variables
   set myNote to item 1 of matches -- this only works if there is only 1 match
   open note window with myNote
   -- add some tags (in progress)
   
   
   --Append to the selected note
   tell myNote to append attachment fileURL
   tell myNote to append text fileURL
end tell


Any help is much appreciated!
Ender2k
 
Posts: 2
Joined: Sat May 16, 2020 11:17 am

Try the script outside of Hazel. My guess is that it's failing because there are no matches. It doesn't appear to be an issue on Hazel's side.
Mr_Noodle
Site Admin
 
Posts: 11236
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Thanks, that helped me troubleshoot it. The problem was in this line:
Code: Select all
set noteSerial to text item 2 of theListOfCustomTokens


It was looking for text, but I hade named my file incorrectly as
Code: Select all
CE Certificate |20200515001|.pdf
when it should have been named
Code: Select all
CE Certificate |CE20200515001|.pdf
Once I fixed that the script ran correctly. Doh! :lol:
Ender2k
 
Posts: 2
Joined: Sat May 16, 2020 11:17 am


Return to Support