barkeeper Posted January 19, 2015 Share Posted January 19, 2015 (edited) Hey Guys, Could really use some help with a script. I'm trying to create a program that sits in my taskbar, whenever I press a hotkey (Ctrl+1 in this case) it generates a random 10 character password and copies it to my clipboard. All goes well, except when I use the script twice or three times. It keeps adding to the clipboard instead of clearing the clipboard first. Does anyone have an idea what to change to the script to clear the clipboard before creating a new random password ? Here is my code : #include <StringConstants.au3> const $pwsMask = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" Local $len Local $n Local $buff $len = StringStripWS(10,$STR_STRIPALL) HotKeySet("^1", "create") While 1 Sleep(1) WEnd Func create() ;This creates the random password. for $i = 1 to $Len ;Pick a random chat between 1 and the pwsMask Length $n = int(random(1,StringLen($pwsMask))) ;Concat each chat that has been picked out of pwsMask to $buff $buff = $Buff & StringMid($pwsmask,$n,1) Next ;Put new password on the clipboard. ClipPut($buff) EndFunc ;==>search Edited January 19, 2015 by barkeeper Link to comment Share on other sites More sharing options...
BrewManNH Posted January 19, 2015 Share Posted January 19, 2015 $buff should be declared local inside the function create() and not outside, you never clear the contents of $buff when you call the function more than once. If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag GudeHow to ask questions the smart way! I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from. Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays. - ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script. - Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label. - _FileGetProperty - Retrieve the properties of a file - SciTE Toolbar - A toolbar demo for use with the SciTE editor - GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI. - Latin Square password generator Link to comment Share on other sites More sharing options...
barkeeper Posted January 19, 2015 Author Share Posted January 19, 2015 Can you example my code to show what you mean ? Link to comment Share on other sites More sharing options...
Solution Danp2 Posted January 19, 2015 Solution Share Posted January 19, 2015 #include <StringConstants.au3> HotKeySet("^1", "create") While 1 Sleep(1) WEnd Func create() const $pwsMask = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" Local $len = 10 Local $n Local $buff = '' ;This creates the random password. for $i = 1 to $Len ;Pick a random chat between 1 and the pwsMask Length $n = int(random(1,StringLen($pwsMask))) ;Concat each chat that has been picked out of pwsMask to $buff $buff = $Buff & StringMid($pwsmask,$n,1) Next ;Put new password on the clipboard. ClipPut($buff) EndFunc ;==>search Latest Webdriver UDF Release Webdriver Wiki FAQs Link to comment Share on other sites More sharing options...
barkeeper Posted January 19, 2015 Author Share Posted January 19, 2015 You Rock, works like a charm! Link to comment Share on other sites More sharing options...
SadBunny Posted January 20, 2015 Share Posted January 20, 2015 As my boss is paying me without giving me something to do, I thought I'd have some fun and make this faster. Not that it has any practical advantage as the function is called by a manual keypress, but there's a couple of inefficiencies in your script. So I wrote a test that created 20,000 pwds, and runs that test 10 times. Then it calculates some averages. Also, I found out that you can BSOD your (almost vanilla) x64 Windows 7 Pro if you manipulate the clipboard roughly 80 thousand times a second. This is on a core-i7 @3.6 GHz with 16 GB of RAM on a 256 GB OCZ Vertex 460 SSD. So I left the ClipPut out. Which makes this benchmark more pure as well, which I hadn't thought of at first, so the BSOD was actually useful this time. Luckily Eclipse keeps backups when idling And, I got to pretend I was very busy fixing problems. 1. The create() function sets the maskstring and the length every time. You only need to set those one time. 2. Not only for every run of create() but for every iteration of the char-adding loop within that, you keep calculating the length of the maskstring. You only need to do that once. 3. Picking characters from a string with StringMid() is really slow. Better to put the characters in an array and pick array indices. 4. You don't need to Int(Random())... The Random() function has a flag that Int's the result for you. I benchmarked that separately and using that flag it apparently about 10% faster than "Inting it from outsite". This is what I came up with as a crude benchmark: expandcollapse popup$amountOfTests = 10 $amountOfPwdsPerTest = 20000 $totalExpired = 0 Const $pwsMask = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" Const $pwsLen = 10 ; create mask array from mask string $maskLen = StringLen($pwsMask) dim $ar_mask[$maskLen] $max_ar_index = $maskLen - 1 for $i = 1 to $maskLen $ar_mask[$i - 1] = StringMid($pwsMask, $i, 1) Next For $k = 1 To $amountOfTests $start = TimerInit() For $pwds = 1 To $amountOfPwdsPerTest create() Next $expired = TimerDiff($start) ConsoleWrite("Expired ms in test " & $k & ": " & $expired & @CRLF) $totalExpired += $expired Next $averageMsPerTest = $totalExpired / $amountOfTests $totalGeneratedPwds = $amountOfTests * $amountOfPwdsPerTest ConsoleWrite("Average ms per test: " & round($averageMsPerTest) & @CRLF) ConsoleWrite("Average ms per generated pwd: " & $totalExpired / $totalGeneratedPwds & @CRLF) ConsoleWrite("Rough estimate of pwgen per second: " & round(1000 / ($totalExpired / $totalGeneratedPwds)) & @CRLF) Func create() Local $buff = '' ;This creates the random password. For $i = 1 To $pwsLen ;Pick a random index $n = Random(0, $max_ar_index, 1) ;Concat each chat that has been picked out of ar_mask to $buff $buff = $buff & $ar_mask[$n] Next ;Put new password on the clipboard. ;ClipPut($buff) EndFunc ;==>create Roses are FF0000, violets are 0000FF... All my base are belong to you. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now