do shell script >> show results

Hi,

i am trying to get the results in a seperate window

activate
tell application "Terminal"
	activate
	do shell script "/private/etc/periodic/daily/500.daily; echo some text " password "XX" with administrator privileges
end tell

That gives the results in the ASeditor window. I would like them in a separate window.


I’ve tried this approach but get a small window that doesn’t show all the results.

set x to do shell script "date" 
 display dialog x

TIA, Jim

What might be a better method is to try writing the results to a file. The display dialog command was not intended to display large amounts of text, I don’t believe. You could even have the script then automatically open the newly created file in TextEdit.

Look in the Standard Additions library for commands to write to a file.

Thanks justin,

I have been trying variations of

write "standard output" as text to file "~/Desktop/results.txt"

without much luck so far. Also tried echo and printf.

My goal is really to have the results displayed in a window, with scollbars if necessary or even the Terminal.

Any help would be appreciated.

Thanks, Jim

You’re mixing your shell commands.

‘do shell script’ is a standard addition that (silently) runs the shell command and returns the result to your script where you can do whatever you like with it (ignore it, write it to a file, etc.). It in no way relates to terminal.app, and doesn’t need to be in a ‘tell application “Terminal”’ block.

‘do script’ on the other hand, is a terminal.app command for (essentially) typing a shell command into a terminal window. As a result, the output of the command will be displayed in the terminal window, just as if you’d typed the command yourself. Maybe that’s sufficient for your needs?

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

Note that ‘do script’ essentially types the string in the window just as if you’d typed it yourself, so you can’t use ‘with administrator privileges’. Instead, to run as root you’ll need to use sudo, just like you would if you typed the command yourself.

thanks again Camelot,

I do see the difference between the two. The question is still how to show the output.

So I see i don’t need the activate Terminal and tell block. I am left with a script that simply states this.

do shell script '/private/etc/periodic/monthly/500.monthly' with administrator privileges

Can I get the standard output of 500.monthly (or the results from AppleScript) to be displayed in a window.

Thanks, jim

try this:

activate
set commandOutput to do shell script "/private/etc/periodic/daily/500.daily; echo some text " password “XX” with administrator privileges

set fp to open for access “YOURHD:SOMEFOLDER:SOMEFILE.txt” with write permission
write commandOutput to fp
close access fp

Note that I’m using the Macintosh file path convention which seperates directories with a “:”.

Okay that works as far as leaving a text file with the results. Thank you for that.

And thanks for reminding me that AS uses colons instead of slashes in filepaths- very annoying that little thing.

:arrow: Now again I ask - Can I get the results to a window?

I’m not clear on what type of a window to you want to send the output to, so I’ll have to leave that up to you.

sudo /private/etc/periodic/monthly/500.monthly tee

The above ran in the terminal.app will output the standard i/o to the open terminal window using the ‘tee’ command. hth

Thanks Greg,

I am already getting that in the Terminal with just

sudo /private/etc/periodic/monthly/500.monthly

What I am struggling with is how to capture the standard output that is printing to the Terminal and display that information to a window.
I would prefer to use the “do shell script” and not even open the Terminal.
So I am starting with

 do shell script "/private/etc/periodic/monthly/500.monthly" with administrator privileges

According to the Apple Technote on shell scripting
Q: How does ‘do shell script’ get the result?

A: Shell commands can write their results to one of two output streams: standard output and standard error

So maybe a better phrasing of my question is how can I capture that standard output from the previous shell command and display it somehow.

As you can probably tell I am not an experienced AppleScripter, but I can see the power of the shell scripting commands. It’s in the AS semantics and cross talk from one application to the next that it seems to get confusing for me.

Thanks again, jim

I see, thanks Jim. Maybe you can use this…

display dialog (do shell script "sudo /private/etc/periodic/monthly/500.monthly")

If the output is more than the display dialog window can handle, it will truncate it. There may be a way to set the output to a dialog window using TIDs. Not sure though.

Are you trying to get the entire output of the command, or just an indication that the command completed succesfully? If it’s the latter then maybe this will work for you…

do shell script "sudo /private/etc/periodic/daily/500.daily"
set x to the result as text
display dialog (summarize x) & return & "Command completed"
--> Subject: My-Names-Computer.local.
--> Command completed

Otherwise the output is much too long for a standard dialog window.

Yes alas, All of it. It’s not that much, but more than the dialog window will hold. Especially since the hoped for end result is the output of daily, weekly and monthly.

Thanks, Jim

That information is already written to your daily, weekly, & monthly.out files everytime you run the periodic command, it’s in your /var/log directory. You could simply open them to get the data.

Or you could use the redirect the command to write the output to a new file…

do shell script "sudo /private/etc/periodic/daily/500.daily >$HOME/daily.txt;open $HOME/daily.txt" with administrator privileges

Which will create the text file named “daily.txt” in your Home directory, then open it.

NOTE: The single redirect (>) will overwrite the file upon each run, if you want to append the daily.txt file on each run then use a double redirect (>>)

Thanks Greggo! That’s great :!:

So after banging my head against the wall for a while I got the basics of what I want to do.

do shell script "sudo /private/etc/periodic/daily/500.daily >$HOME/Desktop/CRON_RESULTS.txt" password "FOO" with administrator privileges
do shell script "sudo /private/etc/periodic/weekly/500.weekly >>$HOME/Desktop/CRON_RESULTS.txt" password "FOO" with administrator privileges
do shell script "sudo /private/etc/periodic/monthly/500.monthly >>$HOME/Desktop/CRON_RESULTS.txt;open $HOME/Desktop/CRON_RESULTS.txt" password "FOO" with administrator privileges

do shell script "rm /System/Library/Extensions.kextcache; rm /System/Library/Extensions.mkext; rm -r /Library/Caches/; rm -r /Users/USER1/Library/Caches/; rm -r /Users/USER2/Library/Caches/; rm -r /Users/USER3/Library/Caches/; rm -r /Users/USER4/Library/Caches/" password "FOO" with administrator privileges

do shell script "reboot" with administrator privileges

Works for me . . . although I couldn’t delete all users caches using a wildcard like Users/*/LibraryCaches
and it would be nicer to see the output as it happened instead of just at the end. But it’s pretty much what i wanted to do. Maybe when i look at it further i can give it some dialog buttons to wait for user feedback as to what operations to do or skip.

Now does anyone know if you can run the Repair Permissions from the command line :lol:

To remove the caches you can use …

sudo rm -r ~/Library/Caches /Library/Caches /System/Library/Caches  --> not a great idea, as caches are a good thing

To repair permissions via the CLI

diskutil repairPermissions /

I have nothing against caches - just that I think they are good to refresh every once in a while as routine maintenance. The wildcard attempt was to remove all other Users caches as well as my own.

A word of warning about storing your admin password in scripts, especially with other users on your machine… see this thread (interesting)

http://bbs.applescript.net/viewtopic.php?t=4635&highlight=run+strings

Also, I do agree with you on the cache thingies. I just thought it was a good idea to mention that they are a good thing. Later Jim.

As for the password, once i get it working it will be the intention to have the user enter it. I guess I could do shell scripts with administrator privileges (leave password out) and have the OS dialog ask for it.

I’m now doing this in PB. I am trying to have a textField (applescript name - ‘password’) and then

set pw to contents of text field "password"

and use ‘pw’ in the do shell scripts

like

do shell script "command" password "pw" with administrator privileges

Does that sound right?

Thanks, jim

Yup, I just finished a script that uses this command. In my script, I obtain the username via the shell’s whoami and the password from my keychain. This offers an acceptable level of security for my situation.

– Rob