Introduction
Reading and processing a series of filenames sent to the MSW clipboard from Windows Explorer. A useful little technique for applying the same processing to each of collection of files selected using Explorer.
I use this script often to play a selection of MP3s. (This will explain why I've used random.choice.)
What Objects are Involved
wxPython.wx.wxTheClipboard: for gaining access to the MSW clipboard
wxPython.wx.wxFileDataObject: as a container for a list of filenames from the clipboard
win32api.ShellExecute: for processing each of the filenames, with the app registered for a file of its type
random.choice: for making a random selection of a file from the list from the clipboard
Process Overview
- Launch Windows Explorer.
- Select a collection of files to be processed by the script using Ctrl-Click.
- Send the collection to the clipboard by right-clicking on one of the filenames and selecting 'Copy'. (Win2K: otherwise, YMMV.)
- Launch the script that appears below.
Code
1 from wxPython.wx import *
2 from win32api import ShellExecute
3 from random import choice
4
5 if wxTheClipboard.Open():
6 data = wxFileDataObject()
7 wxTheClipboard.GetData(data)
8 wxTheClipboard.Close()
9
10 playList = data.GetFilenames()
11 if playList:
12 count = 0
13 while 1 :
14 selection = choice(playList)
15 ShellExecute(0, None, selection, None, 'c:\\', 1)
16 count += 1
17 if count > len(playList): break
18 else:
19 print ".... List seems to be empty"
20 else:
21 print "... Can't open clipboard"
Comments
For the sake of simplicity, this script just plays the first k random selections from the list (k being the length of the list), which implies that not all of the selections will be played and that some selections will probably be played more than once.
Changes
kyle: changed part of the code bill: removed reference to my email address