Author Topic: To have Select Multiple files Dialog by calling PowerShell  (Read 857 times)

0 Members and 1 Guest are viewing this topic.

kozmos

  • Newt
  • Posts: 114
To have Select Multiple files Dialog by calling PowerShell
« on: November 01, 2023, 10:48:19 AM »
Inspired by BIGAL's method in this post: http://www.theswamp.org/index.php?topic=58636.0, I have done some research on calling PowerShell, and figured out a wrapped fucntion to have "select multiple files" dialog via calling PowerShell.

Code - Auto/Visual Lisp: [Select]
  1.  
  2.  
  3. ;; VLDCL-PowerShellCore
  4. ;; Core function to run PowerShell(PS) codes and get return value from internal bridge TXT file
  5. ;; input 1 = dat - PS codes string list
  6. ;; output = content from internal bridge txt file
  7. (Defun VLDCL-PowerShellCore (dat / BAT EXE FNN RTN TXT VLO)
  8.   (or (listp dat) (setq dat (list dat)))
  9.   (setq fnn (strcat
  10.               (vla-item (vlax-get-property
  11.                           (vlax-get-or-create-object "WScript.Shell")
  12.                           "SpecialFolders"
  13.                         )
  14.                         "MyDocuments"
  15.               )
  16.               "\\VLDCL."
  17.             )
  18.         txt (strcat fnn "TXT")
  19.         exe (strcat fnn "PS1")
  20.         bat (strcat fnn "BAT")
  21.         fnn (open exe "w")
  22.   )
  23.   (mapcar (function (lambda (x) (write-line x fnn))) dat)
  24.   (close fnn)
  25.   (setq fnn (close fnn)
  26.         fnn (open bat "w")
  27.   )
  28.     (strcat (substr exe 1 2)
  29.             "\nCD "
  30.             (vl-filename-directory exe)
  31.             "\npowershell .\\VLDCL.PS1"
  32.     )
  33.     fnn
  34.   )
  35.   (close fnn)
  36.   (and (findfile txt) (vl-file-delete (findfile txt)))
  37.   (setq vlo (vlax-create-object "Shell.Application"))
  38.   (vlax-invoke-method vlo "shellexecute" bat "" "" "runas" 0)
  39.   (while (null (findfile txt)))
  40.   (and (findfile txt)
  41.        (setq fnn (open txt "r"))
  42.        (while (setq vlo (read-line fnn))
  43.          (setq rtn (cons vlo rtn))
  44.        )
  45.   )                                     ; Read file content into return list
  46.   (setq fnn (close fnn))
  47.   (foreach abc (list txt bat exe)
  48.     (and (findfile abc) (vl-file-delete (findfile abc)))
  49.   )                                     ; Delete temp
  50.   (reverse rtn)
  51. )
  52.  
  53. ;; VLDCL-PowerShellSelectMultipleFiles
  54. ;; Call PowerShell(PS) dialog to select multiple files.
  55. ;; Note: Some anti-virus program may attempt to stop calling PowerShell PS1 file and BAT file, please allow them to run
  56. ;; input 1 = ttl    - Dialog title text, nil="VLDCL-PowerShellSelectMultipleFiles"
  57. ;; input 2 = dir    - Starting folder to select files, nil=DWGPrefix
  58. ;; input 3 = filter - File type filter, predefined "DWG" "TXT" "DOC", *.* for all files will be added automatically
  59. ;;                    if this is not string or nil, will display help info
  60. ;; output = list of selected files
  61. ;; Sample calling:
  62. ;; (VLDCL-PowerShellSelectMultipleFiles "Select DWG files" nil nil)
  63. ;; (VLDCL-PowerShellSelectMultipleFiles nil nil 5) will display help
  64. (Defun VLDCL-PowerShellSelectMultipleFiles (ttl dir filter / RTN)
  65.   (or ttl (setq ttl "VLDCL-PowerShellSelectMultipleFiles"))
  66.   (or dir (setq dir (getvar "DWGPrefix")))
  67.   (or filter (setq filter "dwg"))
  68.   (cond
  69.     ((= (type filter) 'str)
  70.      (setq filter (cond
  71.                     ((= (strcase filter) "DWG")
  72.                      "Drawing files (*.dwg)|*.dwg"
  73.                     )
  74.                     ((= (strcase filter) "DXF")
  75.                      "Drawing files (*.dxf)|*.dxf"
  76.                     )
  77.                     ((= (strcase filter) "TXT")
  78.                      "Text files (*.txt)|*.txt"
  79.                     )
  80.                     ((= (strcase filter) "DOC")
  81.                      "Microsoft Word files (*.doc;*.docx)|*.doc;*.docx"
  82.                     )
  83.                     ((<= (strlen filter) 4)
  84.                      (strcat filter " files(*." filter ")|*." filter)
  85.                     )
  86.                     ;; Can add more if needed
  87.                     (t filter)
  88.                   )
  89.            rtn    (or (vl-string-search "*.*" filter)
  90.                       (setq filter (strcat filter "|All files (*.*)|*.*"))
  91.                   )
  92.            rtn    (VLDCL-PowerShellCore
  93.                     (list
  94.                       "Add-Type -AssemblyName System.Windows.Forms"
  95.                       "$form = New-Object System.Windows.Forms.OpenFileDialog"
  96.                       (strcat "$form.Title = " (vl-prin1-to-string ttl))
  97.                       (strcat "$form.InitialDirectory = "
  98.                               (vl-prin1-to-string dir)
  99.                       )
  100.                       (strcat "$form.Filter = "
  101.                               (vl-prin1-to-string filter)
  102.                       )
  103.                       "$form.MultiSelect = $true"
  104.                       "$form.ShowHelp = $true"
  105.                       "$form.ShowDialog() > $null"
  106.                       "If($form.FileNames -like \042*\\*\042){$RTN = $form.Filenames}"
  107.                       "else {$RTN = \042.Cancel\042}"
  108.                       "$RTN | Out-File -encoding oem -FilePath .\\VLDCL.TXT"
  109.                     )
  110.                   )
  111.      )
  112.      (if (/= (car rtn) ".Cancel")
  113.        rtn
  114.      )
  115.     )
  116.     (t
  117.      (alert
  118.        (strcat
  119.          "Function Call"
  120.          "\n(VLDCL-PowerShellSelectMultipleFiles Title InitDir FileTypeFilter)\n"
  121.          "\nPlease allow VLDCL.BAT and PowerShell to run if anti-virus program"
  122.          "\nis attempting to stop them from executing\n\n"
  123.          "\nPredefined FileTypeFilter:"
  124.          "\nDWG=Drawing files (*.dwg)|*.dwg"
  125.          "\nTXT=Text files (*.txt)|*.txt"
  126.          "\nDOC=Microsoft Word files (*.doc;*.docx)|*.doc;*.docx\n"
  127.          "\nSample of Format for file type filter:"
  128.          "\nImage files (*.jpg;*.bmp)|*.jpg;*.bmp"
  129.          "\nAdobe PDF files (*.pdf)|*.pdf")
  130.      )
  131.     )
  132.   )
  133. )
  134.  
KozMos Inc.

JohnK

  • Administrator
  • Seagull
  • Posts: 10648
Re: To have Select Multiple files Dialog by calling PowerShell
« Reply #1 on: November 01, 2023, 12:48:35 PM »
Question: does this post need to be moved to the "show your stuff" forum?
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org