Open an .ms file with Edit()


#1

Could someone please help with this idea: I would like to open a scriptfile (edit("file.ms")) that is linked to with eg $userScript instead of a full path in clear text.

In this reply (Can MXS add custom menus to eg Maxscript editor?), @Serejah provided an answer where you could extract the selected string inside the Maxscript Editor.

I went through the scripts I have and gathered an amount of variations of how the script-path could look like (you might know some more variations I have missed):

$userScripts\Folder\scriptfile.ms

  • to open this one, we would need to run edit" … put selected string in here … "

$userScripts/Folder/scriptfile.ms

  • edit" … put selected string in here … "

(getdir #userScripts)+"\Folder\scriptfile.ms")

  • edit( … put selected string in here … )

ScriptPath + “Folder\scriptfile.ms”`

  • in this case, “ScriptPath” is defined globally , like this: Global ScriptPath = “…something…”
  • edit( … put selected string in here … )

#2

define a function to convert to real path


#3

Could you help providing this function? :+1:


#4
fn getrealpath string =
(
	pathsel = #(#userScripts)
	subpathsel = #("/Folder/")
	realpath = ""
	for i in pathsel do
	(
			i = getdir i
			for j in subpathsel do
				if doesFileExist (i+j+string) do 
					realpath = i+j+string
	)
	realpath
)

edit(getrealpath "your file string")


#5

Maybe I am wrong, but aren’t you complicating things? Probably I was a bit unclear too.

if we have $userScripts\Folder\scriptfile.ms we would need edit"$userScripts\Folder\scriptfile.ms"

if we have $userScripts/Folder/scriptfile.ms --> edit"$userScripts/Folder/scriptfile.ms"

(getdir #userScripts)+"\Folder\scriptfile.ms") --> edit((getdir #userScripts)+"\Folder\scriptfile.ms"))

ScriptPath + “Folder\scriptfile.ms” --> edit(ScriptPath + “Folder\scriptfile.ms”) … as mentioned before “ScriptPath” is defined globally , like this: Global ScriptPath = “…something…”

The task here is to create a macroscript that reads what you have selected in the Maxscript Editor och opens the file. Much like the “Open Selected Filename” in Maxscript Editor. That one works only on path in plain text.


#6

edit (getFilenamePath "your file path" + filenameFromPath "your file path" )


#7

I am afraid you are misunderstanding my question :slight_smile:

First, take a look at this post: Can MXS add custom menus to eg Maxscript editor?

There, you will see a solution that captures your selected text inside the Macroscript Editor.
For example, if I do this:

… and run the script provided in the link above, I get this output: “$scripts/SoulburnScripts/scripts/snapShoter.ms”

Now that we have come so far, I would want to package this into a single Macroscript: You select something, you run the macroscript, it opens up the file for you.


#8

you had got what you selected , just need chenged it to the string , so why problem yet


#9

If something doesn’t work check if you have all slashes in place

ScriptPath = "C:\Users\User\AppData\Local\Autodesk\3dsMax\2014 - 64bit\ENU\scripts"
edit ("$userscripts\outliner\update.ms") -- works
edit (ScriptPath + "\outliner\update.ms") -- works
edit (ScriptPath + "outliner\update.ms") -- missing slash, doesn't work
edit (pathConfig.appendPath ScriptPath "outliner\update.ms") -- works again

#10

yes, of course. If we know the script works, then all that counts is the end-string, right? Even if there is a best practice among scripters (if there is one, please tell me) I cant do anything about it, because what I am looking for is a way to open ms-files defined in other peoples scripts. I can’t controll how everyone writes their code, but I can try to make a script that works with theirs :smiley:

This is what I ended up with so far, but it is not compatible with all kinds of cases (se below).
Also, please help me fine tune as much as you can.

(

fn GetMXSEditorWindowText hwnd =
(
local marshal = dotnetclass “System.Runtime.InteropServices.Marshal”
str = “”
try (

  len = windows.sendmessage hwnd 0xE 0 0
  lParam = marshal.AllocHGlobal (marshal.SystemDefaultCharSize*(len+1))
  windows.sendmessage hwnd 0xD (len+1) lParam 

  ptr = dotnetobject "System.IntPtr" lParam
  str = marshal.PtrToStringAuto ptr
  marshal.FreeHGlobal ptr

) catch ()
str
)

g = (dotNetClass “Autodesk.Max.GlobalInterface”).Instance

hwnd = g.TheMxsEditorInterface.EditorGetEditHWND
main = g.TheMxsEditorInterface.EditorGetMainHWND

SCI_GETSELECTIONSTART = 2143
SCI_GETSELECTIONEND = 2145

start = windows.sendMessage hwnd SCI_GETSELECTIONSTART 0 0
end = windows.sendMessage hwnd SCI_GETSELECTIONEND 0 0

edit (substring (GetMXSEditorWindowText hwnd) (start+1) (end - start))

)

summary - please keep in mind that they are all working scripts. I am trying to apply this macroscript on “real life scripts”, that I download from developers websites or scriptspot and so on. I need this to work without the need to change the original script to make them work with mine:

$userScripts/subfolder/file.ms -- works
$userScripts\\folder\\file.ms -- does not work. Typing manually edit"$userScripts\\folder\\file.ms" works though
ScriptPath + "Folder\file.ms" -- does not work. Typing manually edit (ScriptPath + "Folder\file.ms) works though
((getdir #userScripts)+"\\folder\\file.ms") -- does not work. Typing manually edit((getdir #userScripts)+"\\folder\\file.ms") works though

There might be more cases and we need them all to work :wink:

Regarding the ones that don’t work, my guess it is because of all the nested slashes and parenthesis and all.
How can this be solved?


#11

no-worked line because of wrong syntax
if you get right path , use getFilenamePath + filenameFromPath all syntax works , just need to case parametric with execute method


#12

+1

try this

substr = execute (substring (GetMXSEditorWindowText hwnd) (start+1) (end - start))
edit substr

#13

Thank you very much! It works now :sunglasses:

Is it possible to foolproof this script? If I miss the first and/or last " for example, I get an error.
Another thing was if I got one ( or ) too many, then I get errors too.

How can we get a nicer warning, eg explaining the proper use of this script? Or simply not do anything? Or if possible, make it intelligent and have the script add a first/last " for us? What do you think?


#14

You can trim not needed characters

trimLeft “apple” “a”
trimRight “apple” “e”
trimRight (trimLeft “apple” “a”) “e”

“pple”
“appl”
“ppl”

I doubt that you’ll find a ready solution to your problem.
As long as you depend on any local variable in someone’s script it is unlikely that you will be able to make a proper path.


#15

Maybe :wink: A final try then: is there a way in maxscript to verify if a string is a valid path? With a friendly way (as opposed to an aggressive error message like the ones I get now) to tell us true or false?


Can MXS add custom menus to eg Maxscript editor?
#16

Sure, check out doesFileExist and pathConfig struct methods.