Scripting a Popup Shortcut

I have seen several articles explaining methods for scripting a shortcut for an object. Could anyone suggest a method allowing the scripted shortcut to be opened as a folder shortcut popup menu?

I would use the object properties for this except that I don't want the object to react as a shortcut every time files are dragged to it.
11,670 views 21 replies
Reply #1 Top
Depending on what your doing, why not use the "shortcut" object type?

Under the General tab select change beside "Object Type"
Choose shortcut in drop list box
Choose Folder
Check the Menu Location
Reply #2 Top
Thanks for the suggestion, however that doesn't seem to work for my application:


"I would use the object properties for this except that I don't want the object to react as a shortcut every time files are dragged to it."

-Let's say that you were creating an object titled "Drop Files Here to Create a Shortcut". The first time a file or folder was dropped ont the object its path is used to set the object's shortcut. After that you want the object to act as a shortcut to that file or folder using the popup folder.

If you use the "shortcut" object type it acts as a shortcut all the time, even if you set the shortcut path to "" it will default to "My Computer". The first time you drop a file on the object it gets sent to the defalut shortcut. This sets up an interesting Catch 22 situation; the object shortcut was set with path of the file when it was dropped but the object moved the file to the object's default path then set the object path to where the file used to be so the new shortcut is always invalid.

Would you know of any way to set the "object type" from the script?

-Dave-
Reply #3 Top
I think you'd have to create your own r-click menu listing the contents of the target folder.

The basic concept is to drag and drop a folder, get the contents of the folder, put them in an array, and use the array to build a custom r-click menu.

I was working on something similar, and I think with a little tweaking it could be achieved.

Reply #4 Top
Alrighty, this was pieced together from some of scripts I've saved. This goes into the one object. It will take one file or folder. If it's a file it'll open it on l-click. If it's a folder it will show a pop-up menu on l-click.


Code: vbscript
  1. Dim fso, fname(), fpath()
  2. 'Called when the script is executed
  3. Sub Object_OnScriptEnter
  4. Set fso = CreateObject("Scripting.FileSystemObject")
  5. End Sub
  6. 'Called when the script is terminated
  7. Sub Object_OnScriptExit
  8. End Sub
  9. 'Called when files are dropped onto object
  10. Sub Object_OnDropFiles(files)
  11. 'Get the first file dropped
  12. If Instr(files, "|") > 0 Then
  13. fArray = Split(files, "|")
  14. f1 = LBound(fArray)
  15. Else
  16. f1 = files
  17. End If
  18. 'If folder, call function to get contents
  19. If fso.GetExtensionName(f1) = "" Then
  20. Object.ToolTipText = fso.GetBaseName(f1)'--Set tooltip to foldername
  21. GetFolderContents(f1)
  22. 'If file, set arrays, tooltip to file name
  23. Else
  24. ReDim Preserve fname(0), fpath(0)
  25. fname(0) = fso.GetBaseName(f1)
  26. Object.ToolTipText = fso.GetBaseName(f1)
  27. fpath(0) = f1
  28. End If
  29. End Sub
  30. Function GetFolderContents(fldr)
  31. ReDim fname(-1), fpath(-1)
  32. Set f = fso.GetFolder(fldr)
  33. Set fl = f.Subfolders
  34. Set fi = f.files
  35. '--Get subfolders
  36. For Each elem In fl
  37. ReDim Preserve fname(UBound(fname)+1)
  38. ReDim Preserve fpath(UBound(fpath)+1)
  39. fname(UBound(fname)) = fso.GetFileName(elem)
  40. fpath(UBound(fpath)) = fso.GetAbsolutePathName(elem)
  41. Next
  42. '--Get files
  43. For Each elem In fi
  44. ReDim Preserve fname(UBound(fname)+1)
  45. fname(UBound(fname)) = fso.GetFileName(elem)
  46. ReDim Preserve fpath(UBound(fpath)+1)
  47. fpath(UBound(fpath)) = fso.GetAbsolutePathName(elem)
  48. Next
  49. End Function
  50. Function Object_OnLButtonUpEx(obj,x,y,dragged)
  51. If Not dragged Then
  52. '--If folder, show pop up menu
  53. If UBound(fname) > 0 Then
  54. Call CreateSectionMenu()
  55. '--If file, open file
  56. Else
  57. On Error Resume Next
  58. Set Sh = CreateObject("WScript.Shell")
  59. target = fpath(0)
  60. Sh.Run (Chr(34)& target & Chr(34))
  61. Set Sh = Nothing
  62. End If
  63. End If
  64. End Function
  65. Sub CreateSectionMenu()
  66. Set mainmenu = nothing
  67. Set mainmenu = DesktopX.CreatePopupMenu
  68. Set submenu= DesktopX.CreatePopupMenu
  69. For x = 0 To UBound(fname)
  70. mainmenu.AppendMenu 0, x+1, fname(x)
  71. Next
  72. result = mainmenu.TrackPopupMenu(0, System.CursorX, System.CursorY)
  73. Call ExecuteSectionMenu(result,snum)
  74. End Sub
  75. Sub ExecuteSectionMenu(result,snum)
  76. If result > 0 Then
  77. On Error Resume Next
  78. Set Sh = CreateObject("WScript.Shell")
  79. target = fpath(result-1)
  80. Sh.Run (Chr(34)& target & Chr(34))
  81. Set Sh = Nothing
  82. End If
  83. End Sub


