diff --git a/macnotesapp/macnotesapp.applescript b/macnotesapp/macnotesapp.applescript index 22bcace..9f85f80 100644 --- a/macnotesapp/macnotesapp.applescript +++ b/macnotesapp/macnotesapp.applescript @@ -308,6 +308,18 @@ on noteShow(accountName, noteID) end tell end noteShow +on noteAddAttachment(accountName, noteID, attachmentPath) + tell application "Notes" + tell account accountName + set theNote to note id (noteID) + set theAttachment to make new attachment at theNote with data POSIX file attachmentPath + return id of theAttachment + end tell + end tell +end noteAddAttachment + +(******** Folder Class *********) + on noteGetAttachments(accountName, noteID) (* Get attachments for noteID in accountName *) tell application "Notes" diff --git a/macnotesapp/macnotesapp_applescript.py b/macnotesapp/macnotesapp_applescript.py index 0739759..c591ca3 100644 --- a/macnotesapp/macnotesapp_applescript.py +++ b/macnotesapp/macnotesapp_applescript.py @@ -315,6 +315,18 @@ end tell end noteShow +on noteAddAttachment(accountName, noteID, attachmentPath) + tell application "Notes" + tell account accountName + set theNote to note id (noteID) + set theAttachment to make new attachment at theNote with data POSIX file attachmentPath + return id of theAttachment + end tell + end tell +end noteAddAttachment + +(******** Folder Class *********) + on noteGetAttachments(accountName, noteID) (* Get attachments for noteID in accountName *) tell application "Notes" diff --git a/macnotesapp/notesapp.py b/macnotesapp/notesapp.py index ed3f204..eff3d96 100644 --- a/macnotesapp/notesapp.py +++ b/macnotesapp/notesapp.py @@ -152,19 +152,26 @@ def version(self) -> str: """Return version of Notes.app""" return str(self.app.version()) - def make_note(self, name: str, body: str) -> "Note": + def make_note( + self, name: str, body: str, attachments: list[str] | None = None + ) -> "Note": """Create new note in default folder of default account. Args: name: name of notes body: body of note as HTML text + attachments: optional list of paths to attachments to add to note Returns: newly created Note object """ # reference: https://developer.apple.com/documentation/scriptingbridge/sbobject/1423973-initwithproperties account = Account(self.app.defaultAccount()) - return account.make_note(name, body) + note = account.make_note(name, body) + if attachments: + for attachment in attachments: + note.add_attachment(attachment) + return note def account(self, account: Optional[str] = None) -> "Account": """Return Account object for account or default account if account is None. @@ -324,16 +331,27 @@ def show(self): """Show account in Notes.app UI""" self._run_script("accountShow") - def make_note(self, name: str, body: str, folder: str | None = None) -> "Note": + def make_note( + self, + name: str, + body: str, + folder: str | None = None, + attachments: list[str] | None = None, + ) -> "Note": """Create new note in account Args: name: name of note body: body of note - folder: folder to create note in; if None, uses default folder + folder: optional folder to create note in; if None, uses default folder + attachments: optional list of file paths to attach to note Returns: Note object for new note + + Raises: + ScriptingBridgeError: if note could not be created + FileNotFoundError: if attachment file could not be found """ # reference: https://developer.apple.com/documentation/scriptingbridge/sbobject/1423973-initwithproperties @@ -354,10 +372,18 @@ def make_note(self, name: str, body: str, folder: str | None = None) -> "Note": notes.addObject_(note) len_after = len(notes) - if len_after > len_before: - return Note(note) + if len_after <= len_before: + raise ScriptingBridgeError( + f"Could not create note '{name}' with body '{body}'" + ) - raise ScriptingBridgeError(f"Could not create note '{name}' with body '{body}'") + new_note = Note(note) + if attachments: + for attachment in attachments: + if not os.path.exists(attachment): + raise FileNotFoundError(f"File {attachment} does not exist") + new_note.add_attachment(attachment) + return new_note def _noteslist( self, @@ -621,6 +647,11 @@ def attachments(self) -> list["Attachment"]: """Return list of attachments for note as Attachment objects""" return [Attachment(attachment) for attachment in self._note.attachments()] + def add_attachment(self, path: str): + """Add attachment to note""" + attachment_id = self._run_script("noteAddAttachment", path) + return Attachment(self._note.attachments().objectWithID_(attachment_id)) + def show(self): """Show note in Notes.app UI""" self._run_script("noteShow")