sballin / alfred-search-notes-app

Use Alfred to quickly open notes in iCloud/Apple Notes.
https://www.alfredforum.com/topic/11716-search-appleicloud-notes/
MIT License
502 stars 23 forks source link

Cursor not set when creating new note #75

Closed Liebhecht closed 3 months ago

Liebhecht commented 5 months ago

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)

[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

sballin commented 5 months ago

Should be fixed in version 3.6.1.

Liebhecht commented 5 months ago

Thank you!

Liebhecht commented 5 months ago

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.

sballin commented 5 months ago

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?

Liebhecht commented 5 months ago

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

image
sballin commented 5 months ago

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
Liebhecht commented 5 months ago

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.

sballin commented 5 months ago

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
Liebhecht commented 5 months ago
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

sballin commented 5 months ago

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.

Liebhecht commented 5 months ago

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

sballin commented 5 months ago

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

sballin commented 4 months ago

Could be fixed by the latest release (3.6.2)?