property erreur : 0
tell application "Finder"
    set actu to do shell script "pmset -g sched"
    set angl to {"poweron", "wakepoweron", "sleep", "restart", "shutdown", "weekdays only", "weekends only", "every day", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"}
    set franc to {"Démarrer ou réactiver", "Démarrer ou réactiver", "Suspendre", "Redémarrer", "Eteindre", "en semaine", "le week-end", "tous le jours", "lundi", "mardi", "mercredi", "jeudi", "vendredi", "samedi", "dimanche"}
    if actu is "" then
        display dialog "Pas de programmation actuellement ! "
    else
        set affiche to ""
        set affiche1 to ""
        set sauv to AppleScript's text item delimiters --sauvegarder la variable delimiteur
        set AppleScript's text item delimiters to {" "}
        set a to text item 3 of paragraph 2 of actu
        set h to text item 5 of paragraph 2 of actu
        set j to text items 6 thru -1 of paragraph 2 of actu as string
        set affiche to a & " à " & h & " " & j
        if (count paragraph of actu) is 3 then
            set a1 to text item 3 of paragraph 3 of actu
            set h1 to text item 5 of paragraph 3 of actu
            set j1 to text items 6 thru -1 of paragraph 3 of actu as string
        end if
        set AppleScript's text item delimiters to {""}
        set mn to items -4 thru -3 of h as string
        set apm to items -2 thru -1 of h as string
        set AppleScript's text item delimiters to {":"}
        set hf to text item 1 of h
        if apm is "pm" then set hf to hf + 12
        repeat with i from 1 to count of angl
            if item i of angl is a then set pl to i
        end repeat
        set af to item pl of franc
        repeat with i from 1 to count of angl
            if item i of angl is j then set pl to i
        end repeat
        set jf to item pl of franc
        if (count paragraph of actu) is 3 then
            set AppleScript's text item delimiters to {""}
            set mn to items -4 thru -3 of h1 as string
            set apm to items -2 thru -1 of h1 as string
            set AppleScript's text item delimiters to {":"}
            set hf1 to text item 1 of h1
            if apm is "pm" then set hf1 to hf1 + 12
            repeat with i from 1 to count of angl
                if item i of angl is a1 then set pl to i
            end repeat
            set af1 to item pl of franc
            repeat with i from 1 to count of angl
                if item i of angl is j1 then set pl to i
            end repeat
            set jf1 to item pl of franc
            set affiche1 to af1 & " " & jf1 & " à " & hf1 & "H" & mn
        end if
        set AppleScript's text item delimiters to sauv
        set affiche to af & " " & jf & " à " & hf & "H" & mn & return & affiche1
       
        display dialog "La programmation actuelle est :" & return & affiche
    end if
   
end tell
repeat
    tell application "Finder"
        set lemenu to {"Programmer un allumage et une extinction", "Supprimer toutes les programmations"}
        set choixmenu to (choose from list lemenu with prompt "Choisir votre action") as string
        if choixmenu is "false" then exit repeat
        if choixmenu is "Supprimer toutes les programmations" then
            display dialog "Effacer toutes les programmations ?"
            display dialog "Mot de passe" with default answer and hidden answer
            set MDP to text returned of result
            set commande to "echo " & MDP & " | sudo -S pmset repeat cancel"
            do shell script commande
            exit repeat
        else
            set maliste to {"Eteindre", "Suspendre", "Redémarrer"}
           
            set lechoix to choose from list maliste with prompt "Choisir l'action à effectuer)"
            set reponse to lechoix as string
            if reponse is "false" then exit repeat
            set monaction to reponse
           
            set lalist to {"Lundi - M", "mardi - T", "Mercredi -W", "Jeudi -R", "vendredi -F", "samedi -S", "dimanche -U"}
            set lechoix to choose from list lalist with prompt "Choisir les jours pour éteindre l'ordinateur (cmd pour choix multiple)" with multiple selections allowed
            set reponse to lechoix as string
            if reponse is "false" then exit repeat
            set lesjourseteint to ""
            set nb to count of lechoix
            repeat with i from 1 to nb
                set a to last character of (item i of lechoix)
                set lesjourseteint to lesjourseteint & a
            end repeat
            display dialog "Saisir l'heure d'extinction : format HH:MM:SS" default answer "hh:mm:ss"
            set heureeteint to text returned of result
           
            my verif(heureeteint)
            if erreur = 1 then
                display dialog "Erreur dans la saisie !"
                exit repeat
            end if
           
            set lechoix to choose from list lalist with prompt "Choisir les jours pour allumer l'ordinateur (cmd pour choix multiple)" with multiple selections allowed
            set reponse to lechoix as string
            if reponse is "false" then exit repeat
            set lesjoursAllum to ""
            set nb to count of lechoix
            repeat with i from 1 to nb
                set a to last character of (item i of lechoix)
                set lesjoursAllum to lesjoursAllum & a
            end repeat
            display dialog "Saisir l'heure d'allumage : format HH:MM:SS" default answer "hh:mm:ss"
            set heureallum to text returned of result
           
            my verif(heureallum)
            if erreur = 1 then
                display dialog "Erreur dans la saisie !"
                exit repeat
            end if
            if monaction is "Eteindre" then set action to "shutdown"
            if monaction is "Suspendre" then set action to "sleep"
            if monaction is "Redémarrer" then set action to "restart"
            display dialog "Allumer l'ordinateur les: " & lesjoursAllum & " à " & heureallum & " heure" & return & monaction & " l'ordinateur les: " & lesjourseteint & " à " & heureeteint & " heure"
            display dialog "Mot de passe" with default answer and hidden answer
            set MDP to text returned of result
            set commande to "echo " & MDP & " | sudo -S pmset repeat poweron " & lesjoursAllum & " " & heureallum & " " & action & " " & lesjourseteint & " " & heureeteint
            do shell script commande
        end if
    end tell
   
    tell application "Finder"
        activate
        (display dialog ("waouou ... Ca y est c'est fait !") buttons {"Salut !"})
    end tell
    exit repeat
end repeat
on verif(saisie)
    set erreur to 0
    set sauv to AppleScript's text item delimiters --sauvegarder la variable delimiteur
    set AppleScript's text item delimiters to {":"}
    set nb to count text item of saisie
    if nb is not 3 then set erreur to 1
    if (count item of saisie) is not 8 then set erreur to 1
    set h to text item 1 of saisie
    set nb to count characters of h
    if nb is not 2 then set erreur to 1
    set m to text item 2 of saisie
    set nb to count characters of m
    if nb is not 2 then set erreur to 1
    set s to text item 3 of saisie
    set nb to count characters of h
    if nb is not 2 then set erreur to 1
    try
        set h to h as number
        set m to m as number
        set s to s as number
    on error
        set erreur to 1
    end try
    if h > 23 then set erreur to 1
    if m > 59 then set erreur to 1
    if s > 59 then set erreur to 1
    set AppleScript's text item delimiters to sauv
    return erreur
end verif