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
522 stars 24 forks source link

Move notes to folder (working script) #47

Closed derickfay closed 3 years ago

derickfay commented 3 years ago

Hi I've updated my copy to include a script (set up under keyword nf with option down) to move the currently selected notes to the found folder. This would be a great official addition IMHO. The script is below, with lots of your code in comments which can no doubt be deleted.

-- This script takes "URLs" of format "identifier,itemID,accountID,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 AppleScript's text item delimiters to ""
        end if
        tell application "Notes"
            if q 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
*)
            else if q contains "/ICFolder/p" then
                -- Show user-requested folder
                set s to selection
                repeat with n in s
                    if accountID is "null" then
                        move n to folder id itemID in default account
                        move n to folder id itemID in default account
                    else
                        move n to folder id itemID in account id accountID
                        move n to folder id itemID in account id accountID
                    end if
                end repeat
                (*          else
                -- 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
                tell application "System Events"
                    tell process "Notes"
                        set focused of text area of scroll area of group 1 of splitter group 1 of splitter group 1 of window 1 to true
                        key code {125, 125} -- down, down
                    end tell
                end tell
                *)
            end if
        end tell
    on error errorMessage number errorNumber
        display alert "Show Note Error" message ((errorNumber as string) & ": " & errorMessage & "

Input: " & q) as critical
    end try
end alfred_script
sballin commented 3 years ago

Thanks for the contribution, I'll try adding it to the next release!

sballin commented 3 years ago

Implemented in version 3.5.0!