TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: diarmuid on March 06, 2008, 05:12:47 AM

Title: copy to the clipboard via lisp
Post by: diarmuid on March 06, 2008, 05:12:47 AM
Is it possible to copy a string from AutoCAD to the clipboard via a lisp expression, so that it can be inserted into another application just by pasting into it (excel).

Thanks in advance
Title: Re: copy to the clipboard via lisp
Post by: MP on March 06, 2008, 06:25:15 AM
Number of options, best bet IMO? Exploit DOS LIB (http://en.wiki.mcneel.com/default.aspx/McNeel/DOSlib.html#Functions).
Title: Re: copy to the clipboard via lisp
Post by: Patrick_35 on March 06, 2008, 11:16:30 AM
Hi

An example

Code: [Select]
(defun GetClipText(/ html result)
  (setq html (vlax-create-object "htmlfile")
result (vlax-invoke (vlax-get (vlax-get html 'ParentWindow) 'ClipBoardData) 'GetData "Text")
  )
  (vlax-release-object html)
  result
)

@+
Title: Re: copy to the clipboard via lisp
Post by: MP on March 06, 2008, 11:20:13 AM
Be mindful that attempting to set the clipboard contents that route can throw a security warning.
Title: Re: copy to the clipboard via lisp
Post by: xshrimp on March 07, 2008, 10:19:28 AM
;;往剪贴板写文字
;;copy str to the clipboard
Code: [Select]
(defun setClipText(str / html result)
(if (= 'STR (type str))
  (progn
  (setq html   (vlax-create-object "htmlfile")
        result (vlax-invoke (vlax-get (vlax-get html 'ParentWindow) 'ClipBoardData) 'setData "Text" str)
  )
  (vlax-release-object html)
   str
   )
 );end if
)
Title: Re: copy to the clipboard via lisp
Post by: It's Alive! on March 07, 2008, 10:50:22 AM
Welcome to the Swamp xshrimp  :-)
Title: Re: copy to the clipboard via lisp
Post by: MP on March 07, 2008, 11:03:55 AM
2 things --

(1) Welcome to the swamp xshrimp, nice entry.

(2) Apologies to Patrick / all. I could have sworn I had read in Technet a year or so ago (no mention here (http://www.microsoft.com/technet/scriptcenter/resources/qanda/feb05/hey0221.mspx)) that going this route (ie abusing an html object or iexplorer) would throw a security alert, but I've run a couple tests and have seen no such alert. So my apologies and nod for job well done.

:)
Title: Re: copy to the clipboard via lisp
Post by: MP on March 07, 2008, 01:27:14 PM
Thanks to Patrick_35 and XShrimp, this has gone into my library --

Code: [Select]
(defun _SetClipBoardText ( text / htmlfile result )

    ;;  Caller's sole responsibility is to pass a
    ;;  text string. Anything else? Pie in face.

    ;;  Attribution: Reformatted version of
    ;;  post by XShrimp at theswamp.org.
    ;;
    ;;  See http://tinyurl.com/2ngf4r.

    (setq result
        (vlax-invoke
            (vlax-get
                (vlax-get
                    (setq htmlfile (vlax-create-object "htmlfile"))
                   'ParentWindow
                )
               'ClipBoardData
            )
           'SetData
            "Text"
            text
        )
    )

    (vlax-release-object htmlfile)

    text

)

and ...

Code: [Select]
(defun _GetClipBoardText( / htmlfile result )

    ;;  Attribution: Reformatted version of
    ;;  post by Patrick_35 at theswamp.org.
    ;;
    ;;  See http://tinyurl.com/2ngf4r.

    (setq result
        (vlax-invoke
            (vlax-get
                (vlax-get
                    (setq htmlfile (vlax-create-object "htmlfile"))
                   'ParentWindow
                )
               'ClipBoardData
            )
           'GetData
            "Text"
        )
    )

    (vlax-release-object htmlfile)

    result

)

Thanks folks.

:)
Title: Re: copy to the clipboard via lisp
Post by: Patrick_35 on March 07, 2008, 02:49:57 PM
It is a pleasure to share and a fair return that gave me the website  :-)

To alert post, I asked him when read, but as I had no warning message, I expected to see more.

Prevention is better than cure.

@+
Title: Re: copy to the clipboard via lisp
Post by: ASMI on March 08, 2008, 03:06:23 PM
> Patrick_35

That's wonderful! Thank you  :-)
Title: Re: copy to the clipboard via lisp
Post by: VVA on March 10, 2008, 10:10:38 AM
I do not know, that it is better: to create object "htmlfile" or " InternetExplorer. Application ". And can be all the same.
I reached up to clipboard through InternetExplorer. Application
Code: [Select]
;  ! ***************************************************************************
;; !                           copyToclipboard
;; ! ***************************************************************************
;; ! Function : Copy text to clipboard
;; ! Argument : 'str'     - String
;; ! Returns  : nil
;; ! ****************************************************************************

(defun copyToclipboard ( str / ieobj cbrd)
  (setq ieobj (vlax-get-or-create-object
                      "InternetExplorer.Application"
                      )
             )
 (vlax-invoke ieobj 'navigate2 "about:blank")
 (vlax-invoke
(setq cbrd (vlax-get (vlax-get (vlax-get ieobj 'document) 'parentwindow)
'clipboarddata
))
'setdata
"text"
str
)
(vlax-release-object ieobj) 
)

;  ! ***************************************************************************
;; !                           Clearclipboard
;; ! ***************************************************************************
;; ! Function : Clear clipboard
;; ! Argument : nil
;; ! Returns  : nil
;; ! ****************************************************************************;

(defun Clearclipboard (  / ieobj)
  (setq ieobj (vlax-get-or-create-object
                      "InternetExplorer.Application"
                      )
             )
 (vlax-invoke ieobj 'navigate2 "about:blank")
 (vlax-invoke
(vlax-get (vlax-get (vlax-get ieobj 'document) 'parentwindow)
'clipboarddata
)
'clearData
"text"
)
(vlax-release-object ieobj) 
)

;  ! ***************************************************************************
;; !                           Getclipboard
;; ! ***************************************************************************
;; ! Function : Return text string from clipboard
;; ! Argument : nil
;; ! Returns  : string
;; ! ****************************************************************************

(defun Getclipboard ( / ieobj str)
  (setq ieobj (vlax-get-or-create-object
                      "InternetExplorer.Application"
                      )
             )
 (vlax-invoke ieobj 'navigate2 "about:blank")
 (setq str(vlax-invoke
(vlax-get (vlax-get (vlax-get ieobj 'document) 'parentwindow)
'clipboarddata
)
'getData
"text"
))
(vlax-release-object ieobj)
  str
)

Title: Re: copy to the clipboard via lisp
Post by: VovKa on March 11, 2008, 03:53:29 PM
I do not know, that it is better: to create object "htmlfile" or " InternetExplorer. Application ". And can be all the same.
actually it's not the same. method presented by xshrimp is preferrable, because InternetExplorer may not always be present on the system. in my case it was removed with the help of nLite, so you code is not working.
Title: Re: copy to the clipboard via lisp
Post by: Fatty on March 11, 2008, 04:37:07 PM
I do not know, that it is better: to create object "htmlfile" or " InternetExplorer. Application ". And can be all the same.
actually it's not the same. method presented by xshrimp is preferrable, because InternetExplorer may not always be present on the system. in my case it was removed with the help of nLite, so you code is not working.
FYI
The same thing on my machine
I have A2008 and IE7 and VVA's code
is not working for me too

~'J'~

Title: Re: copy to the clipboard via lisp
Post by: VVA on March 12, 2008, 10:24:48 AM
Probably you are right
Title: Re: copy to the clipboard via lisp
Post by: ttechnik on March 09, 2015, 04:41:31 AM
The code is correct. In W2k....
But in W8 not work.
Send safety messages, but the window isn't active.

is someone's idea?