Skip navigation
This discussion is archived

General Scripting Help

1905 Views 7 Replies Latest reply: Mar 12, 2010 6:42 AM by Chachi RSS
SnakeDog Level 1 Level 1 (0 points)
Currently Being Moderated
Mar 11, 2010 1:44 PM
I am having some problems with getting a script to end before the next one starts. I tried using 'do shell script' but couldn't get the 'su' and 'sudo -s' commands to work properly.

I am pretty new to applescripts and any help would be great. Thanks


--------------------------------
Applescript #1 - It works, but only if the delays are long enough for things to finish. Sometimes it takes longer, sometimes it's ok.
--------------------------------

set pass to ""
tell application "Terminal"
activate
display dialog "Please enter your password. Everything else is automated. Please do not press anything beyond this password and the final \"Success\" window." default answer pass buttons {"OK"} default button 1
set pass to text returned of the result
do script "su \"USER NAME\"" in front window
delay 2
do script pass in front window
delay 2
do script "sudo -s" in front window
delay 2
do script pass in front window
delay 2
do shell script "date '+%Y.%m.%d'"
set datelist to result
time of (current date)
set timelist to result
do script "cd /System/Library" in front window
delay 2
do script "tar cfz UserTemplateBackup" & datelist & timelist & ".tar.gz \"User Template\"" in front window
delay 8
do script "cd /Users/Guest" in front window
delay 2
do script "cp .DS_Store \"/System/Library/User Template/English.lproj/.DS_Store\"" in front window
delay 2
do script "rm -R \"/System/Library/User Template/English.lproj/Library/\"" in front window
delay 2
do script "cp -R Library/ \"/System/Library/User Template/English.lproj/Library\"/" in front window
delay 2
do script "rm -R \"/System/Library/User Template/English.lproj/Desktop\"/" in front window
delay 2
do script "cp -R Desktop/ \"/System/Library/User Template/English.lproj/Desktop\"/" in front window
delay 2
do script "rm -R \"/System/Library/User Template/English.lproj/Documents\"/" in front window
delay 2
do script "cp -R Documents/ \"/System/Library/User Template/English.lproj/Documents\"/" in front window

display dialog "SUCCESS!!! (I think)" buttons {"OK"} default button 1
quit
end tell

--------------------------------

Applescript #2 - This one doesn't work, it returns errors at several steps.

--------------------------------


display dialog "Are you sure that you want to save the Guest User Settings? Everything else is automated. Please do not press anything until the final \"Success\" window." buttons {"OK"} default button 1
do shell script "su" with administrator privileges
do shell script "sudo -s" with administrator privileges
do shell script "date '+%Y.%m.%d'"
set datelist to result
time of (current date)
set timelist to result
do shell script "cd /System/Library"
do shell script "tar cfz UserTemplateBackup" & datelist & timelist & ".tar.gz \"User Template\""
do shell script "cd /Users/Guest"
do shell script "cp .DS_Store \"/System/Library/User Template/English.lproj/.DS_Store\""
do shell script "rm -R \"/System/Library/User Template/English.lproj/Library/\""
do shell script "cp -R Library/ \"/System/Library/User Template/English.lproj/Library\"/"
do shell script "rm -R \"/System/Library/User Template/English.lproj/Desktop\"/"
do shell script "cp -R Desktop/ \"/System/Library/User Template/English.lproj/Desktop\"/"
do shell script "rm -R \"/System/Library/User Template/English.lproj/Documents\"/"
do shell script "cp -R Documents/ \"/System/Library/User Template/English.lproj/Documents\"/"
display dialog "SUCCESS!!! (I think)" buttons {"OK"} default button 1

--------------------------------
Thanks
Apple Macbook - Unibody, Mac OS X (10.6.2), Also Windows 7 32bit installed via bootcamp
  • red_menace Level 6 Level 6 (14,275 points)
    Currently Being Moderated
    Mar 11, 2010 3:27 PM (in response to SnakeDog)
    Using do shell script in an AppleScript will wait for the shell script to complete before proceeding. A couple of the problems with your second AppleScript:

    1) sudo and with administrator privileges should not be used together;
    2) each do shell script statement runs in it's own (separate) shell, so you will need to combine those statements that set and use a particular working directory.

    What exactly are you trying to do here? The user account templates really shouldn't be messed with.
    MacBook Pro, Mac OS X (10.6.2), + [Leopard] / G4 Digital Audio, G4 mini [Tiger 10.4.11] / G3 MiniTower [OS 9.2]
  • Chachi Level 4 Level 4 (2,525 points)
    Currently Being Moderated
    Mar 11, 2010 4:11 PM (in response to SnakeDog)
    Here's a completely UNTESTED version of your script. All I've done is attempt to clean up the syntax.

    <pre style="width:630px;height:auto;overflow-x:auto;overflow-y:hidden;"
    title="Copy this code and paste it into your Script Editor application.">
    --can't figure out how having just an OK button gave the user any options?!
    display dialog "Please enter your password. Everything else is automated. Please do not press anything beyond this password and the final "Success" window." default answer "" default button 1 with hidden answer
    set pass to text returned of result

    set datelist to do shell script "date '+%Y.%m.%d'"
    set timelist to time of (current date)

    do shell script "cd /System/Library/;
    tar cfz UserTemplateBackup" & datelist & timelist & ".tar.gz 'User Template';
    cd /Users/Guest/;
    cp .DS_Store '/System/Library/User Template/English.lproj/.DS_Store';
    rm -R '/System/Library/User Template/English.lproj/Library/';
    cp -R Library/ '/System/Library/User Template/English.lproj/Library/';
    rm -R '/System/Library/User Template/English.lproj/Desktop/';
    cp -R Desktop/ '/System/Library/User Template/English.lproj/Desktop/';
    rm -R '/System/Library/User Template/English.lproj/Documents/';
    cp -R Documents/ '/System/Library/User Template/English.lproj/Documents/'" password pass with administrator privileges

    display dialog "SUCCESS!!! (I think)" buttons {"OK"} default button 1
    </pre>

    Hope this helps...
    iMac 20" 2.66GHz, iBook G4 1GHz, Mac OS X (10.6.2), iMac 2 GB RAM, iBook 1.12 GB RAM
  • red_menace Level 6 Level 6 (14,275 points)
    Currently Being Moderated
    Mar 11, 2010 7:14 PM (in response to SnakeDog)
    By the way, you can use the *Workgroup Manager* of the Server Admin Tools to customize a user account without needing to mangle the template.
    MacBook Pro, Mac OS X (10.6.2), + [Leopard] / G4 Digital Audio, G4 mini [Tiger 10.4.11] / G3 MiniTower [OS 9.2]
  • Chachi Level 4 Level 4 (2,525 points)
    Currently Being Moderated
    Mar 12, 2010 6:42 AM (in response to SnakeDog)
    Glad to hear you got it working and have it set up better for what you need! To answer your previous question... yes, that is what the semicolon is used for.
    iMac 20" 2.66GHz, iBook G4 1GHz, Mac OS X (10.6.2), iMac 2 GB RAM, iBook 1.12 GB RAM

Actions

More Like This

  • Retrieving data ...

Bookmarked By (0)

Legend

  • This solved my question - 10 points
  • This helped me - 5 points
This site contains user submitted content, comments and opinions and is for informational purposes only. Apple disclaims any and all liability for the acts, omissions and conduct of any third parties in connection with or related to your use of the site. All postings and use of the content on this site are subject to the Apple Support Communities Terms of Use.