Author Topic: ShortCuts.lsp  (Read 9403 times)

0 Members and 1 Guest are viewing this topic.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
ShortCuts.lsp
« 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.

:)
« Last Edit: April 29, 2011, 10:22:25 AM by MP »
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

jbuzbee

  • Swamp Rat
  • Posts: 851
Re: ShortCuts.lsp
« Reply #1 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-)
James Buzbee
Windows 8

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: ShortCuts.lsp
« Reply #2 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.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

jbuzbee

  • Swamp Rat
  • Posts: 851
Re: ShortCuts.lsp
« Reply #3 on: September 23, 2008, 09:52:52 AM »
Quote
Oh look, my boss. Recloak.


 :lmao:
James Buzbee
Windows 8

Harrie

  • Guest
Re: ShortCuts.lsp
« Reply #4 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.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: ShortCuts.lsp
« Reply #5 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?
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

ronjonp

  • Needs a day job
  • Posts: 7526
Re: ShortCuts.lsp
« Reply #6 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
« Last Edit: September 26, 2008, 11:01:47 AM by ronjonp »

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

ahankhah

  • Guest
Re: ShortCuts.lsp
« Reply #7 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?

Lee Mac

  • Seagull
  • Posts: 12905
  • London, England
Re: ShortCuts.lsp
« Reply #8 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  :-)

ronjonp

  • Needs a day job
  • Posts: 7526
Re: ShortCuts.lsp
« Reply #9 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

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: ShortCuts.lsp
« Reply #10 on: April 29, 2011, 10:20:56 AM »
Nice addition to the thread Ron. :)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: ShortCuts.lsp
« Reply #11 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. :)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

ronjonp

  • Needs a day job
  • Posts: 7526
Re: ShortCuts.lsp
« Reply #12 on: April 29, 2011, 10:48:13 AM »
Nice addition to the thread Ron. :)
:-)

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

ronjonp

  • Needs a day job
  • Posts: 7526
Re: ShortCuts.lsp
« Reply #13 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)
)

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

Pepe

  • Newt
  • Posts: 85
Re: ShortCuts.lsp
« Reply #14 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.