Let me know if this is what you're looking for.
Reply #5 Top
Cool!
Reply #6 Top
Thanks, Zubaz.
Reply #7 Top
Cool!
End of quote


Yes, yes she is!

Um you might want to add a line under the Left Click Function
in order to suppress a subscript error if no item has been dropped.

Like This
Code: vbscript
  1. Function Object_OnLButtonUpEx(obj,x,y,dragged)
  2. If Not dragged Then
  3. On Error Resume Next 'Add here
  4. '--If folder, show pop up menu
  5. If UBound(fname) > 0 Then
  6. Call CreateSectionMenu()
  7. '--If file, open file
  8. Else
  9. On Error Resume Next
  10. Set Sh = CreateObject("WScript.Shell")
  11. target = fpath(0)
  12. Sh.Run (Chr(34)& target & Chr(34))
  13. Set Sh = Nothing
  14. End If
  15. End If
  16. End Function


If your heart is set on the same menu style as the default folder menu's then I have an idea but, it would involve three objects.
Reply #8 Top
you might want to add a line under the Left Click Function
End of quote


Perfect! Thanks SirS.
Reply #9 Top
Wow, thanks everybody. This is really good stuff.

sViz, thanks for the fso script, that gives me an idea....


SirSmiley, yes, I am somewhat attached to the to the default folder menu since it allows for a unique browsing experience -the folders show up as folder icons and open automatically on mouse over. What is your idea?
Reply #10 Top
Okay, here goes.

Create three objects called objBase, objFile, objFolder respectively.
objFile should be set up as a file shortcut object.
objFolder should be set up as a Folder Shortcut Object. *Make sure you check the menu option.

Set objFile & objFolder as child objects of objBase and set both to not visible. Everything will be controlled from the main script in the base object.

Here's the script.

Code: vbscript
  1. Const oBase = "objBase"
  2. Const oFile = "objFile"
  3. Const oFldr = "objFolder"
  4. '1 for File|2 for Folder
  5. Dim oCommand
  6. Set oFSO = CreateObject("Scripting.FileSystemObject")
  7. 'Called when a file is dropped onto object
  8. Function Object_OnDropFiles(files)
  9. 'Makes sure only one file or folder is dropped
  10. If Instr(files, "|") = 0 Then
  11. If oFSO.GetExtensionName(files) = "" Then
  12. DesktopX.Object(oFldr).Command=files
  13. oCommand=2
  14. Else
  15. DesktopX.Object(oFile).Command=files
  16. oCommand=1
  17. End If
  18. Else
  19. msgbox "You must drop only 1 file or folder"
  20. End If
  21. End Function
  22. Function Object_OnLButtonUpEx(obj,x,y,dragged)
  23. If Not dragged Then
  24. If oCommand="" Then
  25. msgbox "Please drop a file or folder first"
  26. 'Excutes objFile Command
  27. ElseIf oCommand = 1 Then
  28. DesktopX.Object(oFile).ExecuteCommand
  29. 'Executes objFolder Command
  30. ElseIf oCommand = 2 Then
  31. DesktopX.Object(oFldr).ExecuteCommand
  32. End If
  33. End If
  34. End Function
Reply #11 Top
I'll bet that if I paid more attention I could learn a bunch.  Until then . . I'l just copy and paste what I need. 
Thanks for the effort all.  It rocks how the DX (sub) community shares knowledge.
Reply #12 Top
I'll bet that if I paid more attention I could learn a bunch.
End of quote


Same here!
Reply #13 Top
Oh, I forgot one thing. Before I went away last week I made a drop script based menu bar for my usb drive.

The above script doesn't work when compiled to a gadget; so, I'm not sure how it will work in widgets or themes. I used an ini file but, persiststorage most likely will be the easiest way.

