Fidelojo Posted April 30 Posted April 30 Hi, Is there a way to get current script name or filepath? I.e. if I have SomeScript.lsp, is there some built-in function that gives me that script filepath when used inside a script? Something like __file__ in Python? Quote
pkenewell Posted April 30 Posted April 30 (edited) @Fidelojo I don't know how you would get the "current script name". If the Lisp file you refer to is in your support paths, would the following work? (findfile "somescript.lsp"). Edited April 30 by pkenewell 1 Quote
GLAVCVS Posted April 30 Posted April 30 Hi This is an interesting point: how do you make the file able to identify itself and where it's located? Why? For example: if you want to create a small *.lsp that works as an installer for other files (lsp, dcl, imgs, etc.) that accompany it in the same directory or in other subdirectories included within it... how do you do it? One way would be to copy the files to a known directory, but the most reliable way would be to make the file able to locate itself, regardless of where it's located. To this day, I still haven't found a good solution for this. 1 Quote
Fidelojo Posted April 30 Author Posted April 30 (edited) @pkenewell method you mentioned would work but that means I would need to add path manually to "supported file paths", but I want it to be more automatic, if that makes sense. Idea is to have all Lisp files in a folder with multiple lisp files, then the folder is deployed on a users system, user loads that one Lisp which every time loads all others inside a folder no matter if some new are added or some are deleted, and also that main lisp has all functions need for others so no need to copy functions in multiple scripts if function needs to be reused, and also can reload them if needed and provide info about them all. But for all that it would be neat for main script to find itself on a system. Edited April 30 by Fidelojo Quote
LanloyLisp Posted April 30 Posted April 30 Hi Fidelojo, here's an option to automatically add it to a supported file path. (setq f (vla-get-files (vla-get-preferences (vlax-get-acad-object))) s (vla-get-supportpath f) ) (vlax-put f 'supportpath (strcat s ";" "<desired folder location>")); edit folder location here 1 Quote
Steven P Posted April 30 Posted April 30 As far as I can tell, you can't identify the file containing the LISP directly. LISP is loaded into CAD and it is used from memory rather than CAD referring to the .lsp file, you could copy and paste the LISP into the command line and it will work just the same for example, likewise load from a file and modify or delete the .lsp file, what was loaded will still run OK. If the .lsp file is saved in a known location you can search these locations and read the files till you find the LISP function you want, and from that you can return the location and file name it contains. For example the Trusted locations are known, so you can search through them. For myself I have a small file, "Location.lsp" that contains the locations for my .lsp files, manually updated one file, all the others refer to this as necessary and this file is in the start-up suit. You can get a LISP to append to this file if you want, even so far as delete the file (you know it's location) and recreating it which can be handy if you have an install LISP: have locations.lps saved in trusted locations, get the user to specify a folder to 'install' .lsp files into, and then append to locations.lsp this new location 1 1 Quote
SLW210 Posted April 30 Posted April 30 If I understand your requirement... You could use this LISP and load it with acaddoc.lsp or your preferred method. Just put the relevant path to the folder with the LISPs to be loaded. ;;; Load all LISPs in folder ;;; ;;; By SLW210 (a.k.a. Steve Wilson) ;;; (defun c:LoadAll ( / mylispfolder mylispfiles acount) (setq mylispfolder "C:\\Path\\To\\Your\\LISP\\Folder\\") ; Change this to the folder path (setq mylispfiles (vl-directory-files mylispfolder "*.lsp" nil)) (foreach file mylispfiles (load (strcat mylispfolder file)) ) (princ) ) Maybe something in here ---> Retrieve list of applications with path? - AutoLISP, Visual LISP & DCL - AutoCAD Forums I prefer the bundle method or an exe for deployments to other users, as mentioned by BIGAL in that thread. There is a recent thread about making a deployment... I found it... 1 Quote
SLW210 Posted April 30 Posted April 30 I actually use this LISP more now, needs Express Tools. ;;; Load all LISPs in selected folder ;;; ;;; By SLW210 (a.k.a. Steve Wilson) ;;; (defun c:LoadLISP ( / mylispfolder mylispfiles) (setq mylispfolder (acet-ui-pickdir)) ; Select a folder (if (and mylispfolder (vl-file-directory-p mylispfolder)) ; Check if a valid (progn (setq mylispfiles (vl-directory-files mylispfolder "*.lsp" nil)) (if mylispfiles (progn (foreach file mylispfiles (setq fullpath (strcat mylispfolder "\\" file)) ; The path (princ (strcat "\nLoading: " fullpath)) ; Print the path (load fullpath) ; Load LISP file(s) ) (princ (strcat "\nLoaded " (itoa (length mylispfiles)) " LISP files.")) ) (princ "\nNo LISP files found in the selected folder.") ) ) (princ "\nInvalid folder selected.") ) (princ) ) May not be what the OP wants. 1 Quote
Saxlle Posted April 30 Posted April 30 I think the @GLAVCVS, @Steven P and @SLW210 gave you a good point what you need to do. 1 Quote
GLAVCVS Posted April 30 Posted April 30 (edited) I think the million-dollar answer is to answer this question: How do I get the location, from the code itself, so I can perform other operations, of a file uploaded with 'appload'? Edited April 30 by GLAVCVS Quote
Fidelojo Posted April 30 Author Posted April 30 (edited) @LanloyLisp your solution can be implemented but it doesn't exclude need to hardcode some information into the script, and I would like to be able to detect scripts file path no matter of the users file system or position of the script in the system, or at least get the file name whose file path then I can track using dos_lisplist but the problem with this approach is that I can't count on that that all systems will have DOSlib installed (but that is solvable which makes this approach acceptable). Anyway thanks for suggestion. @Steven P thanks for advice on searching Trusted folders, I'll try to go with that, if that don't work then I'll probably go and make my own Locations.lsp/MainScript.lsp or something like that. @SLW210 nice solutions, but since it still has some hardcoded parts I'll probably go with Lisp name + dos_listlisp command if I end up doing that, but thanks anyway. @GLAVCVS answer is using (dos_lisplist T) which returns list of full paths of all loaded lisp files in the drawing, but DOSlib needs to be installed on a system for that. Then if all your lisps are in the same folder you can filter them by a common base path. Edited April 30 by Fidelojo Quote
GLAVCVS Posted April 30 Posted April 30 DosLib is a good solution. Although I suppose we were all hoping for something self-sufficient, within the VLisp ecosystem. I'm convinced such a solution exists. Perhaps we haven't looked hard enough. Quote
Saxlle Posted April 30 Posted April 30 Maybe somewhere in registry can be holded data about that (for e.g. HKEY_CURRENT_USER\Software\Autodesk\AutoCAD\<Rxx.x>\ACAD-xxxx:xxx\Profiles\<CurrentProfile>\Dialogs\Appload) 1 Quote
Steven P Posted April 30 Posted April 30 Trusted locations are OK, I think you can get them easy enough. I tend to save mine away on the shared drive - upgrade CAD, machine breaks, or whatever and I don't loose them - and I don't think you can get that location from the LISP that is running, you need some sort of hard coding in there. Hard coding might be constructed within the code (example (strcat "c:\Users" UserName "\CAD\LISPs\") ) but still coded in there 1 Quote
BIGAL Posted May 1 Posted May 1 (edited) I have posted this answer many times as a simple solution it uses a zip file with all your programs, icons, dcls, txt, csv files etc. An Install lisp is used it makes the correct directory for all the files, it unzips all the files and sub directories if required to the correct directory, edits the support paths adding this directory. It uses menuload and loads custom menu's. It is then ready for the user. To use it for multiple users put it on a usb, save on a server or email the 2 files Install.lsp & the Zip, you can drag and drop the lisp use appload and so on. the lisp will ask where is the zip file, if emailed may be in "Downloads" the rest will just happen. We used this method all the time 8 users in my area plus lots of others in company, when we did upgrades. Any way here is an example where all the code is needed to be on a PC, much simpler for a server install. (defun xxxinstall ( / *files* filename newpath oldtrust paths menu_path menu_name Group_name) (vl-load-com) (vl-mkdir "c:\\CAD-TOOLS") (setq filename (getfiled "Select the File \"CAD-TOOLS-SEP-2024\"" "" "ZIP" 16)) ; unzip (startapp (strcat "powershell -command Expand-Archive -Path '" filename "' -DestinationPath 'C:/CAD-TOOLS' -FORCE")) (alert "programs unzipped to C:/CAD-TOOLS") (setq *files* (vla-get-files (vla-get-preferences (vlax-get-Acad-object)))) (setq paths (vla-get-SupportPath *files*)) (vla-put-SupportPath *files* (strcat "C:\\CAD-TOOLS\\SLIDES;C:\\CAD-TOOLS\\IMAGES;C:\\CAD-TOOLS;" paths)) (vla-put-PrinterStyleSheetPath *files* (strcat (vla-get-PrinterStyleSheetPath *files*) ";C:\\CAD-TOOLS")) (if (> (vl-string-search "BricsCAD" (getvar 'acadver)) 0) (princ "Bricscad not found") (progn (setq oldtrust (getvar 'trustedpaths)) (if (wcmatch oldtrust "*CAD-TOOLS*") (princ) (setvar 'trustedpaths (strcat oldtrust ";" "c:\\CAD-TOOLS")) ) ) ) (command "workspace" "save" "" "y") (alert "\nSupport and trusted paths added") (setq Menu_Path "C:\\CAD-TOOLS\\") ; Path to Menu file (setq Menu_Name "CAD-TOOLS") ; pop menu to load (setq Group_Name "CADTOOLSMENU") ; groupname assigned in .mnu or .mns file (if (menugroup menu_Name) (command "_MENUUNLOAD" menu_Name) ) (setq cnt (vla-get-count(vla-get-menuGroups (vlax-get-acad-object)))) (if (> (vl-string-search "BricsCAD" (getvar 'acadver)) 0) (progn (command "MENULOAD" (strcat Menu_Path Menu_Name ".cui")) (menucmd (strcat "P" (rtos (+ cnt 1) 2 0) "=+" menu_Name ".POP1")) ) (progn (command "MENULOAD" (strcat Menu_Path Menu_Name ".cuix")) (menucmd (strcat "P" (rtos (+ cnt 1) 2 0) "=+" menu_Name ".POP1")) ) ) (alert "\n Menu loaded \n \n programs now installed") ) (xxxinstall) Extras (setq *files* (vla-get-files (vla-get-preferences (vlax-get-Acad-object)))) ; savepath ;(vla-get-AutoSavepath *files*) (vla-put-AutoSavepath *files* "C:\\AcadTemp") ; custom icons ;(vla-get-CustomIconPath *files*)) (vla-put-CustomIconPath *files* "L:\\Autodesk\\ICONS") ; custom menu ;(vla-get-Menufile *files*)) ;(vla-put-Menufile *files* "C:\\Users\\2013xxx") ; printers config ;(vla-get-PrinterConfigPath *files*) (vla-put-PrinterConfigPath *files* "L:\\AutoDESK\\Plotting\\Plot Styles 2011") ; printers style sheet ;(vla-get-PrinterStyleSheetPath *files*) (vla-put-PrinterStyleSheetPath *files* "L:\\AutoDESK\\Plotting\\Plot Styles") ; printer drv's ;(vla-get-PrinterDescPath *files*) (vla-put-PrinterDescPath *files* "L:\\AutoDESK\\Plotting\\Drv") ; print spooler ;(vla-get-PrintSpoolerPath *files*) (vla-put-PrintSpoolerPath *files* "C:\\AcadTemp\\") ; template path ;(vla-get-TemplateDwgPath *files*) (vla-put-TemplateDwgPath *files* "L:\\Autodesk\\c3d Templates") ; template location ;(vla-get-QnewTemplateFile *files*) (vla-put-QnewTemplateFile *files* "L:\\Autodesk\\c3d Templates\\xxxx-2019.dwt") ;make new support paths exist + new (setq paths (vla-get-SupportPath *files*)) (setq XXXXpaths "L:\\autodesk\\supportfiles;L:\\autodesk\\lisp;L:\\autodesk\\fonts;") (setq newpath (strcat XXXXpaths paths)) (vla-put-SupportPath *files* newpath) ; Tempdirectory ;(vla-get-TempFilePath *files*)) (vla-put-TempFilePath *files* "C:\\AcadTemp\\") ; PlotLogFilePath = "C:\\Documents and Settings\\YYYY.XXXX-AD\\localsettings\\application data\\autodesk\\c3d 2011\\enu\\" (vla-put-PlotLogFilePath *files* "C:\\AcadTemp\\") ; xref temp path ;(vla-get-TempXrefPath *files*)) (vla-put-TempXrefPath *files* "C:\\AcadTemp\\") ; end use of *files* (setq oldtrust (getvar 'trustedpaths)) (setq newtrust (strcat oldtrust ";" "L:\\Autodesk...")) (setvar 'trustedpaths newtrust) (princ "All Done") ) ; defun (setpaths) (alert "Run user script for toolbars") We also had custom lisps loaded on start up by using Appload and saving in the Startup suite, the details for which are saved in the registry so I guess could be added to the Install.lsp. If you have questions ask. Edited May 1 by BIGAL Quote
Fidelojo Posted May 1 Author Posted May 1 14 hours ago, Saxlle said: Maybe somewhere in registry can be holded data about that (for e.g. HKEY_CURRENT_USER\Software\Autodesk\AutoCAD\<Rxx.x>\ACAD-xxxx:xxx\Profiles\<CurrentProfile>\Dialogs\Appload) That's also good idea, I'll look into it. 8 hours ago, BIGAL said: We used this method all the time 8 users in my area plus lots of others in company, when we did upgrades. Any way here is an example where all the code is needed to be on a PC, much simpler for a server install. Nice scripts, definitely seams easy and efficient solution for deploying on new machines and servers also, thanks for sharing the code 8 hours ago, BIGAL said: We also had custom lisps loaded on start up by using Appload and saving in the Startup suite, the details for which are saved in the registry so I guess could be added to the Install.lsp. If you have questions ask. It would be awesome if you could share some code on that just to see where such info could be found. So when .lsp file is loaded into the CAD software the software is making its own copy of a code that user than can run at will or unload/delete it from software memory, in that case the only way of getting the path of a script is when the software is loading the script because then the software must be reading the script path and storing it somewhere, so if I think right, the only way of retrieving data about the path info is either to intercept it somehow while loading (which seams like not so easy task) or either accessing the location where such data is saved (the location which the dos_lisplist command is accessing) and getting the last created entry/log/data. At least that's how I see it (probably wrong) Quote
Steven P Posted May 1 Posted May 1 Similar to the above, I have this to load all files in a location: You can save your *.lsp files where you want and add this to each users startup suit - you can keep control of this file in a shared location if you want to add more locations as required. Note that if this is saved in the same location as 'MyLispFolder' (in the LISP) then don't auto-run this as it is loaded else it loops forever (load, runs, loads itself and runs, loads and runs...). (defun c:appreload ( / MyLispFolder myfiles acount mylistlength Failedtoload) ;;Define LISP files folder. (setq MyLispFolder "C:\\Users\\SP\\CAD\\") (setq myfiles (vl-directory-files mylispfolder "*.lsp" nil)) ;;Get *.lsp file list (setq mylistlength (length myfiles)) ;;count of number of lsp files (setq acount -1) (repeat mylistlength ;;Loop each file (setq acount (1+ acount)) (setq FailedtoLoad (strcat (nth acount myfiles) " failed to load")) ;; Error message (load (strcat mylispfolder (nth acount myfiles)) FailedtoLoad) ;;Load *.lsp file ) (princ "\n") (princ mylistlength) (princ " lsp files loaded from ") (princ mylispfolder) (princ) ) There is also this option, a bit more work to set up and add to startup suit, loads the LISP on demand, however if you have a lot of (large) LISPs it can make CAD start-up quicker (defun c:0,0 ( / ) ;; duplicate of a LISP name (load "C:\\Users\\SP\\OneDrive\\Desktop\\AutoCAD\\AutoCAD LISPS\\SIMPLELISP.lsp") ;; Load file that the 'real' c:0,0 is saved in (c:0,0) ;; Run 0,0 (princ) ;; exit quietly ) 1 Quote
SLW210 Posted May 1 Posted May 1 After some thinking, if you look at the loaded files in appload or even if you load by drag and drop, the path to the file is displayed. AutoCAD is storing it somewhere. Another thread on the issue... How to get the path of the downloaded lisp file - AutoLISP, Visual LISP & DCL - AutoCAD Forums 1 Quote
BIGAL Posted May 1 Posted May 1 (edited) If you understand support paths OPTIONS FILES, you just need to add the loaction opf where files are saved, you can then use a short version of (load "mylisp22") the mylisp22.lsp can be located anywhere the CAD will look at the support path and use that starting with top path and try to find it. It auto adds the full path to the file name. Re memory problems there is a lisp Function "AUTOLOAD" it allows you to load a lisp when you type the correct command name, it is not loaded till you type the command. An example of a custom lisp I call autoload.lsp. Loaded using Appload save into Start up suite. Way bigger than shown. Note for Acad need to add also to trusted paths, ;The dtr function converts degrees to radians ;The rtd function converts radians to degrees (defun dtr (a) (* pi (/ a 180.0)) ) ; (defun rtd (a) (/ (* a 180.0) pi) ) (setq pi2 (/ pi 2.0)) ; 90 degs up ; pi is 180 degs left (setq pi3 (* pi 1.5)) ; 270 degs down (autoload "COPY0" '("COPY0")) (autoload "COPYCOMMAND" '("ZZZ")) (autoload "COVER" '("COVER")) (autoload "DIMFLIP" '("DIMFLIP")) (autoload "DRAWXFALL" '("DRAWXFALL")) (defun c:vp3 () (command "-vpoint" "1,1,1")) (defun C:ZE () (command "zoom" "E") ) (defun C:15 ()(setvar "osmode" 15359)) ; sets all snaps on (defun C:47 ()(setvar "osmode" 47)(setvar "AUNITS" 0)) (defun C:99 ()(setvar "osmode" 99)) (defun C:8 ()(setvar "osmode" 8)) (defun C:9 ()(setvar "osmode" 9)) (defun C:0 ()(setvar "osmode" 0)) Edited May 1 by BIGAL 1 Quote
Fidelojo Posted May 2 Author Posted May 2 On 5/1/2025 at 12:10 PM, Steven P said: Similar to the above, I have this to load all files in a location: You can save your *.lsp files where you want and add this to each users startup suit - you can keep control of this file in a shared location if you want to add more locations as required. Note that if this is saved in the same location as 'MyLispFolder' (in the LISP) then don't auto-run this as it is loaded else it loops forever (load, runs, loads itself and runs, loads and runs...). I want to achieve something similar but I want to be able to detect path of the lisp that is being loaded and then load all other files on that path. On 5/1/2025 at 12:45 PM, SLW210 said: After some thinking, if you look at the loaded files in appload or even if you load by drag and drop, the path to the file is displayed. AutoCAD is storing it somewhere. Another thread on the issue... How to get the path of the downloaded lisp file - AutoLISP, Visual LISP & DCL - AutoCAD Forums Thanks for pointing me to that thread, it lead me to this one where is written a code to read last location of Appload dialog from registry when loading lisp script, on top of that I found that all loaded lisps are stored into "HKEY_CURRENT_USER\Software\Autodesk\AutoCAD\Rxx.x\ACAD-xxxx:xxx\Profiles\<<Profile_Name>>\Dialogs\Appload\Startup" so if there is only one lisp script in Startup suite which loads all other then its full path with name can be read from that registry location by name "1Startup", the only problem is what if there are more scripts in startup suite, how to filter out the right one? Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.