How to check if application is running using AutoLISP - autocad

In CAD application (ZWCAD) I start my application by AutoLISP.
(startapp "C://[path]//Application.exe")
so the application runs each time new file is created. Works OK.
Now I want to limit instances of application just to one.
So how can I check if the application is already running?

Today I found easier way. Ready to use LISP function
(dos_processes)
Which returns list of all running processes.

You could use a function to query the Win32_Process WMI class for a process with name matching your given application.
Such a function might be written in the following way:
;; Win32 Process-p - Lee Mac
;; Returns T if a process exists with the supplied name
(defun LM:win32process-p ( pro / qry rtn srv wmi )
(if (setq wmi (vlax-create-object "wbemscripting.swbemlocator"))
(progn
(setq rtn
(vl-catch-all-apply
'(lambda ( )
(setq srv (vlax-invoke wmi 'connectserver)
qry (vlax-invoke srv 'execquery (strcat "select * from win32_process where name = '" pro "'"))
)
(< 0 (vla-get-count qry))
)
)
)
(foreach obj (list qry srv wmi)
(if (= 'vla-object (type obj)) (vlax-release-object obj))
)
(and (not (vl-catch-all-error-p rtn)) rtn)
)
)
)
Which could be called in the following way:
_$ (LM:win32process-p "notepad.exe")
T

Related

lsp-mode for go erring out with "no views in session"

What I'm trying to accomplish
I'd like to use go-mode/lsp-mode together. I struggle to get lsp-mode to even execute at first, finally got it to work by appending the paths :facepalm:.
The issue
The issue now is that when lsp-mode starts up for the working golang directory, I receive this error:
LSP :: Error from the Language Server: no views in the session (Unknown error) [3 times]
I've searched around the internet for answers, but have yet to find anything that is relevant to my problem. I'm reaching out to the community for some guidance.
go-mode.el
(defun custom-go-mode ()
(display-line-numbers-mode 1))
(use-package go-mode
:defer t
:ensure t
:mode ("\\.go\\'" . go-mode)
:init
(setq compile-command "echo Building... && go build -v && echo Testing... && go test -v && echo Linter... && golint")
(setq compilation-read-command nil)
(add-hook 'go-mode-hook 'custom-go-mode)
:bind (("M-," . compile)
("M-." . godef-jump)))
(setq compilation-window-height 14)
(defun my-compilation-hook ()
(when (not (get-buffer-window "*compilation*"))
(save-selected-window
(save-excursion
(let* ((w (split-window-vertically))
(h (window-height w)))
(select-window w)
(switch-to-buffer "*compilation*")
(shrink-window (- h compilation-window-height)))))))
(add-hook 'compilation-mode-hook 'my-compilation-hook)
(global-set-key (kbd "C-c C-c") 'comment-or-uncomment-region)
(setq compilation-scroll-output t)
lsp-mode.el
(setq exec-path (append exec-path '("/Users/seanh/.nvm/versions/node/v12.13.0/bin/npm")))
(setq exec-path (append exec-path '("/Users/seanh/.nvm/versions/node/v12.13.0/bin/")))
(setq exec-path (append exec-path '("/usr/local/bin")))
(setq exec-path (append exec-path '("/Users/seanh/go/bin")))
(setq exec-path (append exec-path '("/Users/seanh/go/bin/gopls")))
(setq exec-path (append exec-path '("/usr/local/go/bin/go")))
(use-package lsp-mode
:ensure t
:commands (lsp lsp-deferred)
:hook (go-mode . lsp-deferred))
;;Set up before-save hooks to format buffer and add/delete imports.
;;Make sure you don't have other gofmt/goimports hooks enabled.
(defun lsp-go-install-save-hooks ()
(add-hook 'before-save-hook #'lsp-format-buffer t t)
(add-hook 'before-save-hook #'lsp-organize-imports t t))
(add-hook 'go-mode-hook #'lsp-go-install-save-hooks)
;;Optional - provides fancier overlays.
(use-package lsp-ui
:ensure t
:commands lsp-ui-mode
:init
)
;;Company mode is a standard completion package that works well with lsp-mode.
;;company-lsp integrates company mode completion with lsp-mode.
;;completion-at-point also works out of the box but doesn't support snippets.
(use-package company
:ensure t
:config
(setq company-idle-delay 0)
(setq company-minimum-prefix-length 1))
(use-package company-lsp
:ensure t
:commands company-lsp)
;;Optional - provides snippet support.
(use-package yasnippet
:ensure t
:commands yas-minor-mode
:hook (go-mode . yas-minor-mode))
;;lsp-ui-doc-enable is false because I don't like the popover that shows up on the right
;;I'll change it if I want it back
(setq lsp-ui-doc-enable nil
lsp-ui-peek-enable t
lsp-ui-sideline-enable t
lsp-ui-imenu-enable t
lsp-ui-flycheck-enable t)
I've had this happen when the go environment is not correct.
As a diagnostic try commenting out the go* exec-paths from your lsp-mode.el and set appropriate environment variables (GOROOT, GOPATH and GO111MODULE if you're using it), including adding $GOROOT/bin and $GOPATH/bin to PATH.
Also if still not working verify within emacs those environment variables are set correctly.
For linux of course you can export all that and run emacs from the terminal to test.

Getting the output of a process

I am trying to create a function which takes as input a path and returns the output of the ls terminal command as a string. I'm using a process and sentinel since I'll eventually want to create other functions which will take some time to execute, and I want them to run asynchronously.
(defun ls-to-string (path)
(let (ls-proc
ls-output)
(progn (setq ls-proc (start-process "" "ls-buffer" "ls" path))
(set-process-sentinel ls-proc (lambda (p e)
(if (string= e "finished\n")
(progn (set-buffer "ls-buffer")
(setq ls-output (buffer-string))
(kill-buffer "ls-buffer")
(message ls-output))))) <---- (1)
ls-output))) <---- (2)
(ls-to-string "/home")
I have (temporarily) added (message ls-output) just to show that ls-output does contain the string (1). However the return value is nil (2).

Export Selected Text From AutoCad

I am wondering if any of you fine people can point me in the right direction. I came up with an ssget function that selects the text I'm looking for but I'm not quite sure where to go from there to extract the text to either a txt or csv file.
Here is the ssget function that is working for me
(ssget "_X" '((0 . "TEXT,MTEXT")(1 . "ETCH*,MARK*,STAMP*")))
I need to grab this text from a folder full of drawings and export it to preferably a csv file where I can easily read.
Thanks in advance!
Allan
As per your question, you are extracting text from all drawings of the respective folder you can use ObjectODBX method here you can run this code directly and it extract text from drawing and create csv file with drawing name in the same folder try it you can add filter condition if you required.
(Defun C:ExtractFolderToCSV( / dwgfile filelist textstring f doc LM:GetDocumentObject FolderBox folderpath)
;; Get Document Object - Lee Mac
;; Retrieves the VLA Document Object for the supplied filename.
;; The Document Object may be present in the Documents collection, or obtained through ObjectDBX.
;; It is the callers responsibility to release such object.
;;This function I collect from Lee-Mac Thanks lee
(defun LM:GetDocumentObject (dwg / app dbx dwl err vrs)
(cond
((not (setq dwg (findfile dwg))) nil)
((cdr
(assoc
(strcase dwg)
(vlax-for doc
(vla-get-documents (setq app (vlax-get-acad-object)))
(setq dwl
(cons (cons (strcase (vla-get-fullname doc)) doc) dwl)
)
)
)
)
)
((progn
(setq dbx
(vl-catch-all-apply
'vla-getinterfaceobject
(list app
(if (< (setq vrs (atoi (getvar 'acadver))) 16)
"objectdbx.axdbdocument"
(strcat "objectdbx.axdbdocument." (itoa vrs))
)
)
)
)
(or (null dbx) (vl-catch-all-error-p dbx))
)
(prompt "\nUnable to interface with ObjectDBX.")
)
((vl-catch-all-error-p
(setq err (vl-catch-all-apply 'vla-open (list dbx dwg)))
)
(prompt (strcat "\n" (vl-catch-all-error-message err)))
)
(dbx)
)
)
;This function for select folder
(defun FolderBox (message directory flag / folder sh)
;;I found thiscode on web I am not remember website. sorry for that
;; Arguments:
;; message: the message displayed in th dialog box
;; directory: the directory to browse
;; flag values:
;; 0 = Default
;; 1 = Only file system folders can be selected. If this bit is set, the OK button is disabled if the user selects a folder that doesn't belong to the file system (such as the Control Panel folder).
;; 2 = The user is prohibited from browsing below the domain within a network (during a computer search).
;; 4 = Room for status text is provided under the text box.
;; 8 = Returns file system ancestors only.
;; 16 = Shows an edit box in the dialog box for the user to type the name of an item.
;; 32 = Validate the name typed in the edit box.
;; 512 = None "New folder" button
;; 4096 = Enables the user to browse the network branch of the shell's namespace for computer names.
;; 8192 = Enables the user to browse the network branch of the shell's namespace for printer names.
;; 16384 = Allows browsing for everything.
(vl-load-com)
(setq shell (vlax-create-object "Shell.Application"))
(if (setq
folder (vlax-invoke shell 'browseforfolder 0 message flag directory)
)
(setq folder (vlax-get-property (vlax-get-property folder 'self) 'path))
(setq folder nil)
)
(vlax-release-object shell)
folder
)
(setq folderpath (FolderBox "Select Folder" "D:/" 0))
(if (setq filelist (vl-directory-files (strcat folderpath "/") "*.dwg" 1))
(foreach dwgfile filelist
(setq
f (open
(strcat folderpath
"/"
(vl-string-subst ".CSV" ".DWG" (strcase dwgfile))
)
"w"
)
) ;create csv file in same folder with replaceing .dwg to .csv
(if (setq doc (LM:GetDocumentObject (strcat folderpath "/" dwgfile)))
(progn
(vlax-for lyt (vla-get-layouts doc)
(vlax-for obj (vla-get-block lyt)
(if
(or
(= "AcDbMText" (vla-get-objectname obj));select onlly m_text and text
(= "AcDbText" (vla-get-objectname obj))
)
(progn
(setq textstring
(vla-get-TextString obj)
)
(if
(or (= (vl-string-search "ETCH" textstring) 0) ;your test condition
(= (vl-string-search "MARK" textstring) 0)
(= (vl-string-search "STAMP" textstring) 0)
)
(write-line textstring f)
)
)
)
)
)
(vlax-release-object doc)
)
)
(close f)
)
)
);close defun
Hope this helps

Prevent Emacs from modifying the OS X clipboard?

How can I prevent Emacs from ever modifying the OS X clipboard unless I explicitly ask it to?
I've tried all of:
(setq x-select-enable-clipboard nil)
(setq interprogram-cut-function nil)
(setq x-select-enable-primary nil)
(setq mouse-drag-copy-region nil)
Which does prevent kill/yank from modifying the clipboard, but selected text is still put on the clipboard.
This is GNU Emacs.app on OS X.
What else should I try?
After doing some digging into the same issue, I believe that the issue actually lies in the Emacs x-select-text function, which explicitly ignores the value of x-select-enable-clipboard on NextStep (and OS X is a NextStep).
I've "solved" this problem by replacing x-select-text with a no-op function, then explicitly using ns-{get,set}pasteboard for interprogram{cut,paste}-function:
; Override the default x-select-text function because it doesn't
; respect x-select-enable-clipboard on OS X.
(defun x-select-text (text))
(setq x-select-enable-clipboard nil)
(setq x-select-enable-primary nil)
(setq mouse-drag-copy-region nil)
(setq interprogram-cut-function 'ns-set-pasteboard)
(setq interprogram-paste-function 'ns-get-pasteboard)
Here is the original x-select-text code:
(defun x-select-text (text)
"Select TEXT, a string, according to the window system.
On X, if `x-select-enable-clipboard' is non-nil, copy TEXT to the
clipboard. If `x-select-enable-primary' is non-nil, put TEXT in
the primary selection.
On MS-Windows, make TEXT the current selection. If
`x-select-enable-clipboard' is non-nil, copy the text to the
clipboard as well.
On Nextstep, put TEXT in the pasteboard (`x-select-enable-clipboard'
is not used)."
(cond ((eq (framep (selected-frame)) 'w32)
(if x-select-enable-clipboard
(w32-set-clipboard-data text))
(setq x-last-selected-text text))
((featurep 'ns) ; This is OS X
;; Don't send the pasteboard too much text.
;; It becomes slow, and if really big it causes errors.
(ns-set-pasteboard text)
(setq ns-last-selected-text text))
(t
;; With multi-tty, this function may be called from a tty frame.
(when (eq (framep (selected-frame)) 'x)
(when x-select-enable-primary
(x-set-selection 'PRIMARY text)
(setq x-last-selected-text-primary text))
(when x-select-enable-clipboard
(x-set-selection 'CLIPBOARD text)
(setq x-last-selected-text-clipboard text))))))

Can I read the Windows Registry from within elisp? How?

I just wanna do something like this
(defun my-fun (reg-path)
"reads the value from the given Windows registry path."
...??...
)
is there a built-in fn that does this?
or is there a command-line tool builtin to windows that I can run to retrieve a reg value?
The way I am imagining doing it, is to run a .js file in cscript.exe that does the work.
ANSWER
(defun my-reg-read (regpath)
"read a path in the Windows registry. This probably works for string
values only. If the path does not exist, it returns nil. "
(let ((reg.exe (concat (getenv "windir") "\\system32\\reg.exe"))
tokens last-token)
(setq reg-value (shell-command-to-string (concat reg.exe " query " regpath))
tokens (split-string reg-value nil t)
last-token (nth (1- (length tokens)) tokens))
(and (not (string= last-token "value.")) last-token)))
==> Thank you to Oleg.
Use reg command line utility.
Emacs command
(shell-command "REG QUERY KeyName" &optional OUTPUT-BUFFER ERROR-BUFFER)
allows you to run a shell command. The output is sent to the OUTPUT-BUFFER.
Here's what I did:
(defun my-reg-read (regpath)
"read a path in the Windows registry"
(let ((temp-f (make-temp-file "regread_" nil ".js"))
(js-code "var WSHShell, value, regpath = '';try{ if (WScript.Arguments.length > 0){regpath = WScript.Arguments(0); WSHShell = WScript.CreateObject('WScript.Shell'); value = WSHShell.RegRead(regpath); WScript.Echo(value); }}catch (e1){ WScript.Echo('error reading registry: ' + e1);}")
reg-value)
(with-temp-file temp-f (insert js-code))
(setq reg-value (shell-command-to-string (concat temp-f " " regpath)))
(delete-file temp-f)
reg-value ))
The elisp function creates a temporary file, then writes a bit of javascript logic into it. the javascript reads the windows registry for a given path. The elisp fn then runs the temporary file, passing the registry path to read. It deletes the file, then returns the result of running it.

Resources