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.
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.
‘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.
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.
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.
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 (>>)
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:
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.
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
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.