sballin/alfred-search-notes-app

Cursor not set when creating new note

Closed this issue · 13 comments

To Reproduce

Steps/context to reproduce the behavior:

  1. create new note

Error text

no error text

Info

I got a new Laptop from an old 2015 MacBook Pro to a new m3 pro one. On the old one if I ran the command to create a new note it automatically set the cursor two line beneath the title. Now the cursor is not set at all if the notes app was not already running (its not focussed) or when it is running the cursor is set in front of the title (there are two line created beneath the title, the cursor is just not set there)

  • macOS version: 14.2.1 (23C71)
  • CPU: m3 pro
  • Alfred version: Alfred 5.1.4 Build 2195
  • Workflow version: 3.6
  • In System Preferences > Security & Privacy > Privacy, there are checkmarks for Accessibility > Alfred 4, Full Disk Access > Alfred 4, Automation > Alfred 4 > Notes, and Automation > Alfred 4 > System Events?: no for the very last one, although there isn't even an option
  • What type of account are your notes stored in (iCloud/On My Mac/Email)? iCloud
  • If the workflow can't manage to show a list of your notes, verify that the search_notes executable file is present by right-clicking the "Search Notes" workflow in the workflow list of Alfred Preferences, choosing "Open in Finder" and opening the search_notes folder. Is there a file of the same name inside that folder?
  • If no error notification appears automatically, what is the output of the debug log when you reproduce the problem?

[00:08:32.502] Search Notes[Keyword] Processing complete
[00:08:32.508] Search Notes[Keyword] Passing output 'Bug' to Run NSAppleScript
[00:08:33.195] Search Notes[Run NSAppleScript] Processing complete
[00:08:33.204] Search Notes[Run NSAppleScript] Passing output '' to Run Script

Or more legible, don't know what you prefer:
[00:08:32.502] Search Notes[Keyword] Processing complete
[00:08:32.508] Search Notes[Keyword] Passing output 'Bug' to Run NSAppleScript
[00:08:33.195] Search Notes[Run NSAppleScript] Processing complete
[00:08:33.204] Search Notes[Run NSAppleScript] Passing output '' to Run Script

Should be fixed in version 3.6.1.

Thank you!

Should be fixed in version 3.6.1.

Sadly it's broken again. Same problem as before I updated. For some reason the behavior reverted back.

Weird, it's working for me. Can you check that you definitely have macOS 14.2.1, workflow version 3.6.1? And in the menubar View > as List is checked?

I changed the keyword to "nn", but that's it.

image

Assuming you're creating notes as a fallback after search finds none, can you replace the contents of the block "Show note/folder or create a note" with the following and show me the error messages/debug log when you try to create a new note?

-- This script takes "URLs" of format "identifier,itemID,accountID,itemFolderID,userQuery" or just "userQuery"
-- accountID can be the string "null", if so the default account is used
-- Some commands need to run twice, otherwise they fail when toolbar search is active

