Jump to content
Sign in to follow this  
pete1234

Pass variable from shell extension into AutoIt?

Recommended Posts

pete1234

I've got a script that uses FileSelectFolder to get a path name, and it then turns the list of files in that folder into an array and does a few different things to each file. It looks like this:

$List = FileSelectFolder("Select Folder", @HomeDrive)
$Array = _FileListToArray($List, "*.*", 1)
_ArraySort($Array)

For $i = 1 to $Array[0] Step 1
    MsgBox(0, $1, $Array[$i])
Next

I've replaced my actions with a MsgBox as they're irrelevant to my question. What I'd like to do is instead of using a dialog box, create a shell extension on all folders. Then a user would just have to right-click on the folder and click my shell extension to process the directory. But, I'm not sure how to grab that folder and pass it into my script as $List, thus replacing FileSelectFolder. Any ideas?

Thanks a bunch.

Edited by pete1234

Share this post


Link to post
Share on other sites
Richard Robertson

User scripts can't be shell extensions. Shell extensions are pieces of code that become somehow linked to the shell. What you can do instead, is to edit the registry under the directory shell commands and add your script as an entry with a command line parameter. That way, you can just get the command line which contains the directory name.

Share this post


Link to post
Share on other sites
PsaltyDS

User scripts can't be shell extensions. Shell extensions are pieces of code that become somehow linked to the shell.

That can be done, and quite easily:

1. Add an application key in the registry under shell: HKCR\Directory\Shell\MyDirApp

2. Add a Command key under that: HKCR\Directory\Shell\MyDirApp\Command

3. Change the default REG_SZ value's data to the command line, with variable substitution for the directory path to be passed: (default) = "C:\Temp\MyDirApp.exe" "%1"

Now, when you right click on a directory, you will see an option for "MyDirApp", and selecting it will run the application and pass the directory's path in quotes on the command line in place of "%1".

:P


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

Share this post


Link to post
Share on other sites
pete1234

Yeah, I should have clarified, the reg entry approach is what I was referring to.

I think I'm still a little confused about your idea though. If each time a user ran the program through the context menu, the entry in the registry would point to C:\Program.exe /Folder, correct? How would I then use a variable in the registry so the switch always reflects the folder the user the clicked on, as opposed to something static?

Or maybe I'm taking the wrong approach here. Would the code say something like, Case $parameter = "/Folder" Then (do something to get the full path...I haven't the faintest idea how to do that)?

Thanks.

Edit: The above is a reply to Richard.

PSaltyDS - Thanks for the suggestion. I will play around with it later tonight.

Edited by pete1234

Share this post


Link to post
Share on other sites
Richard Robertson

@PSaltyDS: That's not a shell extension. That's just an association.

@pete1234: The registry approach is what you want. Follow what PSaltyDS wrote. That is what I was suggesting.

Share this post


Link to post
Share on other sites
pete1234

Thanks for the help guys. I've been able to run my program through the shell (the reg entry had to be in HKCR\Folder, not \Directory) but I can't figure out how to pass the %1 into my script.

I was thinking I'd have to do something like this:

For $n = 1 To $CmdLine[0] - 1
    If $CmdLine[$n] = "%1" Then
        msgbox(0, "", $CmdLine[$n + 1])
        ExitLoop
    EndIf
Next

Where my program would take the place of MsgBox...except this doesn't work at all. I'm sure it's something simple, but every variation of this I've tried doesn't work. Can someone point me in the right direction?

Thanks again.

And this is an export of the reg if something is wrong here, but everything with this part seems to be good

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\Folder\shell\BatchPrint]

[HKEY_CLASSES_ROOT\Folder\shell\BatchPrint\command]
@="C:\\PrintTest.exe \"%1\""
Edited by pete1234

Share this post


Link to post
Share on other sites
ProgAndy

The %1 is replaced with the filename , so you cant't get it in you rScript :P

//Edit: And this line is wrong:

For $n = 1 To $CmdLine[0] - 1

it must be

For $n = 1 To $CmdLine[0]

Edited by ProgAndy

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Share this post


Link to post
Share on other sites
PsaltyDS

The %1 is replaced with the filename , so you cant't get it in you rScript :P

To clarify: You won't get the literal "%1" string. You will get the full path of the directory it was called on, i.e. "C:\Temp\TestMe". So if you fix your loop as ProgAndy pointed out, it should work.

:P


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

Share this post


Link to post
Share on other sites
pete1234

