Currently Being ModeratedOct 27, 2012 8:10 PM (in response to yellowb0x)
You need to use:
do shell script "cd ~/Downloads/example;"
and then the second command in the first do shell script line. If not done, the cd won't work, because it targets the top level of the drive and not the home folder, and if it did, its effect wouldn't carry over into the second shell command.
Currently Being ModeratedOct 27, 2012 10:06 PM (in response to yellowb0x)
For some reason the shell script couldn't locate the file "downloads/example" when it was clearly there
You have to consider/realize that each 'do shell script' command is really its own entity. It knows nothing of any other commands run in any other 'do shell script' command.
The solution, therefore, is to wrap all your commands in a single 'do shell script' statement. You can separate commands with a semi-colon, e.g.:
do shell script "cd ~/downloads/example; othercommand -a -b -c; someothercommand -x -y -z"
As for your other approach of using Terminal.app (which isn't recommended if you can get do shell script to work), the problem to that one lies in the fact that you're saying:
do script "..." in problem1
set savedresult to result
Terminal.app's dictionary states that the result of 'do script' is the tab the command was executed in, and you cannot convert a tab to text. So instead of:
display dialog savedresult
display dialog contents of savedresult
Where 'contents' of a tab indicates the text within the window. There are several caveats to this, though. One is that the contents will contain the whole window text, not just the output of the last command. The second is that there's no concept of command execution - that is the script doesn't care if the command takes some time to execute - it wil grab the contents of the window, even if the command hasn't yet finished.
For these reasons (and more) use do shell script and eschew Terminal.app altogether.
Currently Being ModeratedOct 28, 2012 8:16 AM (in response to yellowb0x)
You've gotten some good feedback from folks here.
What's overcomplicating things here is arguablty the use of AppleScript. AppleScript is intended for scripting OS X GUI application operations. Beyond that particular domain and requirement, it's often not the most expeditious choice.
For scripting Unix-level activities, bash and Python and various other scripting languages are often more appropriate; for non-GUI-scripting tasks.
And FWIW, Terminal.app is just a window into a native Unix command shell; it's nothing special as far as scripting applications go. It's the Unix level you're working with here, and an ssh or telnet session into your OS X system could run the same bash scripts; no Terminal.app required. Put another way, you can invoke shell scripts from contexts other than Terminal.app, including directly from the GUI; the shell scripts can be (are) applications.
Invoking GUI dialog boxes from bash (or another shell) is possible with the osascript command that's available on OS X, if you need to prompt for stuff. (But this assumes that the bash script is running with the GUI around.) For instance:
osascript -e 'tell app "System Events" to display dialog "Your Text Here"'
Having gone a very long way 'round to my actual question, what are you up to here? What problem(s) are you solving with this AppleScript? Scripting Terminal.app might well be the shortest route, but there might be an alternative route, and possibly a better route.
Currently Being ModeratedNov 14, 2012 1:08 PM (in response to Camelot)
Unfortuanately this is not working for me. Here is a sample I tried to make to get this working.
tell application "Terminal"
do script "pwd" in window 1
set testVar to result
display dialog contents of testVar
but I still get this errror
"Terminal got an error: Can’t make tab 1 of window id 1447 into type string." number -1700 from tab 1 of window id 1447 to string
I have re-read your reply several times and I still have not been able to pull out why this is not working for me. I am on Mac 10.8
Currently Being ModeratedDec 16, 2013 2:32 PM (in response to ninjon)
I feel your pain ninjon. After numerous futile attempts to access the contents of the Terminal window tab returned by do script, I noticed something. If I actually spelled out the whole object with tab and window indices, it would happily return the contents. So here's a little routine which looks through the windows and tabs until it finds the one in question and returns its contents. (It uses the tty strings to find the matching tab. For whatever reason, the tty property could be read from the original tab object where the contents could not.) This code was tested on my iMac running Mavericks. Hope it works for you too!
tell application "Terminal" to set termTab to do script "pwd"
set tabText to contentsOfTerminalTab(termTab)
tell application "Terminal"
set termTabTTY to tty of termTab
set iWindow to 1
set tabCount to number of tabs in window iWindow
repeat with iTab from 1 to tabCount
set windowTabTTY to tty of tab iTab of window iWindow
if windowTabTTY is termTabTTY then return contents of tab iTab of window iWindow
set iWindow to iWindow + 1
Currently Being ModeratedJan 22, 2014 1:14 PM (in response to Edward K. Chew)
I submitted a bug report to Apple over this and learned that what is returned by "do script" is a reference to the window tab and not the tab itself. You need to specify "contents of" to get to the object and then "contents of" again to get the text in the object. So here's what you need to do:
tell application "Terminal" to set termTabRef to do script "pwd"
set tabText to contents of contents of termTabRef