Opens a File in Tw for editing.
Will read inside script or document directory, other wise needs File access permissions set, but can ask user on occasion if told to (True/False) in the third version of calling the function
Required - fullFileName a fully/qualified/path to the file contents wanted.
The position held by TW here is actually a scriptApiObj.
Line number position (-1 for none), in the second version of calling the function, allows the line position of the file to be set.
Returns a response object with members:
"status" as for other file open functions Return Values for Status
"result" is the opened Document's scripting object with all the properties and functions of TW.target
Example:
retVal = TW.openFileFromScript("c:/somewhere/Mydocument.txt", TW, -1, true);
if (retVal.status == 0) // SystemAccess_OK
{
var openedDocumentObject = retVal["result"] ;
TW.information(null,"Tw Message",openedDocument.fileName); // shows the opened document's base filename
TW.information(null,"Tw Message",openedDocument.text); // shows the contents of the "opened" document.
}
Edit notes: Extracting from here
//Q_INVOKABLE
QMap<QString, QVariant> TWApp::openFileFromScript(const QString& fileName, QObject * scriptApiObj, const int pos /*= -1*/, const bool askUser /*= false*/)
{
QSETTINGS_OBJECT(settings);
QMap<QString, QVariant> retVal;
QObject * doc = NULL;
TWScript * script;
QFileInfo fi(fileName);
TWScriptAPI * scriptApi = qobject_cast<TWScriptAPI*>(scriptApiObj);
retVal["status"] = TWScriptAPI::SystemAccess_PermissionDenied;
// for absolute paths and full reading permissions, we don't have to care
// about peculiarities of the script; in that case, this even succeeds
// if no valid scriptApi is passed; otherwise, we need to investigate further
if (fi.isRelative() || !settings.value("allowScriptFileReading", false).toBool()) {
if (!scriptApi)
return retVal;
script = qobject_cast<TWScript*>(scriptApi->GetScript());
if (!script)
return retVal; // this should never happen
// relative paths are taken to be relative to the folder containing the
// executing script's file
QDir scriptDir(QFileInfo(script->getFilename()).dir());
QString path = scriptDir.absoluteFilePath(fileName);
if (!script->mayReadFile(path, scriptApi->GetTarget())) {
// Possibly ask user to override the permissions
if (!askUser)
return retVal;
if (QMessageBox::warning(qobject_cast<QWidget*>(scriptApi->GetTarget()),
tr("Permission request"),
tr("The script \"%1\" is trying to open the file \"%2\" without sufficient permissions. Do you want to open the file?")\
.arg(script->getTitle()).arg(path),
QMessageBox::Yes | QMessageBox::No, QMessageBox::No
) != QMessageBox::Yes)
return retVal;
}
}
doc = openFile(fileName, pos);
retVal["result"] = QVariant::fromValue(doc);
retVal["status"] = (doc != NULL ? TWScriptAPI::SystemAccess_OK : TWScriptAPI::SystemAccess_Failed);
return retVal;
}
Created with the Personal Edition of HelpNDoc: Free help authoring tool