AgentScript 101: native Swift scripts that run with Agent!'s full permissions #54
AgentiLoop
started this conversation in
General
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
AgentScript is the part of Agent! that gets the least attention and does the most heavy lifting, so here's a walkthrough.
What it is
An AgentScript is a plain Swift file in
~/Documents/AgentScript/agents/Sources/Scripts/. Agent! compiles it with SwiftPM into a.dylib, thendlopens it inside Agent!'s own process — which means the script inherits every TCC grant Agent! has: Accessibility, Automation, Calendar, Contacts, Mail, Photos, and so on. No separate permission prompts, no shelling out toosascript.About 35 examples ship in the folder:
Hello,TodayEvents,NowPlaying,CheckMail,ListReminders,QuitApps,SendMessage,CreateDmg,ArchiveXcode, and more.The minimum script
Rules: no top-level code, no
exit(). Whatever youprintgoes back to the LLM as the tool result; the return value is the exit status.Talking to Mac apps: ScriptingBridge
51 typed app bridges are pre-wired. Any
import XBridgeline is auto-detected — noPackage.swiftedits:Bridges exist for Music, Mail, Safari, Pages, Keynote, Numbers, Finder, System Events, Calendar, Contacts, Notes, Reminders, Messages, Photos, TextEdit, Preview, Terminal, QuickTime, Shortcuts, Xcode and more. When AppleScript is shorter,
NSAppleScript(source:)works in the same file — mix freely.Passing data in
The LLM never sets environment variables itself. It calls
agent_script(action:"run", name:"TodayEvents", arguments:"days=3,json=true")and Agent!'sScriptServiceexports two variables into the process:AGENT_PROJECT_FOLDERAGENT_SCRIPT_ARGSarguments:was passedThe shipped scripts use
key=value,key=valueby convention, but it's just a string —ArchiveXcodetakes positional args,CreateDmgtakes--flagstyle.For structured I/O the scripts read/write plain JSON files under
~/Documents/AgentScript/json/(SendMessage_input.json,TodayEvents_output.json, …) withJSONSerialization. That's a separate mechanism from the env vars — Agent! doesn't create or parse those files, the script does.Lifecycle
agent_scriptsupportslist/read/create/update/edit/run/delete/combine/restore/pull/list_backups.deletenever loses work — the script goes to~/Documents/AgentScript/agents/.Trash/andrestorebrings it back.pullfetches the upstream original from the AgentScripts repo if you want to reset a script you've modified.Why not just AppleScript?
You can — Agent! has
applescriptandjavascript(JXA) tools too. AgentScript is for when you want real Swift: typed APIs, Foundation, URLSession, EventKit, Vision, whatever. It's the escape hatch that turns "the agent can run a command" into "the agent can write a native program and run it with full permissions."If you've written a script worth sharing, post it in Show and tell — or open a PR against the AgentScripts repo.
All reactions