How to Right click of File in Windows Explorer by AutoIt
Asked Answered
C

3

5

I wish to simulate a right click on a file. This is done by opening a Windows Explorer window and then right clicking on it.

The main issue is finding the location of the file in Windows Explorer. I am currently using Autoit v3.3.8.1.

My code 's first line:

 RunWait (EXPLORER.EXE /n,/e,/select,<filepath>)

The next step is the problem. Finding the coordinates of the file. After that, right clicking at that coordinates (it seems to me at this time) is not a problem....

Some background:

  • OS: Windows 7 64-bit
  • Software Languages: C#, Autoit (for scripting)

The Autoit script is called by a code similar to that below:

Process p = new Process();
p.StartInfo.FileName = "AutoItScript.exe";
p.StartInfo.UseShellExecute = false;
p.Start();

The code is compiled into a console class file which is run at startup. The autoit script runs as the explorer window opens up.

Circassia answered 12/3, 2012 at 13:31 Comment(2)
What does this have to do with C# if you are using AutoIt? And why would you want to right click the file? To open the right-click menu you can script it to use the menu key.Syncretism
Do you want to right click on a file/folder?Allocation
C
8

It seems as though you are taking the wrong approach to the problem, so I'll answer what you are asking and what you should be asking.

First up though, that line of code is not valid, and is not what you want either. You want to automate the explorer window, and RunWait waits for the program to finish. Furthermore you want those items to be strings, that code would never work.

Finding the item in explorer

The explorer window is just a listview, and so you can use normal listview messages to find the coordinates of an item. This is done most simply by AutoIt's GUIListView library:

#include<GUIListView.au3>

Local $filepath = "D:\test.txt"

Local $iPid = Run("explorer.exe /n,/e,/select," & $filepath)
ProcessWait($iPid)

Sleep(1000)

Local $hList = ControlGetHandle("[CLASS:CabinetWClass]", "", "[CLASS:SysListView32; INSTANCE:1]")

Local $aClient = WinGetPos($hList)
Local $aPos = _GUICtrlListView_GetItemPosition($hList, _GUICtrlListView_GetSelectedIndices($hList))

MouseClick("Right", $aClient[0] + $aPos[0] + 4, $aClient[1] + $aPos[1] + 4)

As has already been mentioned, sending the menu key is definitely a better way than having to move the mouse.

Executing a subitem directly

This is how it should be done. Ideally you should never need an explorer window open at all, and everything can be automated in the background. This should always be what you aim to achieve, as AutoIt is more than capable in most cases. It all depends on what item you want to click. If it is one of the first few items for opening the file in various programs, then it is as simple as either:

  1. Using ShellExecute, setting the verb parameter to whatever it is you want to do.
  2. Checking the registry to find the exact command line used by the program. For this you will need to look under HKCR\.ext where ext is the file extension, the default value will be the name of another key in HKCR which has the actions and icon associated with the filetype. This is pretty well documented online, so google it.

If the action is not one of the program actions (so is built into explorer) then it is a little more complex. Usually the best way will be to look at task manager when you start the program and see what it runs. Other things can be found online, for example (un)zipping. Actions like copy, delete, rename, create shortcut, send to... They can all be done directly from AutoIt with the various File* functions.

With more information, it would be possible to give you more specific help.

Ctenophore answered 12/3, 2012 at 20:35 Comment(4)
Hi Mat. Thanks for your reply. I understand where you are coming from. Unfortunately, my customer wants to see the mouse move to the file and do things like right click, double click,etc etc.Circassia
@JosephZeng, In which case the first part of my answer should be correct.Ctenophore
It works like @Mat pointed out. You can truely just append the filename to the filepath as he demonstated. If you want to use the keyboard instead you can use ControlSend("", "", $hList, "<filename>+{F10}")and then use the arrow keys (probably with some Sleep(333) in between that the action is more visible to your customer. Alternatively the MouseClick could be varied with speed.Antevert
Oh - and if the Advanced Class [CLASS:SysListView32; INSTANCE:1] doesn't work I used [CLASS:DirectUIHWND; INSTANCE:3] which worked for me... give it a try and rate this comment if it works. ;-)Antevert
C
2

First, you might want to look at the Microsoft Active Accessibility SDK. In particular look at this interface...

http://msdn.microsoft.com/en-us/library/accessibility.iaccessible.aspx

You can use this to walk the items in the control and find the one with the file name you are looking for and its screen location.

From there, maybe try something like this for simulating the right click.

How can I use automation to right-click with a mouse in Windows 7?

Once you have done the right click, use accessibility again to find the right option on the context menu.

Maybe there's an easier way, you should be able to cobble something together like this if you don't find one. Good luck!

Cub answered 12/3, 2012 at 20:34 Comment(0)
A
0

Suppose I have a file named test.txt on D drive. It needs to right click for opening Context Menu. To do this, the following code should work:

Local $filepath = "D:\test.txt"
Local $iPid = Run("explorer.exe /n,/e,/select," & $filepath)
ProcessWait($iPid)
Sleep(1000)
Send('+{F10}')
Allocation answered 28/3, 2014 at 9:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.