Attach Pictures to Notes

Hi there,
cannot get my script working…strange, it should work.

property remind_list : "- Import"

tell application "Finder"
	set thepict to item 1 of (get the selection) as alias
end tell

tell application "Notes"
	#prep default import list
	set all_lists to name of every folder
	if remind_list is not in all_lists then
		make new folder with properties {name:remind_list}
	end if
	#set theme_id to item 1 of (get every list whose name is remind_list)
	set shdt to text 2 thru -1 of POSIX path of (time string of (current date))
	
	try
		#build lists or truncated reminders
		set new_pict_import to "Picture_import-" & shdt
		set new_note to make new note at folder remind_list with properties {name:new_pict_import}
		#set raw_format to (read file thepict)
		#delay 0.2
		make new attachment at end of attachments of new_note with data thepict
		save
	on error err
		activate
		display dialog err
	end try
end tell

The syntax used to create the folder is wrong.
If the folder doesn’t exists, the instruction supposed to create it doesn’t achieve it’s task so when you try to make the new note the script fails with :
→ error number -1728 from folder “- Import”

If I create the folder by hand the note is correctly created.

Check the syntax supposed to create the folder.
According to : https://www.macosxautomation.com/applescript/notes/02.html
you must “speak” to an account to create a folder as I did to get a working script :

tell account "Sur mon Mac" # Edit to use your account
	make new folder with properties {name:remind_list}
end tell

Yvan KOENIG running Sierra 10.12.6 in French (VALLAURIS, France) vendredi 4 aout 2017 18:18:05