on alfred_script(q)
	try
		if q contains "/ICNote/p" or q contains "/ICFolder/p" then
			set AppleScript's text item delimiters to ","
			set identifier to text item 1 of q
			set itemID to text item 2 of q
			set accountID to text item 3 of q
			set itemFolderID to text item 4 of q
			set AppleScript's text item delimiters to ""
			tell application "Notes"
				if itemID contains "/ICNote/p" then
					-- Show user-requested note
					open location "notes://showNote?identifier=" & identifier
					open location "notes://showNote?identifier=" & identifier
					-- Compatibility with macOS < 11 which does not support notes://
					set OSVersion to system version of (system info)
					set mainVersion to text 1 thru ((offset of "." in OSVersion) - 1) of OSVersion as number
					if mainVersion < 11 then
						if accountID is "null" then
							show note id itemID in default account
							show note id itemID in default account
						else
							show note id itemID in account id accountID
							show note id itemID in account id accountID
						end if
					end if
					if itemFolderID is not "null" then
						if accountID is "null" then
							show folder id itemFolderID in default account
						else
							show folder id itemFolderID in account id accountID
						end if
					end if
				else if itemID contains "/ICFolder/p" then
					-- Show user-requested folder
					if accountID is "null" then
						show folder id itemID in default account
						show folder id itemID in default account
					else
						show folder id itemID in account id accountID
						show folder id itemID in account id accountID
					end if
				end if
			end tell
			
		else
			tell application "Notes"
				-- Create new note from user query
				tell default account to make new note at default folder with properties {body:"<div><h1>" & q & "</h1></div><div><br></div>"}
				show first note in default account
				show first note in default account
			end tell
			-- Position cursor in body of new note
			tell application "System Events"
				tell process "Notes"
					set focused of scroll area 3 of splitter group 1 of window 1 to true
					key code {125, 125} -- down, down
				end tell
			end tell
		end if
	on error errorMessage number errorNumber
		display alert "Show Note Error" message ((errorNumber as string) & ": " & errorMessage & "

Input: " & q) as critical
	end try
end alfred_script

No I use "n" for searching and "nn" for creating new notes. I'm not sure how to run your code and create a note with the default/"n" keyword.

You can replace the block that nn is hooked up to with

-- Some commands need to run twice, otherwise they fail when toolbar search is active

on alfred_script(q)
	try
		tell application "Notes"
			-- Create new note from user query
			tell default account to make new note at default folder with properties {body:"<div><h1>" & q & "</h1></div><div><br></div>"}
			show first note in default account
			show first note in default account
		end tell
		-- Position cursor in body of new note
		tell application "System Events"
			tell process "Notes"
				set focused of scroll area 3 of splitter group 1 of window 1 to true
				key code {125, 125} -- down, down
			end tell
		end tell
	on error errorMessage number errorNumber
		display alert "Create Note Error" message (errorNumber as string & ": " & errorMessage & "\n\nInput: " & q) as critical
	end try
end alfred_script
image

Debug outuput:

[17:05:39.890] Search Notes[Script Filter] Queuing argument ''
[17:05:40.003] Search Notes[Script Filter] Script with argv '' finished
[17:05:40.008] Search Notes[Script Filter] { "items": [ { "title": "Lena", "subtitle": "Notes", "arg": "16D2717B-6202-43B9-808B-625916153895,x-coredata://BC1D4D98-C89F-4EB1-B01A-D91AE2B23510/ICNote/p631,null,x-coredata://BC1D4D98-C89F-4EB1-B01A-D91AE2B23510/ICFolder/p6,", "quicklookurl": " " }, { "title": "Keyboard Remapping", "subtitle": "Notes", "arg": "B650F5C9-2513-4DF3-BE4E-231A28B60729,x-coredata://BC1D4D98-C89F-4EB1-B01A-D91AE2B23510/ICNote/p756,null,x-coredata://BC1D4D98-C89F-4EB1-B01A-D91AE2B23510/ICFolder/p6,", "quicklookurl": " " }, { "title": "Test", "subtitle": "Notes", "arg": "46DCC864-22CE-4790-AE71-99E7816912D3,x-coredata://BC1D4D98-C89F-4EB1-B01A-D91AE2B23510/ICNote/p780,null,x-coredata://BC1D4D98-C89F-4EB1-B01A-D91AE2B23510/ICFolder/p6,", "quicklookurl": " " }, AND SO ON FOR ALL MY NOTES, CANT SHARE FOR PRIVACY REASONS
[17:05:42.155] Search Notes[Keyword] Processing complete
[17:05:42.201] Search Notes[Keyword] Passing output 'Test' to Run NSAppleScript
[17:05:55.964] Search Notes[Run NSAppleScript] Processing complete
[17:05:55.976] Search Notes[Run NSAppleScript] Passing output '' to Run Script

Weird, maybe the window layout of the notes app changed as recently as macOS 14.3. You could try upgrading to 14.3 to see if that fixes it. Unfortunately I don't know any better way to implement this feature than the current way which breaks every time apple changes the app slightly.

Okay so always have the most up to date os and workflow version should be the best I can can do right?

And no worries, your effort is very much appreciated (:

Yes, that should help and I'll try to make it do the right thing depending on the OS version going forward.

Could be fixed by the latest release (3.6.2)?