Here's a script that uses persiststorage. Notice the additional function.

Code: vbscript
  1. Const oBase = "objBase"
  2. Const oFile = "objFile"
  3. Const oFldr = "objFolder"
  4. '1 for File|2 for Folder
  5. Dim oCommand
  6. Set oFSO = CreateObject("Scripting.FileSystemObject")
  7. 'Called when a file is dropped onto object
  8. Function Object_OnDropFiles(files)
  9. 'Makes sure only one file or folder is dropped
  10. If Instr(files, "|") = 0 Then
  11. 'Saves dropped file/folder into a persist storage variable
  12. Object.PersistStorage("objShortcut") = files
  13. Else
  14. msgbox "You must drop only 1 file or folder"
  15. End If
  16. Call filefldrCheck
  17. End Function
  18. Function filefldrCheck
  19. 'Loads "objShortuct" Persist Storage variable
  20. oShorcut=Object.PersistStorage("objShortcut")
  21. If oFSO.GetExtensionName(oShorcut) = "" Then
  22. DesktopX.Object(oFldr).Command=oShorcut
  23. DesktopX.Object(oBase).ToolTipText=oFSO.GetBaseName(oShorcut)
  24. oCommand=2
  25. Else
  26. DesktopX.Object(oFile).Command=oShorcut
  27. DesktopX.Object(oBase).ToolTipText=oFSO.GetBaseName(oShorcut)
  28. oCommand=1
  29. End If
  30. End Function
  31. Function Object_OnLButtonUpEx(obj,x,y,dragged)
  32. If Not dragged Then
  33. If oCommand="" Then
  34. msgbox "Please drop a file or folder first"
  35. 'Excutes objFile Command
  36. ElseIf oCommand = 1 Then
  37. DesktopX.Object(oFile).ExecuteCommand
  38. 'Executes objFolder Command
  39. ElseIf oCommand = 2 Then
  40. DesktopX.Object(oFldr).ExecuteCommand
  41. End If
  42. End If
  43. End Function
Reply #14 Top
I'll bet that if I paid more attention I could learn a bunch.


Same here
End of quote
Ummm . . dude . . I was talking about learnign from you too. 
Reply #15 Top
Too cool, SirS. I was having some trouble setting the correct folder location OnDrop because the target was originally set to one of the preset folders. After I browsed for and set it to a different location it worked perfectly. This script is a keeper!


It rocks how the DX (sub) community shares knowledge.
End of quote


Agreed! I learn something new everyday. Gotta love this place.
Reply #16 Top
Yeah, I've got everyone fooled!

It looks complex but, if you've worked with statements/formulas in Excel it's just a matter of finding things in the DesktopX Documentation and the WSH SDK Help File.

Edit: SViz, yeah I had that problem with the menu bar gadget. Probably best to set the command object to a null value in the Exit Script if it appears again.

Oh! One more thing if you're using the Persist Storage Script then maybe put the Call filefldrCheck into the OnScriptEnter Sub.
Reply #17 Top
Really Nice Scripts.
Reply #18 Top

Again, Wow! Thanks for all the help, I'm learning a lot.

SirSmiley, This is very close to the approach I tried except that I had no File System Object script (this is much neater) and I only used a foder object child (didn't think about the file shortcut being different) and I did a poor job at checking for errors ...and it took me a month to figure out. Other than that it's identical :)

Now for the tricky part: Lets say that after you drop a folder the object goes from "Drop Files Here to Create a Shortcut" to "I am a shortcut to the folder you just dropped on me" and now allows drag and drop to the shortcut set in "objFldr". Since the object is a child of "objBase", files that are dropped on the parent don't make it to the child. Is there a way to either pass the dropped file through to the child or promote the child to the parent?

Thanks,

-Dave-
Reply #19 Top
Was about to say you really don't need the file object but, then it sort of depends on your needs. If you want to use command parameters it makes it simple but, that can also be scripted.

To answer your question really depends on a few things but, I'm assuming you want the ability for the shortcut to be dynamic? So, I scripted that into a right click function.
You need to set the objFldr object Activation to visible area and set the transparency to 0 then still set visibility to no and make sure it's the same size as the base object.

Oh, last night I scripted in the ability to change the base object to a png or ico image. You should also note that this script doesn't clean out persist storage cache;so, if you reset it then don't drop anything it will call the settings on the next load.

