[How To] Applescript the command line

Discussion in 'HOWTO & FAQs' started by Urbansory, Dec 17, 2003.

  1. Urbansory

    Urbansory The Definition of...

    Joined:
    Apr 3, 2002
    Messages:
    1,181
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    Atlanta, GA
    I created routine maintenance Applescript out of curiosity and the need for a free solution that I could setup to run my daily/weekly and monthly.

    Preparation--
    I have mine setup to run from Entourage X, but i believe you can use iCal to run them on a timed schedule, or simply run them as applications when you see fit or from within Script Editor, where you can use the Result window to show the script processing *More on this later.
    Setting up a new Key in Keychain------
    The Settings I used are as follows:

    Name: applescriptAllow
    Kind: application password
    Account: *Your User account
    Where: applescriptallow
    Password: Your user account password/ *Administrator password

    The Script------
    property theAppName : "weekly run script"
    property myKeyName : "applescriptAllow" --Name of the key in Keychain Access

    -------------------------Other global properties-----------------------------
    property fromRun : false --whether we have entered the main run loop yet (true) or not (false)


    on theCommands(thePassword)

    --This runs the actual script using your Administrator password
    --Substitute "weekly" for "daily" or monthly" to run those scripts

    do shell script "sudo /etc/weekly" password thePassword with administrator privileges


    do shell script "sudo -k" --timeout our sudo ability for security

    end theCommands


    (*
    * Main handler.
    *)
    on run


    set {thekey} to getKey()



    tell application "Keychain Scripting" to set thePassword to password of thekey

    theCommands(thePassword)

    --reinitialize fromRun for the next time the script is run.

    end run


    on getKey()
    set validKey to false
    set changedPassword to false

    tell application "Keychain Scripting"
    set theKeychain to current keychain

    tell theKeychain


    set thekey to every generic key of theKeychain whose name is myKeyName
    if thekey = {} then
    set thekey to my makeKey()
    set firstTime to true
    set validKey to true
    else
    set thekey to item 1 of thekey
    set firstTime to false
    end if

    end tell --end tell theKeychain

    end tell --end tell application Keychain Scripting

    repeat while not validKey
    tell application "Keychain Scripting" to set thePassword to the password of thekey

    try
    do shell script "sudo -k" --timeout our sudo ability so the following test is valid
    do shell script "ls /private/var/root/" password thePassword with administrator privileges
    do shell script "sudo -k" --timeout our sudo ability for security
    set validKey to true

    on error
    set thePassword to text returned of ¬
    (display dialog "The Administrator password stored by “" & theAppName & "” is no longer valid. Please enter your valid Administrator password.

    NOTE: the password will be displayed in plain text, but will be securly stored in your keychain." default answer "" with icon caution)

    set changedPassword to true
    set validKey to false

    end try
    end repeat

    if changedPassword then tell application "Keychain Scripting" to ¬
    set the password of thekey to thePassword

    return {thekey}
    end getKey​

    The Script End------

    How to setup this script with Entourage X coming soon.

    Initial script post 12-17-03
     
  2. Urbansory

    Urbansory The Definition of...

    Joined:
    Apr 3, 2002
    Messages:
    1,181
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    Atlanta, GA

Share This Page