Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Right click of File in Windows Explorer by AutoIt

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.

like image 416
Joseph Zeng Avatar asked Mar 12 '12 13:03

Joseph Zeng


2 Answers

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.

like image 108
Matt Avatar answered Oct 14 '22 08:10

Matt


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!

like image 41
Steve Sheldon Avatar answered Oct 14 '22 09:10

Steve Sheldon