Hi,
I can’t find any documentation for the correct syntax for the “scriptDialog.ShowSaveFileDialog” & “scriptDialog.ShowOpenFileDialog” functions…
For example, I want to have a button which when pressed, save’s or loads the various GUI Control values out to an ascii file.
I have this so far…(specifically, what do the “?” symbols do?)
[code]SaveButton = scriptDialog.AddControl( “SaveSettingsButton”, “ButtonControl”, “Save Settings”, 85, -1 )
SaveButton.ValueModified += SaveSettingsButtonPressed
def SaveSettingsButtonPressed(*args):
global scriptDialog
if scriptDialog.ShowSaveFileDialog("",".ini","ini Files (*.ini)|*.ini|All Files (*.*)|*.*",?,?):
SettingsFilename = #How do I grab the file name and path from the ShowSaveFileDialog from above?
writer = File.CreateText( SettingsFilename )
writer.WriteLine( "[Bla Bla...]" )[/code]
Out of curiosity, where did you originally see ShowSaveFileDialog? I don’t see it in our documentation anywhere, which would explain why you can’t find any information on it. This is an internal function, and as far as I know, we’re not using it in any of our scripts. Please correct me if I’m wrong though!
Ah. Its in the command script submission script for loading/saving cmd’s from a text file.
Mike
Heh, I guess we missed that one. We should expose this function a bit better to make it easier to use, but in the meantime, you should be able to make use of this internal function. Here’s how you would use it in your code you posted:
import clr
SaveButton = scriptDialog.AddControl( "SaveSettingsButton", "ButtonControl", "Save Settings", 85, -1 )
SaveButton.ValueModified += SaveSettingsButtonPressed
def SaveSettingsButtonPressed(*args):
global scriptDialog
filterIndex = 0
selectedFile = clr.Reference[str]("")
if scriptDialog.ShowSaveFileDialog("",".ini","ini Files (*.ini)|*.ini|All Files (*.*)|*.*",filterIndex,selectedFile):
SettingsFilename = selectedFile.Value
writer = File.CreateText( SettingsFilename )
writer.WriteLine( "[Bla Bla...]" )
Cheers,
Thanks Ryan.
The *.ini file is created, but it is empty for some reason?
The following code doesn’t seem to be working?
writer.WriteLine( "Test" )
writer.WriteLine( "SubmitSuspendedBox=%s" % scriptDialog.GetValue( "SubmitSuspendedBox" ) )
Thanks,
Mike
Can you post the full SaveSettingsButtonPressed(*args) function? That writer stuff should be working, so it might help to to see the function as a whole.
Thanks!
OK, Here you go…
[code]import clr
SaveButton = scriptDialog.AddControl( “SaveSettingsButton”, “ButtonControl”, “Save Settings”, 85, -1 )
SaveButton.ValueModified += SaveSettingsButtonPressed
def SaveSettingsButtonPressed(*args):
global scriptDialog
filterIndex = 0
selectedFile = clr.Reference[str]("")
if scriptDialog.ShowSaveFileDialog("",".ini",“ini Files (.ini)|.ini|All Files (.)|.”,filterIndex,selectedFile):
SettingsFilename = selectedFile.Value
writer = File.CreateText( SettingsFilename )
writer.WriteLine( "Test" )
writer.WriteLine( "SubmitSuspendedBox=%s" % scriptDialog.GetValue( "SubmitSuspendedBox" ) )[/code]
Thanks,
Mike
I’m wondering if the problem is that you’re not calling writer.Close() when you finish writing to the file. This would leave the file handle open, so the next time you try to write to it, it fails. Try adding this line and then restarting the Monitor to see of the problem persists.
Cheers,
Ryan
Spot on Ryan. That fixed it!
Thanks,
Mike
Sorry, just trying to get the “read” syntax correct, but lacking the documentation!
Something like this should work, but I think I have the ‘reader’ line incorrect:
[code]def LoadSettingsButtonPressed(*args):
global scriptDialog
filterIndex = 0
selectedFile = clr.Reference[str]("")
if scriptDialog.ShowOpenFileDialog("",".ini","ini Files (*.ini)|*.ini|All Files (*.*)|*.*",filterIndex,selectedFile):
SettingsFilename = selectedFile.Value
reader = open(SettingsFilename, "r")
#The next line is wrong…!
reader.ReadLine( “CommentBox=%s” % scriptDialog.SetValue( “CommentBox” ) )[/code]
Thanks,
Mike
Since you’re using the Python file reading functions, some documentation can be found here:
docs.python.org/tutorial/inputoutput.html
Just search the page for the readline() function. Note that you shouldn’t be passing in any arguments to the readline().
If you want to use the .NET classes, you will want to use File.OpenText( “filename” ):
msdn.microsoft.com/en-us/library … ntext.aspx
Then use ReadLine():
msdn.microsoft.com/en-us/library … dline.aspx
Don’t forget to close the file handle when you’re done.
Cheers,
Thanks for the links!
However, I’m still stuck on these couple of lines! Any chance of some more help?! I’m stuck on the “readLine” bit…
I want to be able to look for a particular INI entry such as “TestBox” and then apply its value to the Dialog Control, “TestBox”.
I thought it would work the same way as when you .WriteLine the file out!
[code]def LoadSettingsButtonPressed(*args):
global scriptDialog
filterIndex = 0
selectedFile = clr.Reference[str]("")
if scriptDialog.ShowOpenFileDialog("",".ini","ini Files (*.ini)|*.ini|All Files (*.*)|*.*",filterIndex,selectedFile):
SettingsFilename = selectedFile.Value
reader = File.OpenText( SettingsFilename )
reader.ReadLine( "TestBox=%s" ) scriptDialog.SetValue( "TestBox",% ) )
reader.Close()[/code]
Thanks,
Mike
When you call ReadLine(), you’re just reading in the next line in the file. This function would return a string that contains that line. What you could do is loop through all the lines and parse each line to see if it matches the INI entry you’re looking for.
As an alternative, you could use File.ReadAllLines():
msdn.microsoft.com/en-us/library/s2tte0y1.aspx
This returns a string array that you can loop through looking for the value you want. Saves you the hassle of maintaining the file handle.
Cheers,
OK, nearly there (although I think my approach in the last 2 lines of code could be improved?) …however, my INI keys contain booleans as well as float values. The example below kicks up an error due to the value being a string and not a boolean, ‘True’, when I pass it to the .SetValue command:
[code]def LoadSettingsButtonPressed(*args):
global scriptDialog
filterIndex = 0
selectedFile = clr.Reference[str]("")
if scriptDialog.ShowOpenFileDialog("",".ini","ini Files (*.ini)|*.ini|All Files (*.*)|*.*",filterIndex,selectedFile):
SettingsFilename = selectedFile.Value
File.OpenText( SettingsFilename )
INISettings = File.ReadAllLines( SettingsFilename )
for INISetting in INISettings:
mike = StringUtils.FromCommaSeparatedString( "TestBox=%s" % scriptDialog.GetValue( "TestBox" ) )
scriptDialog.SetValue( "TestBox", mike )
#ReadAllLines automatically closes the file, no need for File.Close()[/code]
Mike
In python, you can cast string values to other types, like:
intNumber = int( “1” )
floatNumber = float( “1.0” )
So just cast whatever value you’re passing to .SetValue() to the correct type.
Cheers,