macscript
Script macOS applications from Python, using appscript. Start with Microsoft Word; use the vocabulary helpers to explore any scriptable app.
Requires macOS. Word automation also requires Microsoft Word. macOS may ask you to allow Automation access; dialog controls use Accessibility access, and screenshots need Screen Recording access.
Word
from macscript.word import new_doc, set_text, doc_text, save_docx, close_doc
doc = new_doc()
set_text(doc, 'Hello from Python!')
print(doc_text(doc))
doc = save_docx(doc, 'hello.docx')
close_doc(doc)
open_doc(path) opens an existing document. save_docx returns a fresh document reference because saving under a new name makes the old reference stale. save_pdf(doc, path) exports a PDF. close_doc discards unsaved changes unless passed save=True. Body text returned by doc_text uses LF line endings.
word() returns the appscript application proxy for commands not wrapped here. Document operations accept a timeout in seconds; the default is 15.
Screenshots and dialogs
win_pic(path)captures Word's front document window on the current Space. An occluded window can still be captured.win_id()returns its window ID;win2png(window_id, path)captures a window by ID.dlg_read()returns the front dialog's button names and message, orNone.dlg_click(label)clicks a named button.dismiss_dlgs()dismisses dialogs and returns their messages. It chooses OK, then No, then the first available button; use it only when that behavior is appropriate.
Dialog queries default to a three-second timeout. They use System Events so they can work while Word's own event queue is blocked by a modal dialog.
Discover scripting commands
Word's scripting dictionary is bundled, so its documentation can be searched without launching Word:
from macscript.word import sd, sdfind
print(sd('document'))
print(sd('save_as'))
sdfind('bookmark')
sd shows a command, class, or enumeration with types and descriptions. Underscores and spaces both work in names. sdfind searches names and descriptions with a case-insensitive regex and returns (kind, name, description) rows.
The generic helpers work with other appscript apps too:
from appscript import app
from macscript.asvocab import vocab, props, sd, sdfind
finder = app('Finder')
vocab(finder, 'selection|window')
props(finder, 'name')
sd('application', path='Finder.sdef')
vocab searches an app's live terminology. props reads a reference's properties as a dictionary of names and truncated representations, with optional regex filtering. Generic sd and sdfind require an explicit .sdef path; only the Word module supplies a Word default.
Development
Install the checkout and its development dependencies:
pip install -e '.[dev]'
python -m pytest -q
The default tests exercise scripting documentation without launching apps. There is no dependency on mdhtml2docx or oxml; document validation and converter acceptance checks belong to their callers. App-specific behavior lives in app modules, with reusable scripting exploration in asvocab.
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file macscript-0.1.0.tar.gz.
File metadata
- Download URL: macscript-0.1.0.tar.gz
- Upload date:
- Size: 143.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/7.0.0 CPython/3.13.15
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
752feebfeb21df04b1251b8afd19da48b2abcfc74620d07b1ee6bd8af1028231
|
|
| MD5 |
4b088a033634838caa043723b5787400
|
|
| BLAKE2b-256 |
c85d60bd3efbb6c9ac5bf28dc07f3114d27a28596b10f3616e084203ff2a9d49
|
File details
Details for the file macscript-0.1.0-py3-none-any.whl.
File metadata
- Download URL: macscript-0.1.0-py3-none-any.whl
- Upload date:
- Size: 146.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/7.0.0 CPython/3.13.15
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c02816dba13a11fe895887d50f4c1d8174a37fe2d4a5e7a588fcb68d852e6c6c
|
|
| MD5 |
1ed66ad2bde3dfe73037bf92f9ca00a2
|
|
| BLAKE2b-256 |
6fef67c8bb09320d1408376444f4863a009caaddf9efc7ea1a0c9e1e3cd28a28
|