Do script in Terminal with results returned to Applescript

I’m a bit stuck between “do shell script” and “do script” in terms of the Terminal.app

My goal is to be able to be anywhere in any directory in my current Terminal window. Even ssh shomewhere, and be able to activate an applescript that runs a command. The problem I have is that I need the results to also be returned to the applescript as well as the normal display on my Terminal window.

So when I do:

set theCommand to "ls -lash"

do shell script theCommand

This is great. I get the results back in the applescript.

But if I do:

set theCommand to "ls -lash"

tell application "Terminal"
	activate
	do script theCommand in window 1
end tell

This is also great, in that it performs the command in my front window. But the applescript is not able to receive the results.

Hi.

I think this just reflects the way the Terminal’s used normally: you type commands one after another and any results either appear in the window or change the effect of what you type next. do script is just a way of automating the typing.

It’s possible to use do script and then get the contents of the tab in which it was executed. But 1) you have to wait until the shell script’s finished before looking, and 2) you can only get either the tab’s entire contents (its history) or literally just what’s currently visible in the frame (its contents). Neither’s ideal for your purposes. You’d have to parse what you want from the returned text.

set theCommand to "ls -lash"

tell application "Terminal"
	tell (do script theCommand in front window) -- tell the tab in which the script's executed.
		repeat while busy
			delay 0.2
		end repeat
		-- To get the tab's entire contents (probably more useful):
		set windowContents to history
		-- Or, to get just what's currently showing:
		-- (We need the tab's 'contents' property rather than the contents of the tab specifier!)
		-- set windowContents to contents of (get properties)
	end tell
end tell
2 Likes

Thanks Nigel. I had a feeling that was the situation. At the moment I’m looking into using iTerm instead. It has some Python integration that can grab all characters in the response.

Even then; it requires some changes to the users terminal session settings to allow a hidden character to show the end of the response.

Hoping I can kind of bridge that into Applescript; but maybe it’s not meant to be.

Hi @mk500.

It occurs to me after a night’s sleep that this isn’t actually difficult! :roll_eyes:

set theCommand to "ls -lash"

tell application "Terminal"
	activate
	tell (do script theCommand in front window) -- tell the tab in which the script's executed.
		repeat while busy
			delay 0.2
		end repeat
		-- Get the tab's entire contents.
		set windowContents to history
	end tell
end tell

-- Get the text which follows the last instance of theCommand in the above result.
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to theCommand
set newStuff to text item -1 of windowContents
set AppleScript's text item delimiters to astid

-- Trim unwanted paragraphs.
set theResult to text from paragraph 2 to paragraph -3 of newStuff
1 Like

Another approach that returns the exact result of the command without dealing with history, or cluttering the Terminal window.

use scripting additions

set omitCols to "$1=$2=$3=$4=$5=\"\""

-- get file size, date/time, and filename columns without dealing with history
-- the -lash syntax will sometimes only return the time in the year column (not caused by this script)
set theCommand to "script -q -a - " & "ls -lash --color=never | awk  '{" & omitCols & "} 1' | column -c 5 | pbcopy"
tell application "Terminal"
	activate
	do script theCommand in front window
	delay 1
	set termOut to the clipboard
end tell
return termOut

Tested: macOS Tahoe 26.5.2