Jump to content

Got my head lost in an array.


jebus495
 Share

Recommended Posts

Credits to the author of the ChangeWallpaper function

This version of my script is ready to run for someone trying to help me.

C:\Documents and Settings\Jebus\Desktop\wallpaper.au3 (18) : ==> Array variable has incorrect number of subscripts or subscript dimension range exceeded.:

ProgressSet(($i/UBound($filelist))*100, $filelist[$i])

ProgressSet(($i/UBound($filelist))*100, ^ ERROR

So I know where the problem is happening but I'm not exactly clear on what's happening and how to fix it. I must not have a clear understanding of arrays yet. Probably something to do with arrays starting at 0. But I don't actually want to display $filelist[0] in my progress bar because that just the number of files.

#Include <File.au3>
#include <Array.au3>
#include <Timers.au3>

$filelist = _FileListToArray(@WindowsDir, "*", 1)
If @Error = 1 Then
    MsgBox (0,"","No Files\Folders Found.")
    Exit
EndIf

_ArrayDisplay($filelist)
ProgressOn("Wallpaper Rotater", "Loading images")
For $i = 1 To Ubound($filelist)
    ProgressSet(($i/UBound($filelist))*100, $filelist[$i])
    Sleep(100)
    If StringInStr($filelist[$i], ".bmp") = 0 Then _ArrayDelete($filelist, $i)
Next
_ArrayDisplay($filelist)
ProgressOff()


ChangeWallpaper(@WindowsDir & $filelist[Random(1, UBound($filelist)-1, 1)], 3)
While 1
If _Timer_GetIdleTime() > 5*60*1000 Then
    ChangeWallpaper(@WindowsDir & $filelist[Random(1, UBound($filelist)-1, 1)], 3)
    Sleep(15*60*1000)
EndIf
Sleep(1000)
WEnd

Func ChangeWallpaper($FileLong, $State = 3)
    Local $String = $FileLong, $SPI_SETDESKWALLPAPER = 20, $SPIF_UPDATEINIFILE = 1, $SPIF_SENDCHANGE = 2
    Do
        $FileLong = $String
        $String = StringReplace($FileLong, "/", "\")
    Until @extended = 0
    If Not FileExists($FileLong) Then
        SetError(-1)
        Return 0
    EndIf
    If StringRight($FileLong, 3) <> "bmp" Then
        SetError(-2)
        Return 0
    EndIf

    Local $WDir = RegRead('HKLM\Software\Microsoft\Windows\CurrentVersion', 'WallPaperDir')
    $FileShort = StringSplit($FileLong, "\")
    $FileShort = $FileShort[$FileShort[0]]
    If StringInStr($WDir, @WindowsDir) <> 0 Then
        $WDir = StringTrimLeft($WDir, 12)
        $WDir = @WindowsDir & $WDir
    EndIf
    FileCopy($FileLong, $WDir, 1); make wallpaper available in desktop properties window
    FileCopy($FileLong, @UserProfileDir &  '\Local Settings\Application Data\Microsoft\Wallpaper1.bmp', 1)

    RegWrite('HKCU\Control Panel\Desktop', 'Wallpaper', 'reg_sz', @UserProfileDir &  '\Local Settings\Application Data\Microsoft\Wallpaper1.bmp')
    RegWrite('HKCU\Control Panel\Desktop', 'ConvertedWallpaper', 'reg_sz', $WDir & "\" & $FileShort)
    Switch $State
        Case 1; centered
            RegWrite('HKCU\Control Panel\Desktop', 'TileWallpaper', 'reg_sz', '0')
            RegWrite('HKCU\Control Panel\Desktop', 'WallpaperStyle', 'reg_sz', '0')
        Case 2; tiled
            RegWrite('HKCU\Control Panel\Desktop', 'TileWallpaper', 'reg_sz', '1')
            RegWrite('HKCU\Control Panel\Desktop', 'WallpaperStyle', 'reg_sz', '0')
        Case 3; stretched
            RegWrite('HKCU\Control Panel\Desktop', 'TileWallpaper', 'reg_sz', '0')
            RegWrite('HKCU\Control Panel\Desktop', 'WallpaperStyle', 'reg_sz', '2')
        Case Else
    EndSwitch

    $Dll = DllCall("user32.dll", "int", "SystemParametersInfo", _
            "int", $SPI_SETDESKWALLPAPER, _
            "int", 0, _
            "str", $FileLong, _
            "int", BitOR($SPIF_UPDATEINIFILE, $SPIF_SENDCHANGE))
EndFunc
Link to comment
Share on other sites

Arrays are 0-based, but Ubound() returns a 1-based count. The last valid index is Ubound($YourArray) - 1, so you want:

For $i = 1 To Ubound($filelist) - 1

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

You might also want to double check this bearing in mind directory macros do not contain trailing backslashes.

ChangeWallpaper(@WindowsDir & $filelist[Random(1, UBound($filelist)-1, 1)], 3)

You may need to chamge it to

ChangeWallpaper(@WindowsDir & "\" & $filelist[Random(1, UBound($filelist)-1, 1)], 3)

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

You might also want to double check this bearing in mind directory macros do not contain trailing backslashes.

ChangeWallpaper(@WindowsDir & $filelist[Random(1, UBound($filelist)-1, 1)], 3)

You may need to chamge it to

ChangeWallpaper(@WindowsDir & "\" & $filelist[Random(1, UBound($filelist)-1, 1)], 3)

Ah yes that's right. Unfortunately the script still doesn't make it that far.

Arrays are 0-based, but Ubound() returns a 1-based count. The last valid index is Ubound($YourArray) - 1, so you want:

For $i = 1 To Ubound($filelist) - 1

:)

I tried that and still have an error with
ProgressSet(($i/UBound($filelist))*100, $filelist[$i])

There is a similar issue there?

Link to comment
Share on other sites

Try ProgressSet(($i/UBound($filelist)-1)*100, $filelist[$i])

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

  • Developers

There is a logic issue in the script.

This line is only evaluated one time at the start of the For-next loop:

For $i = 1 To Ubound($filelist) - 1

So Whenever the script performs this line :

If StringInStr($filelist[$i], ".bmp") = 0 Then _ArrayDelete($filelist, $i)

... and the IF is true it means the Array will be smaller, but for-next loop will still loop on the original size.

possible solution (untested):

_ArrayDisplay($filelist)
ProgressOn("Wallpaper Rotater", "Loading images")
$i = 1
While $i < Ubound($filelist)-1
    ProgressSet(($i/UBound($filelist)-1)*100, $filelist[$i])
    Sleep(100)
    If StringInStr($filelist[$i], ".bmp") = 0 Then _ArrayDelete($filelist, $i)
    $i += 1
Wend
_ArrayDisplay($filelist)

Jos

Edited by Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Sharp eyes. I totally missed that.

My solution usually involves copying the original array to a new one and then loop through the new array and use _ArrayDelete() on the original.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

There is a logic issue in the script.

This line is only evaluated one time at the start of the For-next loop:

For $i = 1 To Ubound($filelist) - 1

So Whenever the script performs this line :

If StringInStr($filelist[$i], ".bmp") = 0 Then _ArrayDelete($filelist, $i)

... and the IF is true it means the Array will be smaller, but for-next loop will still loop on the original size.

possible solution (untested):

_ArrayDisplay($filelist)
ProgressOn("Wallpaper Rotater", "Loading images")
$i = 1
While $i < Ubound($filelist)-1
    ProgressSet(($i/UBound($filelist)-1)*100, $filelist[$i])
    Sleep(100)
    If StringInStr($filelist[$i], ".bmp") = 0 Then _ArrayDelete($filelist, $i)
    $i += 1
Wend
_ArrayDisplay($filelist)

Jos

Wow thanks! Had to add some brackets to make the progressbar to work.

_ArrayDisplay($filelist)
ProgressOn("Wallpaper Rotater", "Loading images")
$i = 1
While $i < Ubound($filelist)-1
    If StringInStr($filelist[$i], ".bmp") = 0 Then _ArrayDelete($filelist, $i)
    ProgressSet(($i/(UBound($filelist)-1))*100, $filelist[$i])
    Sleep(100)
    $i += 1
Wend
_ArrayDisplay($filelist)
ProgressOff()

It runs through without errors but no longer removes elements that don't contain ".bmp".

Did I miss something?

Edited by jebus495
Link to comment
Share on other sites

There is a logic issue in the script.

This line is only evaluated one time at the start of the For-next loop:

For $i = 1 To Ubound($filelist) - 1

So Whenever the script performs this line :

If StringInStr($filelist[$i], ".bmp") = 0 Then _ArrayDelete($filelist, $i)

... and the IF is true it means the Array will be smaller, but for-next loop will still loop on the original size.

possible solution (untested):

Jos

More often I would just walk the array backwards if I was going to be deleting things. All you have to do is make a slight change to the progress math:
_ArrayDisplay($filelist)
ProgressOn("Wallpaper Rotater", "Loading images")
For $i = $filelist[0] To 1 Step -1
    ProgressSet(($filelist[0] - $i) / $filelist[0] * 100, $filelist[$i])
    Sleep(100)
    If StringInStr($filelist[$i], ".bmp") = 0 Then _ArrayDelete($filelist, $i)
Next
$filelist[0] = UBound($filelist) - 1
_ArrayDisplay($filelist)

All the Ubound'ing isn't required since the array came from _FileListToArray(), so count is in $filelist[0] anyway. It just needs updating after the deletes are done.

:)

Edited by PsaltyDS
Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

  • Developers

Thinking about it there is another flaw in the logic in case you delete a entry from the array.

Try:

_ArrayDisplay($filelist)
ProgressOn("Wallpaper Rotater", "Loading images")
$i = 1
While $i < Ubound($filelist)-1
    If StringInStr($filelist[$i], ".bmp") = 0 Then 
       _ArrayDelete($filelist, $i)
    else
       $i += 1
    endif
    ProgressSet(($i/(UBound($filelist)-1))*100, $filelist[$i])
    Sleep(100)
Wend
_ArrayDisplay($filelist)
ProgressOff()

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

More often I would just walk the array backwards if I was going to be deleting things. All you have to do is make a slight change to the progress math:

_ArrayDisplay($filelist)
ProgressOn("Wallpaper Rotater", "Loading images")
For $i = $filelist[0] To 1 Step -1
    ProgressSet(($filelist[0] - $i) / $filelist[0] * 100, $filelist[$i])
    Sleep(100)
    If StringInStr($filelist[$i], ".bmp") = 0 Then _ArrayDelete($filelist, $i)
Next
$filelist[0] = UBound($filelist) - 1
_ArrayDisplay($filelist)

All the Ubound'ing isn't required since the array came from _FileListToArray(), so count is in $filelist[0] anyway. It just needs updating after the deletes are done.

:)

Perfect!

Now... the silliest of things...

I could have avoided this issue all together if I would have changed:

$filelist = _FileListToArray(@WindowsDir, "*", 1)

To:

$filelist = _FileListToArray(@WindowsDir, "*.bmp", 1)

Then I could skip the loop. LOL

Link to comment
Share on other sites

Now... the silliest of things...

I could have avoided this issue all together if I would have changed:

$filelist = _FileListToArray(@WindowsDir, "*", 1)

To:

$filelist = _FileListToArray(@WindowsDir, "*.bmp", 1)

Then I could skip the loop. LOL

To quote the sage philosopher Homer: Doh! :)
Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...