[AppleScript] Performance issues, beter schrijven?

Pagina: 1
Acties:
  • 113 views sinds 30-01-2008
  • Reageer

  • Guru Evi
  • Registratie: Januari 2003
  • Laatst online: 23-12-2025
Ik heb volgend script, maar ik vind dat het een beetje traag loopt. Ik denk persoonlijk dat het niet direct de commando's zijn maar eerder de functie's in AppleScript die niet optimaal zijn. Het script duurt ongeveer 5s om uit te voeren. Als ik mijn shell scripts manueel doe, dan duurt het misschien 1 of 2s.

Heeft iemand tips voor mij om het beter te doen of iets anders wat ik kan verbeteren aan dit script?

code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
property pKeychainItemName : "Exchange" --get the information out of this program in case Kerberos doesn't work (Entourage)
property dcserver : "put_ad_server_here" --Which domain controller to use
property warnTime : "14" --What time to warn before the actual expiration

--Created by Guru Evi under the GPL License. Please adhere to this license when using this script.
--More information about the GPL and the License itself can be found here: http://www.gnu.org/copyleft/gpl.html
--Contact me at evi@valerieandevi.be or find more information at www.valerieandevi.be or www.guruevi.com

on startKeychainScripting()
    
    -- This function ensures that the Keychain Scripting.app is launched by a process
    -- different from the one executing this script. It is only needed when using Keychain Scripting to
    -- retrieve the password property of a key.
    
    -- Quit Keychain Scripting if it's running, since opening this script may have implicitly launched it.  
    try
        tell application "System Events"
            set foundApp to first process whose name is "Keychain Scripting"
            set pid to id of foundApp -- this will exit the try block if the process isn't running
            quit foundApp -- we get here if the process is found
        end tell
    end try
    
    -- get Finder to launch Keychain Scripting
    tell application "Finder"
        open application file ((startup disk as string) & "System:Library:ScriptingAdditions:Keychain Scripting") as alias
    end tell
    
    -- make sure that Keychain Scripting is up and running
    repeat while true
        try
            tell application "System Events"
                set foundApp to first process whose name is "Keychain Scripting"
                set pid to id of foundApp -- this will exit the try block if the process isn't running
            end tell
            exit repeat -- we get here if the process is found
        end try
    end repeat
    
end startKeychainScripting

on getPassword(keyname)
    --This function gets the password out of the Exchange keychain. Smart eh ;-)
    startKeychainScripting()
    
    tell application "Keychain Scripting"
        try
            set theKey to first key of current keychain whose name is keyname
            set thePassword to password of theKey
        on error message number errNum
            set thePassword to message & " (" & errNum & ")"
        end try
    end tell
    
    return thePassword
    
end getPassword

on run
    set username to do shell script "whoami"
    
    try
        --first see if it won't work through Kerberos auth.
        set usingKerberos to true
        set test to do shell script ¬
            "rpcclient " & dcserver & " -k"
        set enumofuser to do shell script ¬
            "rpcclient " & dcserver & " -c enumdomusers -k | grep -i " & username & ¬
            " | awk 'BEGIN {FS = \"\\\[\" } {print$3}' | awk 'BEGIN {FS = \"\\]\" } {print$1}'" as text
    on error
        set usingKerberos to false
        --it doesn't work, let's get it through the Keychain of Exchange
        set netpass to my getPassword(pKeychainItemName)
        
        set enumofuser to do shell script ¬
            "rpcclient " & dcserver & " -c enumdomusers -U " & username & "%" & netpass & " | grep -i " & username & ¬
            " | awk 'BEGIN {FS = \"\\\[\" } {print$3}' | awk 'BEGIN {FS = \"\\]\" } {print$1}'" as text
    end try
    
    if usingKerberos is true then
        set rawdate to do shell script ¬
            "rpcclient " & dcserver & " -k -c 'queryuser " & enumofuser & "' | grep \"Password must change\" | awk 'BEGIN {FS = \"Time:\" } {print$2}' | awk '{print$1 \" \" $2 \" \" $3 \" \" $4 \" \" $5 \" \" $6}'" as text
    else
        set rawdate to do shell script ¬
            "rpcclient " & dcserver & " -U " & username & "%" & netpass & " -c 'queryuser " & enumofuser & "' | grep \"Password must change\" | awk 'BEGIN {FS = \"Time:\" } {print$2}' | awk '{print$1 \" \" $2 \" \" $3 \" \" $4 \" \" $5 \" \" $6}'" as text
    end if
    try
        set parseddate to (date rawdate)
    on error
        display dialog "Your local account is not in the Active Directory" buttons {"OK"} default button "OK"
        return false
    end try
    
    set daysleft to (parseddate - (current date)) div days
    
    if daysleft ? (warnTime / 2) then
        set theButton to button returned of ¬
            (display dialog "You have " & daysleft & " days left to change your password" buttons {"Change Password", "OK"} default button "Change Password" with icon stop)
    else if daysleft ? warnTime then
        set theButton to button returned of ¬
            (display dialog "You have " & daysleft & " days left to change your password" buttons {"Change Password", "OK"} default button "OK")
    else
        set theButton to false
    end if
    if theButton is "Change Password" then
        tell application "Terminal"
            activate
            set theError to do script "clear && kpasswd " & username & " && clear && echo \"Thank you, you can close this window now\" && exit"
            set custom title of front window to "Change Active Directory Password"
        end tell
    end if
end run

Pandora FMS - Open Source Monitoring - pandorafms.org


  • chem
  • Registratie: Oktober 2000
  • Laatst online: 12-02 10:41

chem

Reist de wereld rond

Ik gok dat de meeste tijd in het opstarten van het script zit; applescript is niet bepaald een performance monster; maar het bespaart je wel een hoop tijd.

Probeer je script eens in te krimpen, en kijk eens of bij bv 1/10e van je code het zooitje nog traag is: dan ligt het domweg aan AS en zul je óf je script als bv applicatie moeten bewaren, óf afstappen van AS.

Klaar voor een nieuwe uitdaging.