:P Still doesn't work. I'm trying to get the MsgBox to display the path, but something is wrong with the If statement I believe. I have this:

For $n = 1 To $CmdLine[0]
    If $CmdLine[$n] = "%1" Then
        msgbox(0, $CmdLine[$n], $CmdLine[$n + 1])
    Else
        msgbox(0, "Test", "Doesn't work")
        ExitLoop
    EndIf
Next

The test MsgBox always pops up.

I've also tried replacing "%1" with chr(34) & "%1" & chr(34) and it also doesn't work. Any ideas?

Thanks for all the help guys.

Edited by pete1234

Share this post


Link to post
Share on other sites
Valik

What part about what PsaltyDS and ProgAndy said about %1 are you not getting? If you're going to ask for help at least be courteous enough to comprehend what people write. You're completely ignoring them and asking a question that's been answered twice now.

Share this post


Link to post
Share on other sites
pete1234

Um, I'm not ignoring them at all. I'm new to this sort of programming and am trying to follow their advice. And following all the posts in this thread, I thought I was going in the right direction with my loop except for the one error that ProgAndy pointed out. So now I'm trying to understand what I'm missing, and am thus asking for help.

Share this post


Link to post
Share on other sites
Valik

But you have been told what you are missing and you are completely ignoring them. Further, you're completely ignoring common sense, too. You're testing $CmdLine[$i] = "%1". Ignoring the fact, that as you've been told, that is never going to appear, your code doesn't even bother to report what you are getting. If you'd think for half a second and wonder, "Hey, $CmdLine[$i] isn't %1, I wonder what it is" and bothered to look, you'd slap your face with your palm and feel suitably moronic.

Think. Stop. Read. Think some more.

You've been given the answer, multiple times, in clear ways.

Share this post


Link to post
Share on other sites
pete1234

I understand your point, but insults are not necessary. I clearly didn't clarify this as well as I should have, but I was under the impression that command line parameters that aren't static (like /help or something) needed to be tested with an If statement, hence the %1. When they pointed out it wouldn't be in my code, I took that in a more conceptual context...something along the lines of, you don't need to specify the command line, as running from the shell would run program.exe %1, and then my script would say if the command line is %1, and that's how the variable would be passed. You have to remember I'm a beginner.

Anyways, for anyone else struggling with this concept, this works:

For $n = 1 To $CmdLine[0]
    msgbox(0, "", $cmdline[$n])
Next

Thanks again to everyone who has helped me.

Share this post


Link to post
Share on other sites
Squirrely1

I understand your point, but insults are not necessary...

You think you were wrong before - well this is where you are still wrong. See the Canon of Valik below.

Now, Valik is one of the gods of AutoIt, but since you are a newbie, I guess he is going easy on you. You can argue with these guys, but only very politely and not ad nauseum; and try to avoid complaining. And if things go way bad, you end up getting verse number 19 thrown at you like I did.


Das Häschen benutzt Radar

Share this post


Link to post
Share on other sites
ZoScr

1. Add an application key in the registry under shell: HKCR\Directory\Shell\MyDirApp

2. Add a Command key under that: HKCR\Directory\Shell\MyDirApp\Command

3. Change the default REG_SZ value's data to the command line, with variable substitution for the directory path to be passed: (default) = "C:\Temp\MyDirApp.exe" "%1"

Now, when you right click on a directory, you will see an option for "MyDirApp", and selecting it will run the application and pass the directory's path in quotes on the command line in place of "%1".

That's an awesome tip.

I'd like to do the same thing, but for a particular file type (rather than all folders). I.e. I want my option to only appear when the user right clicks on files of one particular file type. (I want to write something that does useful things with the URL info in .mht files.)

Thanks if you can help!

Share this post


Link to post
Share on other sites
ResNullius

That's an awesome tip.

I'd like to do the same thing, but for a particular file type (rather than all folders). I.e. I want my option to only appear when the user right clicks on files of one particular file type. (I want to write something that does useful things with the URL info in .mht files.)

Thanks if you can help!

Here's an example for pdf files:

RegWrite("HKEY_CLASSES_ROOT\.pdf\shell\Pdf2Jpg\command", '', 'REG_SZ', '"' & @ScriptFullPath & '" "%1"')
RegWrite("HKEY_CLASSES_ROOT\.pdf\shell\Pdf2Jpg", '', 'REG_SZ', "--> Convert to JPG")

The 2nd entry is the literal text that will appear in the right click menu; in this case "--> Convert to JPG"

Share this post


Link to post
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
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.