Can someone help me with this folder action script?

Hey guys, still a newbie, but I am learning, I swear!

I found this awesome folder action script for Evernote, and it works great. I use it all the time.

Now I was trying to figure out how to adjust it for my automator workflow that I am working on.

What I want it to do is NOT prompt me to choose the notebook or tags. All I want it to do when I add a file to the folder is to automatically add it to one of my notebooks (“Changes Log”). For instance if I drop a text file into the folder, then what I want it to do is automatically file it in Evernote in my notebook called “Changes Log”. That’s all.

How do I got about doing this?


property theNum : "0"
property EVnotebook : ""
property thename : ""
property theNotes : ""
property theBody : ""
property theURL : ""

on adding folder items to this_folder after receiving added_items
	delay 5
	
	(*MAIN PROGRAM *)
	tell application "Evernote"
		--set successCount to 0
		set defaultTag to "@Evernote"
		display dialog "" & ¬
			"Enter Your Tags (Separated By Colons or Commas)" with title "Evernote Import" default answer defaultTag buttons {"Create in Default Notebook", "Select Notebook from List", "Cancel"} default button "Create in Default Notebook" cancel button ¬
			"Cancel" with icon path to resource "Evernote.icns" in bundle (path to application "Evernote")
		set dialogresult to the result
		set userInput to text returned of dialogresult
		set ButtonSel to button returned of dialogresult
		set theDelims to {":", ","}
		set EVTag to my Tag_List(userInput, theDelims)
		
		if ButtonSel is "Select Notebook from List" then set EVnotebook to my Notebook_List()
		
		repeat with i from 1 to number of items in added_items
			set new_item to item i of added_items
			create note from file new_item notebook EVnotebook tags EVTag
			(*DELETE THE TEMP FILE/FOLDER *)
		end repeat
	end tell
	
	tell application "Finder" to delete added_items
end adding folder items to

(* SUBROUTINES *)
--TAG SELECTION SUBROUTINE
on Tag_List(userInput, theDelims)
	set oldDelims to AppleScript's text item delimiters
	set theList to {userInput}
	repeat with aDelim in theDelims
		set AppleScript's text item delimiters to aDelim
		set newList to {}
		repeat with anItem in theList
			set newList to newList & text items of anItem
		end repeat
		set theList to newList
	end repeat
	return theList
	set AppleScript's text item delimiters to oldDelims
end Tag_List
--EVERNOTE NOTEBOOK SELECTION SUBROUTINE 
on Notebook_List()
	tell application "Evernote"
		activate
		set listOfNotebooks to {} (*PREPARE TO GET EVERNOTE'S LIST OF NOTEBOOKS *)
		set EVNotebooks to every notebook (*GET THE NOTEBOOK LIST *)
		repeat with currentNotebook in EVNotebooks
			set currentNotebookName to (the name of currentNotebook)
			copy currentNotebookName to the end of listOfNotebooks
		end repeat
		set Folders_sorted to my simple_sort(listOfNotebooks) (*SORT THE LIST *)
		set SelNotebook to choose from list of Folders_sorted with title "Select Evernote Notebook" with prompt ¬
			"Current Evernote Notebooks" OK button name "OK" cancel button name "New Notebook" (*USER SELECTION FROM NOTEBOOK LIST *)
		if (SelNotebook is false) then (*CREATE NEW NOTEBOOK OPTION *)
			set userInput to ¬
				text returned of (display dialog "Enter New Notebook Name:" default answer "")
			set EVnotebook to userInput
		else
			set EVnotebook to item 1 of SelNotebook
		end if
	end tell
end Notebook_List

--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



here is the tip which I use to test folder action scripts :


set added_items to choose file
set this_folder to "path:to:the:target:folder" as alias

--on adding folder items to this_folder after receiving added_items

-- the code

--end adding folder items to

With that, I may study what’s returned in the log report.

Yvan KOENIG (VALLAURIS, France) vendredi 14 décembre 2012 23:32:32

Hello, and hello Yvan!

That was a totally nice debugging techique! C’est Magnifique!

There is no log report here, I can’t even compile it as I don’t have Evernote! I stil have good hopes, sotry and see if it works! :slight_smile:


property theNum : "0"
property EVnotebook : "Changes Log"
property thename : ""
property theNotes : ""
property theBody : ""
property theURL : ""
# What I want it to do is NOT prompt me to choose the notebook or tags.
# All I want it to do when I add a file to the folder is to automatically add it to one of my 
# notebooks ("Changes Log"). 

on adding folder items to this_folder after receiving added_items
   delay 5
   
   (*MAIN PROGRAM *)
   tell application "Evernote"
       --set successCount to 0
       set defaultTag to "@Evernote" -- * Uses this tag
       repeat with i from 1 to number of items in added_items
           set new_item to item i of added_items
           create note from file new_item notebook EVnotebook tags defaultTag
           (*DELETE THE TEMP FILE/FOLDER *)
       end repeat
   end tell
   
   tell application "Finder" to delete added_items
end adding folder items to


McUsrII and Yvon!

Thanks a lot! It works like a gem.

Phil

Thank you both of you.

I don’t own/use Evernote so I wa unable to test.
No need for an expensive third party product to debug.
I use the posted tip for years when a folder action script doesn’t behave the wanted way or worse when it doesn’t work.

Yvan KOENIG (VALLAURIS, France) samedi 15 décembre 2012 09:55:32

I had forgotten about that tip, I think I saw it years ago, I haven’t used folder action that much lately. It is a very nice way to debug a folder action! :slight_smile:

I usually test it in Script Debugger though, but that can’t compare to running it stand-alone like you do! :slight_smile:

Now that I am retired, 99% of what I do with my machine is to try to help other users (my brain jogging).
I can’t afford script debugger for that :frowning:

Yvan KOENIG (VALLAURIS, France) samedi 15 décembre 2012 14:45:51

Hello Yvan.

Have you tried to use XCode for debugging?

It used to be pretty good for debugging .applescript files, I can’t remember exactly how I did it, but I used it for AppleScript Studio Projects.

I think it should be easy to set it up for debugging regular AppleScript files Maybe you’ll have to start out with an Asoc project, but I think it should be possible.

What you won’t get, is the Explorer part of Script Debugger, that lets you go in and examine the values in its properties, and the available properties.

No, it seems too complex for me :frowning:

Yvan KOENIG (VALLAURIS, France) samedi 15 décembre 2012 19:07:23

Too complex for you, yeah right! If you really don’t want to, just say so. :smiley:

I am pretty sure that if you create an Asoc Project, and then paste in some code into the empty (hopefully) handler, then choose to debug, and step through the code, that it will work, and it should be easy to grasp from there. :slight_smile:

It is of course some overhead, but at least sometimes this is better than having log statements all over. I also think you can edit the values while your script runs, which is more powerful than Script Debugger.