Jump to content
bethany7

Create a Note in Notes App

Recommended Posts

Hi,

 

Okay, please forgive me for any mistakes I make here! This is my first, very basic, AppleScript workflow. It is probably terribly done and completely wrong, but I was proud and wanted to share it! It's very simple, just creates a note in the Notes app. It can either close immediately after or (by pressing alt) can leave the app open.

 

I apologise if this workflow is a duplicate of someone else's, I couldn't find one by searching when I looked, as I wasn't originally planning on creating it myself!

 

I was hoping somebody could give me some advice on extending the workflow to add a notification when successful. I wanted to display the contents of the note in the notification, but using {query} just results in a blank message! Any corrections, improvements and modifications are very welcome!

 

Workflow available here: http://d.pr/f/H7f6

Share this post


Link to post

Hi Bethany,

 

Nice workflow!

 

To get a notification, you have to return a string of some kind from the "alfred_script" subroutine. I'm no Applescript wizard myself, but I think I can help.

 

Here's a sample applescript that will query Notes.app to see if the note was created and then return information about it. I've made lots of comments so hopefully it's relatively clear. 

 

on alfred_script(q)
	tell application "Notes"
		set theNoteID to make new note in folder "Notes" with properties {name:"From Alfred", body:q}
	end tell
	-- this function will check for the note and retun a string with some information from it
	set theResultOfNoteQuery to my verify_note(theNoteID)
	
	-- next return the string from this script to alfred
	return theResultOfNoteQuery
	
end alfred_script

on verify_note(theNoteID)
	
	try
		tell application "Notes"
			-- get references for the properties of the note and the list that contains it
			set theNoteProperties to properties of theNoteID
			set theListProperties to properties of container of theNoteID
			
			-- get some of the properties of interest from the note and from the list
			set theTitle to the name of theNoteProperties
			set theText to the body of theNoteProperties
			set theCreationDate to the creation date of theNoteProperties
			set theListName to the name of theListProperties
		end tell
		
		-- shorten the main text of the note so it fits in the notification
		-- can adjust this via the integer for "maxStringLength"
		set maxStringLength to 15
		if the length of theText > maxStringLength then
			set theShortText to (items 1 thru maxStringLength of theText as string) & ".."
		else
			set theShortText to theText
		end if
		
		-- put this together to make a string with note info
		-- this joins some variables with text
		-- the backslashes before some of the double quotes are so that these double quotes are part of the text that is shown
		-- this can be customized if you want to change the notification
		
		-- this is a detailed version of the string to be returned
		-- it has currently been commented out so it is not used
		--set theSuccessString to "Created note with title \"" & theTitle & "\" on list \"" & theListName & "\" on " & theCreationDate & " with text \"" & theShortText & "\"."
		
		-- this is a shorter version
		set theSuccessString to "Created note with text \"" & theShortText & "\"."
	on error
		-- if the "try" function fails, it likely means the note does not exist
		set theSuccessString to "There was an error creating the note."
	end try
	
	-- send this back to the main alfred function
	return theSuccessString
	
end verify_note

 

 

It sounds like you know this, but after the "Run Applescript" block in the workflow, you then also have to add a block to "Post Notification" within that block, add {query} to the "Text:" field and customize anything else. This notification block has to be connected to the backend of your "Run Applescript" blocks.

 

Hope this helps.

Share this post


Link to post

Thank you so much! Works perfectly :) Just had to figure out how to get it to quit after the note was made, but managed it eventually!

 

Cheers,

 

Bethany

Share this post


Link to post

Thank you so much! Works perfectly :) Just had to figure out how to get it to quit after the note was made, but managed it eventually!

 

Cheers,

 

Bethany

 

 

Glad it worked for you, Bethany! I wrote lots of comments and extracted more data from the note than you may want since I figured it might help you learn.

 

I do have one comment about automatically quitting notes once the note was created. If you use notes on your phone or another computer, you will want to make sure the application has time to sync the note to iCloud. This happens very quickly in my experience, but it may not be instantaneous. So it might be wise to insert a time delay before quitting notes. The code to do this is very simple:

 

delay 5 

 

where the number afterwards is the delay in seconds. I've guessed, but 5 seconds should almost certainly be enough. You can also enter fractions, so 

 

delay 0.5

 

would be half a second.

 

Enjoy!

Share this post


Link to post

Ah, I hadn't thought of that! Nice one!

 

I really appreciate this! As I said, I hadn't set out to create it myself, I mistakenly assumed a workflow would already exist! I am attempting to learn scripting (my only previous coding experience was in C a long time ago when I was at school), but a) wasn't planning on using AppleScript and B) didn't expect to get thrown in at the deep end! So I really do appreciate your help, not only for the workflow, but furthering my knowledge as well.

 

Thanks,

 

Bethany

Share this post


Link to post

Glad it worked for you, Bethany! I wrote lots of comments and extracted more data from the note than you may want since I figured it might help you learn.

 

I do have one comment about automatically quitting notes once the note was created. If you use notes on your phone or another computer, you will want to make sure the application has time to sync the note to iCloud. This happens very quickly in my experience, but it may not be instantaneous. So it might be wise to insert a time delay before quitting notes. The code to do this is very simple:

 

delay 5 

 

where the number afterwards is the delay in seconds. I've guessed, but 5 seconds should almost certainly be enough. You can also enter fractions, so 

 

delay 0.5

 

would be half a second.

 

Enjoy!

 

 

Where do you add this code? 

Share this post


Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...