I use iTerm instead of Terminal, so I'm not sure if this code catches all conditions, but you can do this:
Open Alfred Preferences, click the Features tab and click Terminal / Shell in the sidebar.
Set Application to Custom and paste this script in the large textfield:
on alfred_script(q)
tell application "Terminal"
if exists window 1 then
do script q in window 1
else
do script q
end if
activate
end tell
end alfred_script
Close Alfred Preferences to save the changes.
Note that when you run a Terminal command manually with Alfred (by prefixing it with ">"), it will now also run in the existing window.
Perfect! I've used a bit of AppleScript I've been using with Emacs for years:
on alfred_script(q)
tell application "Terminal"
try
if (count of windows) is 0 then
do script q
else
do script q in window 1
end if
activate
on error
beep
end try
end tell
end alfred_script
That works a treat: if I already have a Terminal window open it'll send the commands there, otherwise it'll create a new window.