Code: vbscript
  1. Const oBase = "objBase"
  2. Const oFile = "objFile"
  3. Const oFldr = "objFolder"
  4. '1 for File|2 for Folder
  5. Dim oCommand
  6. 'Object.Command and Object.CommandParams
  7. 'Object.ExecuteCommand
  8. 'Object.PersistStorage
  9. 'Object.Command = "c:\program files\Internet Explorer\iexplore.exe"
  10. 'Object.CommandParams = "https://www.wincustomize.com"
  11. 'Called when a file is dropped onto object
  12. Function Object_OnDropFiles(files)
  13. 'Makes sure only one file or folder is dropped
  14. If Instr(files, "|") = 0 Then
  15. Set oFSO = CreateObject("Scripting.FileSystemObject")
  16. 'Checks to see if dropped item is an image file for the icon
  17. oDropItem=oFSO.GetExtensionName(files)
  18. If oDropItem = "ico" Or oDropItem = "png" Then
  19. DesktopX.Object(oBase).Picture = files
  20. Object.PersistStorage("objIcon")=files
  21. Else
  22. 'Saves dropped file/folder into a persist storage variable
  23. Object.PersistStorage("objShortcut") = files
  24. End If
  25. Else
  26. msgbox "You must drop only 1 file or folder"
  27. End If
  28. Call filefldrCheck
  29. End Function
  30. 'Called on Left Click Up
  31. Function Object_OnLButtonUpEx(obj,x,y,dragged)
  32. If Not dragged Then
  33. Select Case obj.name
  34. Case "oBase"
  35. If oCommand="" Then
  36. msgbox "Please drop a file or folder first"
  37. 'Excutes objFile Command
  38. ElseIf oCommand = 1 Then
  39. DesktopX.Object(oFile).ExecuteCommand
  40. 'Executes objFolder Command
  41. ElseIf oCommand = 2 Then
  42. DesktopX.Object(oFldr).ExecuteCommand
  43. End If
  44. End Select
  45. End If
  46. End Function
  47. ' Called on Right Click up
  48. Function Object_OnRButtonUpEx(obj,x,y,dragged)
  49. If Not dragged Then
  50. Select Case obj.name
  51. Case oFldr
  52. Call resetSettings
  53. End Select
  54. End If
  55. End Function
  56. Function filefldrCheck
  57. DesktopX.Object(oBase).Picture=Object.PersistStorage("objIcon")
  58. Set oFSO = CreateObject("Scripting.FileSystemObject")
  59. 'Loads "objShortuct" Persist Storage variable
  60. oShorcut=Object.PersistStorage("objShortcut")
  61. oIcon=Object.PersistStorage("objIcon")
  62. If oIcon="" Then oIcon="red.png"
  63. DesktopX.Object(oBase).Picture=oIcon
  64. If oFSO.GetExtensionName(oShorcut) = "" Then
  65. DesktopX.Object(oFldr).Command=oShorcut
  66. DesktopX.Object(oBase).ToolTipText=oFSO.GetBaseName(oShorcut)
  67. oCommand=2
  68. Else
  69. DesktopX.Object(oFile).Command=oShorcut
  70. DesktopX.Object(oBase).ToolTipText=oFSO.GetBaseName(oShorcut)
  71. oCommand=1
  72. End If
  73. If oCommand = 2 Then
  74. DesktopX.Object(oFldr).Visible = True
  75. DesktopX.Object(oFldr).OnTop
  76. Exit Function
  77. End If
  78. Set oFSO = nothing
  79. End Function
  80. 'Called when the script is executed
  81. Sub Object_OnScriptEnter
  82. Call filefldrCheck
  83. End Sub
  84. 'Cleans up items when executed
  85. Sub cleanup
  86. With DesktopX
  87. .Object(oFldr).Command=""
  88. .Object(oFile).Command=""
  89. .Object(oBase).ToolTipText=""
  90. .Object(oBase).Picture="red.png" 'This should be your default image
  91. .Object(oFldr).Visible = False
  92. End With
  93. End Sub
  94. 'Resets & Clears settings when executed
  95. Sub resetSettings
  96. DesktopX.Object(oFldr).Visible = False
  97. DesktopX.Object(oBase).Picture="red.png" 'This should be your default image
  98. oCommand=""
  99. Object.PersistStorage("objIcon")=""
  100. Object.PersistStorage("objShortcut") = ""
  101. End Sub
  102. 'Called when the script is terminated
  103. Sub Object_OnScriptExit
  104. Call cleanup
  105. End Sub
Reply #20 Top
you all are doing some great stuff here.. love it!
Reply #21 Top

I believe there is a typographcial error in the Case statement for the left click function above:

Case "oBase"

The quotes refer to a variable rather than the literal expression. Removing the quotes allows the left click to function properly:

Case oBase


-Dave-