码迷,mamicode.com
首页 > 编程语言 > 详细

Python写自动化之图标锁定到任务栏或删除图标

时间:2015-03-05 17:12:32      阅读:434      评论:0      收藏:0      [点我收藏+]

标签:win32com   任务栏脱离   unpin   锁定   

这个功能在windows上测试安装卸载时,有时会用到,网上查到的两种语言的版本如下:

C#版:

Shell shell = new Shell();
Folder folder = shell.NameSpace(Path.GetDirectoryName(appPath));
FolderItem app = folder.ParseName(Path.GetFileName(appPath));
string sVerb = isLock ? "锁定到任务栏(&K)" : "从任务栏脱离(&K)";
 
foreach (FolderItemVerb Fib in app.Verbs())
{
    if (Fib.Name == sVerb)
    {
       Fib.DoIt();
       return true;
    }
}
 
return false;

VB版:

Public Shared Function LockApp(isLock As Boolean, appPath As String) As Boolean
	Dim shell As New Shell()
	Dim folder As Folder = shell.[NameSpace](Path.GetDirectoryName(appPath))
	Dim app As FolderItem = folder.ParseName(Path.GetFileName(appPath))
	Dim sVerb As String = If(isLock, "锁定(&K)", "脱离(&K)")

	For Each Fib As FolderItemVerb In app.Verbs()
		If Fib.Name = sVerb Then
			Fib.DoIt()
			Return True
		End If
	Next

	Return False

End Function


接下来,就是要把上面的代码如何转化为Python了,此处使用到了Windows接口,Python中调用windows接口,可以使用win32com

代码如下:

def DeleteQuickLaunchOnTaskBar(lnkName):
    objShell = win32com.client.Dispatch("Shell.Application")
    
    taskbarPath = os.path.join(os.environ["appdata"], r'Microsoft\Internet Explorer\Quick Launch\User Pinned\TaskBar')
    lnkName = DesktopCommon.ToUnicode(lnkName)
    objFolder = objShell.NameSpace(taskbarPath)
    desktopItems = objFolder.Items()
    
    for item in desktopItems:
        if DesktopCommon.ToUnicode(item.Name) == lnkName:
            verbs = item.Verbs()
            for verb in verbs:
                if DesktopCommon.ToUnicode(verb.Name) == u"从任务栏脱离(&K)" or  DesktopCommon.ToUnicode(verb.Name) == u"Unpin from Tas&kbar":
                    verb.DoIt()
Python版本的图标操作就OK了

欢迎关注“搜狗测试”公众号,每天一篇测试相关的文章与您分享,共同讨论软件测试的技术与发展

转载请注明:http://blog.csdn.net/sogouauto

Python写自动化之图标锁定到任务栏或删除图标

标签:win32com   任务栏脱离   unpin   锁定   

原文地址:http://blog.csdn.net/sogouauto/article/details/44082445

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!