DM 脚本不支持按需加载包,但有两种不同的方法可以实现您想要的:
使用库包
当您“安装”脚本时,您可以选择将其安装为菜单命令或作为库。这对于通过菜单命令(存储在全局首选项文件中)或通过脚本命令(可用于
创建 .gtk 文件,然后可以从插件中添加/删除
文件夹)。
“菜单”选项将安装一个脚本,以便通过菜单项调用一次,但不会保留在内存中。
“库”选项将在启动时执行一次脚本并将脚本本身保持在范围内。因此,您可以在库文件中定义方法(或类)并使其普遍可用。如果您需要一些启动操作,您可以将一些可执行代码放入库中。
使用脚本库作为 .gtk 插件可能是实现您想要的推荐方式。但是,它们总是被加载。
忠告:如果您制作库,请确保您使用非常唯一的类和方法名称以避免任何冲突。我建议使用一些库名称预先修复所有类/方法名称,即使用 MyLib_MyClass 而不是 MyClass 等。
澄清:作为库包添加的脚本永久添加到软件中,即这些包被创建一次 然后放在插件文件夹中。它们将始终在 DM 启动时加载并可用。库包方法不适合临时“加载”外部脚本。 DM 脚本不支持这种“按需导入”。
创建包的脚本命令是一种实用命令,可帮助人们以一种简单且易于管理的方式创建包。通常,人们会创建一个“创建包 XY”脚本,其中包含几个这样的命令,将某个位置的所有脚本添加到包中。该脚本将被调用一次以创建包文件(之后它已经在插件文件夹中。)
仅当包含的脚本发生更改并因此需要更新包时,才会再次调用 create-package 脚本。请注意,在这种情况下,首先需要从插件文件夹中删除包文件并在不加载它的情况下启动 DigitalMicrograph,以便创建一个 new 包。否则脚本会追加到包中,如果包中已经存在同名的方法,这是不可能的。
F1 帮助文档有一个示例脚本:
典型示例,使用 GMS 3.4.0:
脚本存储在:C:\Tmp\testLib.s
void TestCall()
{
Result("\nTest")
}
脚本存储在:C:\Tmp\menuAction.s
Result("\nPerforming an action here.")
一次性运行脚本安装包:
// General package parameters
// *********************************************
string pkNa = "myPkg" // Filename of plugin
number pkLe = 3 // level 3 (.gtk) only needed for load order
string pkLo = "user_plugin" // plugin location
string scriptRoot = "C:\\Temp\\"
// List of Scripts to be installed as menu items
// *********************************************
// Each entry needs a (unique) command-name, a menu-name and an optional sub-menu name.
// The "isLibary" flag is set to 0
// It is possible to add the same script multiple times. The script will be executed when the menu item
// is chosen. Methods and Classes of the script are not available otherwise
// A separator can be added by installing and empty script with a (unique) command name starting with "-"
AddScriptFileToPackage( scriptRoot + "menuAction.s", pkNa, pkLe, pkLo, "Call 1", "MyMenu", "MySubMenu", 0 )
AddScriptFileToPackage( scriptRoot + "menuAction.s", pkNa, pkLe, pkLo, "Call 2", "MyMenu", "", 0 )
AddScriptToPackage( "", pkNa, pkLe, pkLo, "-sep1", "MyMenu", "", 0 )
AddScriptFileToPackage( scriptRoot + "menuAction.s", pkNa, pkLe, pkLo, "Call 3", "MyMenu", "", 0 )
// List of Scripts to be installed as library
// *********************************************
// Each entry needs a (unique) command-name. Menu-name and sub-menu name are "".
// The "isLibary" flag is set to 1
// The script will be executed once on startup (if there is executable code). It is also executed once
// here during the install.
// Methods and Classes of the script are permanently available and need unique names.
// Adding a script to the package as libary can be used to create on-load-version info output.
AddScriptFileToPackage( scriptRoot + "testLib.s", pkNa, pkLe, pkLo, "library-1", "", "", 1 )
AddScriptToPackage( "Result(\"Script packages myPkg loaded.\\n\")", pkNa, pkLe, pkLo, "myPkg-versionInfo", "", "", 1 )
运行安装脚本后会出现:
- 这样的菜单:
- 在结果窗口中输出如下:
- 文件夹
C:\Users\USERNAME\AppData\Local\Gatan\Plugins\myPkg.gtk中的一个包文件
- 脚本命令
TestCall()在所有脚本中普遍可用。
只要 .gtk 文件保留在 plugins 文件夹中,每次 DM 启动时都会加载该包。
从脚本中调用脚本代码
脚本语言支持从脚本中调用脚本的两个命令:
使用命令从光盘执行脚本可以做你想做的事,但以这种方式维护一个有用的“库”可能很乏味。它也不允许您安装类。
从脚本中调用脚本的示例:
// Direct example
void Demo()
{
ClearResults()
Result( "I am a test call.\n")
number n = 5
Result( "I am working on the number: " + n )
}
Demo()
//Having the script as a string
number otherNumber = 11 // To show how you can modify a script call as an example
string scriptStr
scriptStr += "void Demo()\n{" + "\n"
scriptStr += "ClearResults()" + "\n"
scriptStr += "Result( \"I am a test call.\\n\")" + "\n"
scriptStr += "number n = " + otherNumber + "\n"
scriptStr += "Result( \"I am working on the number: \" + n )"+ "\n"
scriptStr += "}\n"
scriptStr += "Demo()\n"
If ( TwoButtonDialog("Script-call","Show it", "Run it") )
{
ClearResults()
Result( scriptStr )
}
else
ExecuteScriptString( scriptStr )