Source code for PyXA.apps.Drafts

""".. versionadded:: 0.0.8

Control Drafts using JXA-like syntax.
"""

from datetime import datetime
from typing import Union, Any
from enum import Enum

from PyXA import XABase
from PyXA import XABaseScriptable
from ..XAProtocols import XACanOpenPath


[docs] class XADraftsApplication(XABaseScriptable.XASBApplication, XACanOpenPath): """A class for managing and interacting with Drafts.app. .. versionadded:: 0.0.8 """
[docs] class ObjectType(Enum): """Object types that can be created using :func:`XADraftsApplication.make`.""" DRAFT = "draft"
def __init__(self, properties): super().__init__(properties) @property def name(self) -> str: """The name of the application.""" return self.xa_scel.name() @property def version(self) -> str: """The version of Drafts.app.""" return self.xa_scel.version()
[docs] def new_draft(self, content: str) -> "XADraftsDraft": """Creates a new draft with the given name and content :param content: The full content of the draft (the first line is the name) :type content: str :return: The newly created draft object :rtype: XADraftsDraft .. versionadded:: 0.0.8 """ new_draft = self.make("draft", {"content": content}) self.drafts().push(new_draft) print(self.drafts().content()) return self.drafts().last()
[docs] def drafts(self, filter: Union[dict, None] = None) -> "XADraftsDraftList": """Returns a list of drafts, as PyXA-wrapped objects, matching the given filter. :param filter: Keys and values to filter drafts by, defaults to None :type filter: dict, optional :return: A PyXA list object wrapping a list of drafts :rtype: XADraftsDraftList .. versionadded:: 0.0.8 """ return self._new_element(self.xa_scel.drafts(), XADraftsDraftList, filter)
[docs] def make( self, specifier: Union[str, "XADraftsApplication.ObjectType"], properties: dict = None, data: Any = None, ): """Creates a new element of the given specifier class without adding it to any list. Use :func:`XABase.XAList.push` to push the element onto a list. :param specifier: The classname of the object to create :type specifier: Union[str, XADraftsApplication.ObjectType] :param properties: The properties to give the object :type properties: dict :param data: The data to initialize the object with, defaults to None :type data: Any, optional :return: A PyXA wrapped form of the object :rtype: XABase.XAObject .. versionadded:: 0.0.8 """ if isinstance(specifier, XADraftsApplication.ObjectType): specifier = specifier.value if data is None: camelized_properties = {} if properties is None: properties = {} for key, value in properties.items(): if key == "url": key = "URL" camelized_properties[XABase.camelize(key)] = value obj = ( self.xa_scel.classForScriptingClass_(specifier) .alloc() .initWithProperties_(camelized_properties) ) else: obj = ( self.xa_scel.classForScriptingClass_(specifier) .alloc() .initWithData_(data) ) if specifier == "draft": return self._new_element(obj, XADraftsDraft)
[docs] class XADraftsDraftList(XABase.XAList): """A wrapper around a list of drafts. .. versionadded:: 0.0.8 """ def __init__(self, properties: dict, filter: Union[dict, None] = None): super().__init__(properties, XADraftsDraft, filter)
[docs] def id(self) -> list[str]: return list(self.xa_elem.arrayByApplyingSelector_("id") or [])
[docs] def name(self) -> list[str]: return list(self.xa_elem.arrayByApplyingSelector_("name") or [])
[docs] def content(self) -> list[str]: return list(self.xa_elem.arrayByApplyingSelector_("content") or [])
[docs] def flagged(self) -> list[bool]: return list(self.xa_elem.arrayByApplyingSelector_("flagged") or [])
[docs] def tags(self) -> list[list[str]]: return list(self.xa_elem.arrayByApplyingSelector_("tags") or [])
[docs] def created_at(self) -> list[datetime]: return list(self.xa_elem.arrayByApplyingSelector_("createdAt") or [])
[docs] def modified_at(self) -> list[datetime]: return list(self.xa_elem.arrayByApplyingSelector_("modifiedAt") or [])
[docs] def accessed_at(self) -> list[datetime]: return list(self.xa_elem.arrayByApplyingSelector_("accessedAt") or [])
[docs] def by_id(self, id: str) -> Union["XADraftsDraft", None]: return self.by_property("id", id)
[docs] def by_name(self, name: str) -> Union["XADraftsDraft", None]: return self.by_property("name", name)
[docs] def by_content(self, content: str) -> Union["XADraftsDraft", None]: return self.by_property("content", content)
[docs] def by_flagged(self, flagged: bool) -> Union["XADraftsDraft", None]: return self.by_property("flagged", flagged)
[docs] def by_tags(self, tags: list[str]) -> Union["XADraftsDraft", None]: return self.by_property("tags", tags)
[docs] def by_created_at(self, created_at: datetime) -> Union["XADraftsDraft", None]: return self.by_property("createdAt", created_at)
[docs] def by_modified_at(self, modified_at: datetime) -> Union["XADraftsDraft", None]: return self.by_property("modifiedAt", modified_at)
[docs] def by_accessed_at(self, accessed_at: datetime) -> Union["XADraftsDraft", None]: return self.by_property("accessedAt", accessed_at)
[docs] class XADraftsDraft(XABase.XAObject): """A draft in Drafts.app. .. versionadded:: 0.0.8 """ def __init__(self, properties): super().__init__(properties) @property def id(self) -> str: """The unique identifier of the draft.""" return self.xa_elem.id() @property def name(self) -> str: """The first line of the draft.""" return self.xa_elem.name() @property def content(self) -> str: """The content of the draft.""" return self.xa_elem.content() @content.setter def content(self, content: str): self.set_property("content", content) @property def flagged(self) -> bool: """The flagged status of the draft.""" return self.xa_elem.flagged() @flagged.setter def flagged(self, flagged: bool): self.set_property("flagged", flagged) @property def tags(self) -> list[str]: """The tags assigned to the draft.""" return self.xa_elem.tags() @tags.setter def tags(self, tags: list[str]): self.set_property("tags", tags) @property def created_at(self) -> datetime: """The date the draft was created.""" return self.xa_elem.createdAt() @property def modified_at(self) -> datetime: """The date the draft was last modified.""" return self.xa_elem.modifiedAt() @property def accessed_at(self) -> datetime: """The date the draft was last accessed.""" return self.xa_elem.accessedAt() @property def permalink(self) -> str: """The URL of the draft.""" return self.xa_elem.permalink() def __repr__(self): return "<" + str(type(self)) + str(self.name) + ">"