Programmatically generate macOS/iOS shortcut files (.shortcut) to implement automated workflows.
Use Cases
- The user says "Help me create a shortcut that automatically opens my work app every day"
- The user says "Make a shortcut that takes a screenshot and sends it by email"
- The user says "Create a shortcut that automatically organizes the Downloads folder"
Execution Method
How It Works
A shortcut is essentially a plist file in .shortcut format, which can be generated and imported via Python.
Generate a Shortcut
import plistlib
import uuid
def make_action(identifier, parameters=None):
action = {
"WFWorkflowActionIdentifier": identifier,
"WFWorkflowActionParameters": parameters or {},
}
return action
shortcut = {
"WFWorkflowMinimumClientVersion": 900,
"WFWorkflowMinimumClientVersionString": "900",
"WFWorkflowActions": [
make_action("is.workflow.actions.openapp", {
"WFAppIdentifier": "com.apple.Safari",
}),
make_action("is.workflow.actions.notification", {
"WFNotificationActionBody": "工作模式已启动!",
}),
],
"WFWorkflowClientVersion": "2612.0.4",
"WFWorkflowHasOutputFallback": False,
"WFWorkflowIcon": {
"WFWorkflowIconStartColor": 4282601983,
"WFWorkflowIconGlyphNumber": 59511,
},
"WFWorkflowImportQuestions": [],
"WFWorkflowInputContentItemClasses": [],
"WFWorkflowOutputContentItemClasses": [],
"WFWorkflowTypes": ["NCWidget", "WatchKit"],
}
output_path = "/tmp/work_mode.shortcut"
with open(output_path, "wb") as f:
plistlib.dump(shortcut, f)
Import a Shortcut
open /tmp/work_mode.shortcut
The system will display the import confirmation screen of the Shortcuts app.
Common Action Identifiers
| Function | Identifier |
|---|---|
| Open App | is.workflow.actions.openapp |
| Send Notification | is.workflow.actions.notification |
| Get Clipboard | is.workflow.actions.getclipboard |
| Run Shell Script | is.workflow.actions.runscript |
| Send Email | is.workflow.actions.sendmail |
| Show Alert | is.workflow.actions.alert |
Output Specifications
- After generating the .shortcut file, prompt the user to double-click to import it
- Explain the shortcut's function and trigger method
- For complex shortcuts, list the steps first for the user to confirm