Thanks, the tag 'account ’ helped me, now my script works on Sierra.
Not so on my macpro running Yosemite, :(( which i can’t upgrade anymore… :frowning:

Sierra: -pay attention to your default account name-

property remind_list : "- Import"

tell application "Finder" to set thepict to the selection

tell application "Notes"
	#prepare default import list
	set all_lists to name of every folder
	if remind_list is not in all_lists then
		make new folder with properties {name:remind_list}
	end if
	set shdt to text 2 thru -1 of POSIX path of (time string of (current date))
	set new_pict_import to "Bildimport-" & shdt
	
	tell account "Lokal"
		try
			#build reminders
			set new_note to make new note at folder remind_list with properties {name:new_pict_import}

			make new attachment at end of attachments of new_note with data file (thepict as text)
			save
		on error err
			activate
			display dialog err
		end try
		
	end tell
end tell

You MUST tell account WXYZ to make the new folder.

There is no need to do that for the other instructions.

property remind_list : "- Import"

tell application "Finder" to set thepict to the selection

tell application "Notes"
	#prepare default import list
	set all_lists to name of every folder
	if remind_list is not in all_lists then
		tell account "Lokal" # REQUIRED
			make new folder with properties {name:remind_list}
		end tell # REQUIRED
	end if
	set shdt to text 2 thru -1 of POSIX path of (time string of (current date))
	set new_pict_import to "Bildimport-" & shdt
	
	# tell account "Lokal" # not needed
	try
		#build reminders
		set new_note to make new note at folder remind_list with properties {name:new_pict_import}
		
		make new attachment at end of attachments of new_note with data file (thepict as text)
		save
	on error err
		activate
		display dialog err
	end try
	# end tell # not needed
end tell

Yvan KOENIG running Sierra 10.12.6 in French (VALLAURIS, France) samedi 5 aout 2017 10:54:38

This is a Raw-file picture format of my digital camera, with jpg i get no feedback…
I’ve seen that cr2 or text or workflow files are added as Url on drag and drop. But jpg should work… :rolleyes: confused

My script on Yosemite works, the attachment is added, but… Not visible… I tried to add ‘save’ to my code but nothing. Quitting and restarting Notes does not help either - the attachment is there but not visible :frowning:

I wrote after testing !

property remind_list : "- Import"

tell application "Finder" to set thepict to the selection

tell application "Notes"
	#prepare default import list
	set all_lists to name of every folder
	log all_lists
	if remind_list is not in all_lists then
		log "the folder doesn't exist"
		log (get name of every account)
		tell account "Sur mon Mac" # REQUIRED
			make new folder with properties {name:remind_list}
		end tell # REQUIRED
		log (get name of every folder)
	end if
	set shdt to text 2 thru -1 of POSIX path of (time string of (current date))
	set new_pict_import to "Bildimport-" & shdt
	
	# tell account "Lokal" # not needed
	log (get name of every attachment)
	try
		#build reminders
		set new_note to make new note at folder remind_list with properties {name:new_pict_import}
		
		make new attachment at end of attachments of new_note with data file (thepict as text)
		save
		
		log (get name of every attachment)
	on error err
		activate
		display dialog err
	end try
	# end tell # not needed
end tell

Here is the history:

tell application "Finder"
	get selection
		--> {document file "_DSC0064_05.nef" of folder "traitées" of folder "gaby_3" of folder "Gaby Giordano" of folder "autour_hommage" of folder "tout_pour_expo ƒ" of folder "Tout Gerbino" of folder "Important" of folder "Users" of disk "Macintosh HD"}
end tell
tell application "Notes"
	get name of every folder
		--> {"Notes"}
	(*Notes*)
	(*the folder doesn't exist*)
	get name of every account
		--> {"Sur mon Mac"}
	(*Sur mon Mac*)
	make new folder with properties {name:"- Import"}
		--> folder id "x-coredata://CABFE677-3CC1-4D76-A9FF-7B60DEA99F51/ICFolder/p52"
	get name of every folder
		--> {"- Import", "Notes"}
	(*- Import, Notes*)
	current date
		--> error number -10004
end tell
tell current application
	current date
		--> date "samedi 5 août 2017 à 12:02:39"
end tell
tell application "Notes"
	get name of every attachment
		--> {}
	(**)
	make new note at folder "- Import" with properties {name:"Bildimport-12/02/39"}
		--> note id "x-coredata://CABFE677-3CC1-4D76-A9FF-7B60DEA99F51/ICNote/p53"
end tell
tell application "Finder"
	get document file "_DSC0064_05.nef" of folder "traitées" of folder "gaby_3" of folder "Gaby Giordano" of folder "autour_hommage" of folder "tout_pour_expo ƒ" of folder "Tout Gerbino" of folder "Important" of folder "Users" of disk "Macintosh HD"
		--> "Macintosh HD:Users:Important:Tout Gerbino:tout_pour_expo ƒ:autour_hommage:Gaby Giordano:gaby_3:traitées:_DSC0064_05.nef"
end tell
tell application "Notes"
	make new attachment at end of every attachment of note id "x-coredata://CABFE677-3CC1-4D76-A9FF-7B60DEA99F51/ICNote/p53" with data file "Macintosh HD:Users:Important:Tout Gerbino:tout_pour_expo ƒ:autour_hommage:Gaby Giordano:gaby_3:traitées:_DSC0064_05.nef"
		--> attachment id "x-coredata://CABFE677-3CC1-4D76-A9FF-7B60DEA99F51/ICAttachment/p55"
	save current application
	get name of every attachment
		--> {"_DSC0064_05.nef"}
	(*_DSC0064_05.nef*)
end tell

As you may see, the folder “-import” was unavailable.
An extraneous log prove that there was no attachment available.
The script created it then the script created the new note and it’s attachment.
An extraneous log instruction prove that the attachment was really created.
As you may see, the attachment is a raw file created by a Nikon camera.

If you wish, you may send a sample file issued from your camera so that I may test if there is a problem with the format CR2.
My address is : k o e n i g . y v a n @ s f r . f r without the space characters.

Yvan KOENIG running Sierra 10.12.6 in French (VALLAURIS, France) samedi 5 aout 2017 12:08:19

Having no feedback, as I am curious, I reread your late message and was a bit surprised.

The given error message claims that the script tried to store the attachment at the path :
“/Users/joy/Library/Containers/com.apple.Notes/Data/Library/CoreData/Attachments/F5E3EC76-FDFB-401C-B598-0E291B04868E/IMG_1179.CR2, NSFilePath= /Users/joy/Desktop/IMG_1179.CR2”

Here, it was saved as :
“/Users/???/Library/Group Containers/group.com.apple.notes/Media/E204B198-9B51-40DE-B2BA-182144901567/_DSC0064_05.nef”

I’m wondering : why such difference ?

Here I Notes stores in my local HD. As I have no mobile device I never used iCloud.
Are you storing your notes on the Cloud ?

It’s really puzzling because, like me you run under Sierra.

Yvan KOENIG running Sierra 10.12.6 in French (VALLAURIS, France) samedi 5 aout 2017 20:05:49

the problem is that i cant work all the time with my laptop, running Sierra.
My MacPro runs Yosemite (10.10.5) and i could update to El Capitain, -but not Sierra because too old my machine- at the cost to sacrify several apps i need for work.

The script works flawlessly on Sierra, but not on Yosemite.
In Yosemite, my script isnt able to add the attachment - this is the only limitation. Folders and notes are easy to create whatever version i use.

I tried like crazy to get the attachment added to Notes (on yosemite), but failed. :frowning:
your script is easy to understand, thanks, but it doesnt work for yosemite.

I booted under Yosemite and ran this script :

property remind_list : "- Import"
set theFolder to (path to library folder from user domain as text) & "Containers:com.apple.Notes:Data:Library:CoreData:Attachments:"
tell application "Finder"
	set thepict to the selection
	set oldAttachmentsFolders to folders of folder theFolder as alias list
end tell

set myAccount to "On My Mac"

tell application "Notes"
	#prepare default import list
	set all_lists to name of every folder
	log all_lists
	if remind_list is not in all_lists then
		log "the folder doesn't exist"
		log (get name of every account)
		tell account myAccount # REQUIRED
			make new folder with properties {name:remind_list}
		end tell # REQUIRED
		log (get name of every folder)
	end if
	set shdt to text 2 thru -1 of POSIX path of (time string of (current date))
	set new_pict_import to "Bildimport-" & shdt
	
	try
		#build reminders
		set new_note to make new note at folder remind_list of account myAccount with properties {name:new_pict_import, body:"what a mess!"}
		make new attachment at end of attachments of new_note with data file (thepict as text)
		--> document file "IMGP2139+.JPG" of folder "Desktop" of folder "??????????" of folder "Users" of startup disk
	on error err
		activate
		display dialog err
	end try
	
	save # It seems that it does nothing
end tell


tell application "Finder"
	set theAttachmentsFolders to folders of folder theFolder as alias list
	set found to false
	repeat with anItem in theAttachmentsFolders
		if anItem is not in oldAttachmentsFolders then
			set found to true
			exit repeat
		end if
	end repeat
	set theAttachments to name of every file of anItem
	--> get name of every file of alias "Aluice_250:Users:??????????:Library:Containers:com.apple.Notes:Data:Library:CoreData:Attachments:6DD32AF2-CFE8-48C3-AB30-C0BFD0FC9503:"
end tell

The history was :

tell current application
	path to library folder from user domain as text
end tell
tell application "Finder"
	get selection
	get every folder of folder "Aluice_250:Users:??????????:Library:Containers:com.apple.Notes:Data:Library:CoreData:Attachments:"
end tell
tell application "Notes"
	get name of every folder
	(*Notes*)
	(*the folder doesn't exist*)
	get name of every account
	(*On My Mac*)
	make new folder with properties {name:"- Import"}
	get name of every folder
	(*- Import, Notes*)
	current date
end tell
tell current application
	current date
end tell
tell application "Notes"
	make new note at folder "- Import" of account "On My Mac" with properties {name:"Bildimport-17/11/11", body:"what a mess!"}
end tell
tell application "Finder"
	get document file "IMGP2139+.JPG" of folder "Desktop" of folder "??????????" of folder "Users" of startup disk
end tell
tell application "Notes"
	make new attachment at end of every attachment of note id "x-coredata://C7A540F8-DF9B-42BC-A923-B1C6F257AC05/Note/p20" with data file "Aluice_250:Users:??????????:Desktop:IMGP2139+.JPG"
	save current application
end tell
tell application "Finder"
	get every folder of folder "Aluice_250:Users:??????????:Library:Containers:com.apple.Notes:Data:Library:CoreData:Attachments:"
	get name of every file of alias "Aluice_250:Users:??????????:Library:Containers:com.apple.Notes:Data:Library:CoreData:Attachments:6DD32AF2-CFE8-48C3-AB30-C0BFD0FC9503:"
end tell
Résultat :
{"IMGP2139+.JPG"}

As you see (and may check on your machine), the attached file was really stored on disk.

Alas :
(a) the picture didn’t appear in the note window
(b) when I quitted the application, the folder “Aluice_250:Users:???:Library:Containers:com.apple.Notes:Data:Library:CoreData:Attachments:6DD32AF2-CFE8-48C3-AB30-C0BFD0FC9503:” disappeared (with the file “IMGP2139+.JPG”).

When I searched in Google I found several links pointing upon threads in which users asked about this odd behavior.
I don’t know in which system it was solved.

Thanks to the CLI cliclick available from https://www.bluem.net/en/mac/cliclick/
you may replace in your script the instruction supposed to create the attachment by new ones designed to drag and drop the selected file onto the newly created note window.

Yvan KOENIG running Sierra 10.12.6 in French (VALLAURIS, France) dimanche 6 aout 2017 17:35:22

Exactly.
I found out that picture files from older Os get attached on drag n drop as url string, and using only my script they disappear after re-launching Notes. Looking for a similar but scriptable App.
Thanks for your generous feedback