bethany7 Posted February 1, 2013 Share Posted February 1, 2013 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 drking, mlgill and jibbe 3 Link to comment
mlgill Posted February 2, 2013 Share Posted February 2, 2013 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. bethany7 1 Link to comment
bethany7 Posted February 4, 2013 Author Share Posted February 4, 2013 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 Link to comment
mlgill Posted February 4, 2013 Share Posted February 4, 2013 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! Link to comment
bethany7 Posted February 4, 2013 Author Share Posted February 4, 2013 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 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 Link to comment
LexIcon76 Posted April 2, 2013 Share Posted April 2, 2013 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? Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now