8daysaweek.co.uk Posted October 29, 2005 Share Posted October 29, 2005 (edited) I'm trying to create a script that allows a user to run any file by dropping a shortcut (.lnk) to it into a predefined folder. expandcollapse popup;------------------------- ; Shows the filenames of all files in the current directory. $search = FileFindFirstFile("Program1\*.lnk") ; Check if the search was successful If $search = -1 Then MsgBox(0, "Error", "No files/directories matched the search pattern") Exit EndIf While 1 $file1 = StringTrimRight(FileFindNextFile($search), 4) MsgBox(4096, "File:", $file1 ) If @error Then ExitLoop $details1 = FileGetShortcut($file1) MsgBox(4096, "Details:", $details1 ) $path1 = $details1[0] MsgBox(0, "Path:", $path1 ) If ( $var = "all" ) then Sleep(2000) Run ( $path1 ) Else If ( $var = "some" ) then $__msgbox = MsgBox ( 4, "startup program 1", "Do you want to start" &@LF&@LF& $file1 ) If NOT ( $__msgbox = 7 ) then Run ( $path1 ) ExitLoop EndIf ExitLoop EndIf EndIf WEnd ; Close the search handle FileClose($search) ;------------------------- The strange thing is, this works with some shortcuts, but not with others, and I have no idea why. Depending on the shortcut I copy into the folder, either 1) everything works correctly or 2) I get the error: Line 18 (File "<file path here.au3") $path1 = $details1[0] $path1 = $details1^ERROR Error: Subscript used with non-Array variable. I realize there may be many improvements that could be made to this code, but mostly I'd like to know why this works with some shortcuts but not with others? TIA, Edited November 4, 2005 by 8daysaweek.co.uk [size=15]James :)[/size]www.8daysaweek.co.uk - A User-Focused OOo site, with Forums, Help Centre,Easy Installation CDs & OOo on USB Key Link to comment Share on other sites More sharing options...
MHz Posted October 30, 2005 Share Posted October 30, 2005 FileGetShortcut() is failing as it needs a path to the shortcut that was searched. If I add FileChangeDir('Program1') immediately after FileFindFirstFile() then it works ok.It is possibly a good idea to use the working directory retrieved from the shortcut to be used with the Run() function.I would assume that the shortcuts that did work were perhaps in the script directory itself. Link to comment Share on other sites More sharing options...
8daysaweek.co.uk Posted October 30, 2005 Author Share Posted October 30, 2005 (edited) Thanks for the tip MHz. Here is my correct code (that works ):expandcollapse popupConst $workingDir = @WorkingDir ;------------------------- ; Shows the filenames of all files in the current directory. $search = FileFindFirstFile("Program1\*.lnk") FileChangeDir("Program1") ; Check if the search was successful If $search = -1 Then MsgBox(0, "Error", "No files/directories matched the search pattern") Exit EndIf While 1 $file1 = StringTrimRight(FileFindNextFile($search), 4) ;MsgBox(4096, "File:", $file1 ) If @error Then ExitLoop $details1 = FileGetShortcut($file1) ;MsgBox(4096, "Details:", $details1[0] ) $path1 = $details1[0] ;MsgBox(0, "Path:", $path1 ) If ( $var = "all" ) then Sleep(2000) Run ( $path1 ) FileChangeDir($WorkingDir) ExitLoop Else If ( $var = "some" ) then $__msgbox = MsgBox ( 4, "Start Program 1", "Do you want to start" &@LF&@LF& $file1 ) If NOT ( $__msgbox = 7 ) then Run ( $path1 ) FileChangeDir($WorkingDir) ExitLoop EndIf EndIf EndIf WEnd ; Close the search handle FileClose($search)I would assume that the shortcuts that did work were perhaps in the script directory itself.No, that's what is strange different shortcuts in exactly the same folder (i.e. Program1) behaved differently. Not sure why, but the above is working now, so thanks very much BFN, Edited November 4, 2005 by 8daysaweek.co.uk [size=15]James :)[/size]www.8daysaweek.co.uk - A User-Focused OOo site, with Forums, Help Centre,Easy Installation CDs & OOo on USB Key Link to comment Share on other sites More sharing options...
Bert Posted November 4, 2005 Share Posted November 4, 2005 I tried to use this code, and I'm getting a error on line 28: "If ( $var = "all" ) then" It is saying the value isn't declared. Did you make the code with the latest beta? The Vollatran project My blog: http://www.vollysinterestingshit.com/ Link to comment Share on other sites More sharing options...
8daysaweek.co.uk Posted November 4, 2005 Author Share Posted November 4, 2005 Hi vollyman, It's just a code fragment, the variable was declared earlier. I am using a Beta, not sure if it's the latest... 3.1.1.79 on the help files? Coincidentally I just finished the code today, (I spoke too soon before when I said "it works" I managed to find quite a few bugs when testing) here is some of it (it carries on in the same fashion )... expandcollapse popup$__msgbox = MsgBox ( 4, 'Startup Programs', 'Do you want to STOP all programs from starting?', 30 ) if ( $__msgbox = 6 ) then $var = "none" Else $sleepA = IniRead("sleep.ini", "Prompt1", "sleep", "10000") ;MsgBox(4096, "Result", $sleepA) Sleep($sleepA) $__msgbox = MsgBox ( 4, 'Startup Some Programs', 'Do you only want to start SOME programs?', 10 ) if ( $__msgbox = 6 ) then $var = "some" Else $var = "all" $sleepB = IniRead("sleep.ini", "Prompt2", "sleep", "10000") ;MsgBox(4096, "Result", $sleepB) Sleep($sleepB) EndIf EndIf ;MsgBox(4096,"Var", $var) If $var = "none" then Exit Else Const $workingDir = @WorkingDir ;------------------------- ; Shows the filenames of all files in the current directory. $search = FileFindFirstFile("P1Program1\*.lnk") FileChangeDir("P1Program1") ; Check if the search was successful If $search <> -1 Then While 1 $file1 = StringTrimRight(FileFindNextFile($search), 4) ;MsgBox(4096, "File:", $file1 ) If @error Then ExitLoop $details1 = FileGetShortcut($file1) ;MsgBox(4096, "Details:", $details1[0] ) $path1 = $details1[0] ;MsgBox(0, "Path:", $path1 ) If ( $var = "all" ) then $sleep1 = IniRead("sleep.ini", "P1Program1", "sleep", "10000") ;MsgBox(4096, "Result", $sleep1) Sleep($sleep1) Run ( $path1 ) FileChangeDir($WorkingDir) ExitLoop Else If ( $var = "some" ) then $__msgbox = MsgBox ( 4, "Start Program 1", "Do you want to start" &@LF&@LF& $file1 ) If NOT ( $__msgbox = 7 ) then Run ( $path1 ) EndIf FileChangeDir($WorkingDir) ExitLoop EndIf EndIf WEnd Else ;MsgBox(0, "Error", "No files/directories matched the search pattern (1)") FileChangeDir($WorkingDir) ; Close the search handle FileClose($search) EndIf ;------------------------- ; Shows the filenames of all files in the current directory. $search = FileFindFirstFile("P1Program2\*.lnk") FileChangeDir("P1Program2") ; Check if the search was successful If $search <> -1 Then While 1 $file2 = StringTrimRight(FileFindNextFile($search), 4) If @error Then ExitLoop $details2 = FileGetShortcut($file2) $path2 = $details2[0] If ( $var = "all" ) then $sleep2 = IniRead("sleep.ini", "P1Program2", "sleep", "10000") Sleep($sleep2) Run ( $path2 ) FileChangeDir($WorkingDir) ExitLoop Else If ( $var = "some" ) then $__msgbox = MsgBox ( 4, "Start Program 2", "Do you want to start" &@LF&@LF& $file2 ) If NOT ( $__msgbox = 7 ) then Run ( $path2 ) EndIf FileChangeDir($WorkingDir) ExitLoop EndIf EndIf WEnd Else ;MsgBox(0, "Error", "No files/directories matched the search pattern (2)") FileChangeDir($WorkingDir) ; Close the search handle FileClose($search) EndIf ;------------------------- I imagine that there might be a much "cleaner" way of doing it, but this is working for me ...so far BFN, [size=15]James :)[/size]www.8daysaweek.co.uk - A User-Focused OOo site, with Forums, Help Centre,Easy Installation CDs & OOo on USB Key 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