TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: MP on September 22, 2008, 01:59:54 PM

Title: ShortCuts.lsp
Post 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

Code: [Select]
(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

Code: [Select]
(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.

:)
Title: Re: ShortCuts.lsp
Post by: jbuzbee on September 23, 2008, 08:57:37 AM
Wow - is this the lisp equivalent of what you posted over in the VB(A) thread?  If so, Lisp is much cleaner eh?  8-)
Title: Re: ShortCuts.lsp
Post by: MP on September 23, 2008, 09:26:40 AM
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) --

Code: [Select]
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.
Title: Re: ShortCuts.lsp
Post by: jbuzbee on September 23, 2008, 09:52:52 AM
Quote
Oh look, my boss. Recloak.


 :lmao:
Title: Re: ShortCuts.lsp
Post by: Harrie on September 26, 2008, 09:50:46 AM
Sorry,

When I use
Code: [Select]
(_CreateShortcut "c:\\Windows\\Desktop\\Hearts.lnk" "c:\\Windows\\Hearts.exe")
nothing is happening.
What is wrong?

Regards Harrie.
Title: Re: ShortCuts.lsp
Post by: MP on September 26, 2008, 10:09:56 AM
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?
Title: Re: ShortCuts.lsp
Post by: ronjonp on September 26, 2008, 10:23:23 AM
Try this instead Harrie:

Code: [Select]
(_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.

Code: [Select]
(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
Title: Re: ShortCuts.lsp
Post by: ahankhah on April 29, 2011, 09:58:42 AM
A useful code.
1- Is it possible to pass a folder address to (_CreateShortcut) function as the second argument?
Code: [Select]
(_CreateShortcut "c:\\test.lnk" "d:\\")
2- Also how should add an icon to the shortcut?
Title: Re: ShortCuts.lsp
Post by: Lee Mac on April 29, 2011, 10:07:43 AM
I should imagine it would be:

Code: [Select]
(_CreateShortcut "c:\\test.lnk" "d:")
/guess

Welcome to theSwamp Ahankhah  :-)
Title: Re: ShortCuts.lsp
Post by: ronjonp on April 29, 2011, 10:14:54 AM
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
Title: Re: ShortCuts.lsp
Post by: MP on April 29, 2011, 10:20:56 AM
Nice addition to the thread Ron. :)
Title: Re: ShortCuts.lsp
Post by: MP on April 29, 2011, 10:23:43 AM
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. :)
Title: Re: ShortCuts.lsp
Post by: ronjonp on April 29, 2011, 10:48:13 AM
Nice addition to the thread Ron. :)
:-)
Title: Re: ShortCuts.lsp
Post by: ronjonp on April 29, 2011, 11:18:34 AM
Here's a quick example (using shell32.dll) to give you some ideas.

Code: [Select]
(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)
)
Title: Re: ShortCuts.lsp
Post by: Pepe on April 29, 2011, 05:55:43 PM
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.
Title: Re: ShortCuts.lsp
Post by: ahankhah on April 30, 2011, 04:23:05 AM
I should imagine it would be:

Code: [Select]
(_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.
Title: Re: ShortCuts.lsp
Post by: ahankhah on April 30, 2011, 04:30:40 AM
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.
Title: Re: ShortCuts.lsp
Post by: ahankhah on April 30, 2011, 04:40:36 AM
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:
Code: [Select]
right: (_CreateShortcut (strcat (getenv "userprofile") "\\Desktop\\Shortcut to Z.lnk")
                 "Z:")
false: (_CreateShortcut (strcat (getenv "userprofile") "\\Desktop\\Shortcut to Z.lnk")
                 "Z:\\")
Title: Re: ShortCuts.lsp
Post by: ronjonp on April 30, 2011, 05:34:20 PM
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 :)
Title: Re: ShortCuts.lsp
Post by: Pepe on May 01, 2011, 08:34:27 AM
Quote
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...