【问题标题】:AppleScript: How to automate creating a new note in Apple NotesAppleScript:如何在 Apple Notes 中自动创建新笔记
【发布时间】:2023-04-04 22:37:02
【问题描述】:

我正在尝试在 Notes.app 中自动快速创建新笔记,并且我想在单个浮动窗口中打开我新创建的笔记。

这是我创建笔记的代码:

set RunTime to ((current date)) as string
tell application "Notes"
    activate
    tell account "iCloud"
        make new note at folder "Notes" with properties {name:RunTime}
        --does not work
        --open document {name:RunTime}
    end tell
end tell

有什么想法吗?

【问题讨论】:

    标签: macos applescript


    【解决方案1】:

    Notes 本身没有selection 的脚本命令,但show 命令会在显示的过程中选择指定的注释。也没有使用 Notes 浮动窗口的命令,但可以使用 GUI 脚本来选择该菜单项,例如 (Mojave):

    set example to (current date) as string
    tell application "Notes"
        activate
        tell folder "Notes" -- or whatever
            set theNote to make new note with properties {name:example} -- 'make' returns a reference to the new note
        end tell
    
        try
            (* If not making a new note, existing notes can be referenced by using id or name properties:
                    set theNote to note id "xyz" -- id property
                    -- or --
                    set theNote to note example -- name property
                    -- or --
                    tell (get every note whose name is example) -- filter form
                        if it is {} then error "A note named \"" & example & "\" was not found."
                        set theNote to its first item
                    end tell
            *)
            show theNote -- also selects the note
            delay 0.25
            tell application "System Events" to click menu item "Float Selected Note" of menu "Window" of menu bar item "Window" of menu bar 1 of application process "Notes"
        on error errmess -- not able to get the note
            log errmess
            display alert "Error getting note" message errmess
        end try
    end tell
    

    【讨论】:

      【解决方案2】:

      试试这个,对我有用

      set example to ("New Note") as string
      tell application "Notes"
      activate
      tell folder "Notes" -- or whatever
          set theNote to make new note with properties {name:example} -- 'make' returns a reference to the new note
      end tell
      
      try
          (* If not making a new note, existing notes can be referenced by using id or name properties:
                  set theNote to note id "xyz" -- id property
                  -- or --
                  set theNote to note example -- name property
                  -- or --
                  tell (get every note whose name is example) -- filter form
                      if it is {} then error "A note named \"" & example & "\" was not found."
                      set theNote to its first item
                  end tell
          *)
          show theNote -- also selects the note
          tell application "System Events" to click menu item "Float Selected Note" of menu "Window" of menu bar item "Window" of menu bar 1 of application process "Notes"
          close back window
      on error errmess -- not able to get the note
          log errmess
          display alert "Error getting note" message errmess
      end try 
      end tell
      

      【讨论】:

        【解决方案3】:
        set noteContent to ¬
            "I'm trying to automate quickly creating new notes in the Notes.app
         and I would like to open my newly created note in a single floating window.
        Here's my code for creating the note:"
        
        set noteTitle to "Quickly created new note" & linefeed
        
        set noteHTMLText to ¬
            "<pre style=\"font-family:Helvetica,sans-serif;font-size:20;bold:false\">" & ¬
            noteContent & ¬
            "</pre>"
        
        tell application "Notes" to tell account "iCloud"
            -- make new note
            set theNote to make new note at folder ¬
                "Notes" with properties {name:noteTitle, body:noteHTMLText}
            -- get its id
            try
                id of theNote
            on error errorMessage
                set ATID to AppleScript's text item delimiters
                set AppleScript's text item delimiters to "\""
                set theID to text item 2 of errorMessage
                set AppleScript's text item delimiters to ATID
            end try
            -- show created note to select it
            set theDoc to show note id theID of folder "Notes"
        end tell
        
        -- open created note in a single floating window
        tell application "Notes" to activate
        tell application "System Events" to tell process "Notes" to ¬
            click menu item "Float Selected Note" of menu 1 of ¬
                menu bar item "Window" of menu bar 1
        

        【讨论】:

          【解决方案4】:

          以下 AppleScript 将打开新创建的 RunTime 注释。

          tell application "Notes"
              tell account "iCloud"
                  make new note at folder "Notes" with properties {name:"RunTime", body:"RunTime body"}
                  set noteRunTime to get notes whose name is "RunTime"
                  show item 1 of noteRunTime
              end tell
          end tell
          

          如果您知道笔记的 ID,则可以使用以下 AppleScript 将其打开。

          在执行脚本之前,您需要适当地设置变量 noteID 和 myAccount 的值。

          set noteID to "x-coredata://05A6F727-5F0B-478F-A493-B2DFAEA12067/ICNote/p153"
          set myAccount to "iCloud"
          
          tell application "Notes"
              tell account myAccount
                  set aNote to get notes whose id is noteID
                  if aNote is {} then display dialog "The note was not found."
                  show item 1 of aNote        
              end tell
          end tell
          

          此外,您可以使用以下脚本获取便笺的 id。 id 被复制到剪贴板。

          tell application "Notes"
              activate
              set noteNames to name of notes
              set chosenNote to (choose from list noteNames)
              if (chosenNote is false) then error number -128 -- Cancel button.
              set aNote to get notes whose name is item 1 of chosenNote
              set noteID to id of item 1 of aNote
              set the clipboard to noteID
              display dialog "The id of the selected note is " & noteID
          end tell
          

          【讨论】:

            【解决方案5】:
            1. Notes.app AppleScript 套件不支持在新文档中打开笔记,例如 Safari。
            2. 标准套件包含 document,但是,您需要在 tell account “iCloud”tell application “Notes 之外使用它
            3. 您最后的希望可能是 在使用 noteID 时打开便笺位置 {url}

              set noteID to make new note at folder "Notes" with properties {name:RunTime}
              

            【讨论】:

              【解决方案6】:

              试试这个

              tell application "Notes"
                  tell account "iCloud"
                      tell folder "Notes" -- you can remove this to show notes from any folder
                          show note 1 -- notes are indexed by their last edited date
                      end tell
                  end tell
              end tell
              

              很遗憾,我无法弄清楚如何通过笔记的名称或 ID 来引用笔记。上面的答案似乎不再有效。如果有人知道怎么做,请告诉我。

              【讨论】:

              • 你说的不是show note named "the name"show note id "the id"吗?
              • 我现在已经知道如何通过他们的名字来引用笔记。但是,我不知道如何通过他们的 id 来引用笔记。 @red_menace
              • 您可以通过查找它(名称、索引等)或在创建它时获取它,然后使用note id "the id"
              • 我试过了,但我无法让它工作。我认为一定有一个错误。你能看看你是否可以使用一个简单的脚本来显示具有特定 ID 的笔记?
              • 这似乎不再是一个答案。我已经为原始发帖者的问题添加了一个答案(尽管他们已经有一段时间没有出现了),这可能会有所帮助。
              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2015-08-21
              • 1970-01-01
              • 2021-02-04
              • 1970-01-01
              • 1970-01-01
              • 2011-12-23
              • 2020-04-26
              相关资源
              最近更新 更多