TheSwamp
Code Red => AutoLISP (Vanilla / Visual) => Topic started by: MP on September 22, 2008, 01:59:54 PM
-
Given the thread in the VB forum I thought someone may need the lisp equivalent ...
CreateShortCut
(defun _CreateShortCut ( shortCutPath targetPath / shell sc result )
;; If the shortcut is created T is returned, otherwise
;; nil. Does not validate the target path, that's the
;; responsibility of the caller.
(vl-catch-all-apply
'(lambda ( )
(setq
shell (vlax-create-object "WScript.Shell")
sc (vlax-invoke shell 'CreateShortcut shortCutPath)
)
(vlax-put-property sc 'TargetPath targetPath) [color=green];; added 2011/04/29 per Ron's post above[/color]
(vlax-invoke sc 'save)
(setq result t)
)
)
(if (eq 'vla-object (type sc)) (vlax-release-object sc))
(if (eq 'vla-object (type shell)) (vlax-release-object shell))
result
)
GetShortCutTarget
(defun _GetShortCutTarget ( shortCutPath / shell sc result )
;; If the shortcut exists the target path is returned
;; (though said path is not validated).
(vl-catch-all-apply
'(lambda ( )
(setq
shell (vlax-create-object "WScript.Shell")
sc (vlax-invoke shell 'CreateShortcut shortCutPath)
result (vlax-get sc 'TargetPath)
)
)
)
(if (eq 'vla-object (type sc)) (vlax-release-object sc))
(if (eq 'vla-object (type shell)) (vlax-release-object shell))
result
)
Nominally tested but should work.
:)
-
Wow - is this the lisp equivalent of what you posted over in the VB(A) thread? If so, Lisp is much cleaner eh? 8-)
-
Not really. It was spawned from, but it's not a translation of the VB code. Plus -- the VB code was more ambitious, recursively processing what directory was passed to it.
The VB version of this LISP wuold be more like (not saying it's good code) --
Function GetShortCutTarget(shortCutPath As String) As String
On Error Resume Next
Dim shell As Object, _
shortcut As Object, _
result As String
Set shell = CreateObject("WScript.Shell")
Set shortcut = shell.CreateShortcut(shortCutPath)
result = shortcut.TargetPath
If Err.Number Then Err.Clear
GetShortCutTarget = result
End Function
etc. so it's about as clean / dirty as the LISP is. Mind you a lot has to do with who is the author, i.e. I bashed the lisp and vb out quick; I'm sure it could (and probably should) be written better, I just don't have the time right now.
Oh look, my boss. Recloak.
-
Oh look, my boss. Recloak.
:lmao:
-
Sorry,
When I use
(_CreateShortcut "c:\\Windows\\Desktop\\Hearts.lnk" "c:\\Windows\\Hearts.exe")
nothing is happening.
What is wrong?
Regards Harrie.
-
Call me Daphne, but that doesn't look like a valid path to me.
What does this return ... (vl-file-directory-p "c:\\Windows\\Desktop")
If it's legit, do you have full read-write access?
Other than that, what version of AutoCAD? Windoze?
-
Try this instead Harrie:
(_CreateShortcut
(strcat (getenv "userprofile") "\\Desktop\\Hearts.lnk")
(strcat (getenv "windir") "\\Hearts.exe")
)
Actually I could not get it to work either...I had to add (vlax-put-property sc 'TargetPath targetPath) to Michael's code above to get it to work.
(defun _CreateShortCut ( shortCutPath targetPath / shell sc result )
;; If the shortcut is created T is returned, otherwise
;; nil. Does not validate the target path, that's the
;; responsibility of the caller.
(vl-catch-all-apply
'(lambda ( )
(setq
shell (vlax-create-object "WScript.Shell")
sc (vlax-invoke shell 'CreateShortcut shortCutPath)
)
(vlax-put-property sc 'TargetPath targetPath)
(vlax-invoke sc 'save)
(setq result t)
)
)
(if (eq 'vla-object (type sc)) (vlax-release-object sc))
(if (eq 'vla-object (type shell)) (vlax-release-object shell))
result
)
Ron
-
A useful code.
1- Is it possible to pass a folder address to (_CreateShortcut) function as the second argument?
(_CreateShortcut "c:\\test.lnk" "d:\\")
2- Also how should add an icon to the shortcut?
-
I should imagine it would be:
(_CreateShortcut "c:\\test.lnk" "d:")
/guess
Welcome to theSwamp Ahankhah :-)
-
And to change the image of the link you'd need to add:
(vlax-put-property sc 'IconLocation "C:\\Windows\\System32\\shell32.dll, 9")
Where the number in red is the icon index in the dll.
Welcome X2 :-)
http://msdn.microsoft.com/en-us/library/3s9bx7at(v=vs.85).aspx
-
Nice addition to the thread Ron. :)
-
A useful code.
1- Is it possible to pass a folder address to (_CreateShortcut) function as the second argument?
A quick test suggests yes, a folder as the target argument is fine. :)
-
Nice addition to the thread Ron. :)
:-)
-
Here's a quick example (using shell32.dll) to give you some ideas.
(defun _createshortcut (shortcutpath targetpath index / icon result sc shell)
;; If the shortcut is created T is returned, otherwise
;; nil. Does not validate the target path, that's the
;; responsibility of the caller.
(vl-catch-all-apply
'(lambda ()
(setq shell (vlax-create-object "WScript.Shell")
icon (strcat (vlax-invoke shell 'expandenvironmentstrings "%windir%")
"\\System32\\shell32.dll, "
)
sc (vlax-invoke shell 'createshortcut shortcutpath)
)
(vlax-put-property sc 'targetpath targetpath)
(vlax-put-property sc 'iconlocation (strcat icon (itoa index)))
(vlax-invoke sc 'save)
(setq result t)
)
)
(if (eq 'vla-object (type sc))
(vlax-release-object sc)
)
(if (eq 'vla-object (type shell))
(vlax-release-object shell)
)
result
)
(defun c:test (/ desktop homedrive shell)
(setq shell (vlax-create-object "WScript.Shell")
desktop (strcat (vlax-invoke shell 'expandenvironmentstrings "%userprofile%") "\\desktop\\")
homedrive (vlax-invoke shell 'expandenvironmentstrings "%homedrive%")
)
(_createshortcut (strcat desktop (substr homedrive 1 1) " - Drive.lnk") (strcat homedrive "\\") 10)
)
-
Hi!
One simple (I hope) question:
Using these methods would I be able to get which Autocad's shorcutmenu has been used for last time? I mean EDIT, etc...
Many months ago I asked for a way to get it in order to know which menu item has been clicked in a GRREAD loop. I'm still looking for it :|.
Thanks in advance.
-
I should imagine it would be:
(_CreateShortcut "c:\\test.lnk" "d:")
/guess
Welcome to theSwamp Ahankhah :-)
Lee,
nice to see you here.
You are right as always.
Thank you very much.
-
Welcome X2 :-)
Thank you very much.
And to change the image of the link you'd need to add:
(vlax-put-property sc 'IconLocation "C:\\Windows\\System32\\shell32.dll, 9")
Where the number in red is the icon index in the dll.
ronjonp,
I appreciate your help.
-
Is it possible to pass a folder address to (_CreateShortcut) function as the second argument?
A quick test suggests yes, a folder as the target argument is fine. :)
Yes, One must pass folder name to he function without trailing slash:
right: (_CreateShortcut (strcat (getenv "userprofile") "\\Desktop\\Shortcut to Z.lnk")
"Z:")
false: (_CreateShortcut (strcat (getenv "userprofile") "\\Desktop\\Shortcut to Z.lnk")
"Z:\\")
-
Welcome X2 :-)
Thank you very much.
And to change the image of the link you'd need to add:
(vlax-put-property sc 'IconLocation "C:\\Windows\\System32\\shell32.dll, 9")
Where the number in red is the icon index in the dll.
ronjonp,
I appreciate your help.
No prob :)
-
Hi!
One simple (I hope) question:
Using these methods would I be able to get which Autocad's shorcutmenu has been used for last time? I mean EDIT, etc...
Many months ago I asked for a way to get it in order to know which menu item has been clicked in a GRREAD loop. I'm still looking for it .
Thanks in advance.
I can hear the wind creeping through the frozen moors... It makes me shiver...