Skip to content

Commit

Permalink
Initial support for adding attachments, #15
Browse files Browse the repository at this point in the history
  • Loading branch information
RhetTbull committed Jan 3, 2023
1 parent 860ea87 commit 710cb53
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 7 deletions.
12 changes: 12 additions & 0 deletions macnotesapp/macnotesapp.applescript
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
12 changes: 12 additions & 0 deletions macnotesapp/macnotesapp_applescript.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
45 changes: 38 additions & 7 deletions macnotesapp/notesapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit 710cb53

Please sign in to comment.