3 回答

TA貢獻1811條經驗 獲得超5個贊
在您可以從 Python 腳本調用的 DM 腳本中,該命令GetApplicationDirectory()
為您提供所需內容。通常,您會想要使用“open_save”,即
GetApplicationDirectory("open_save",0)
這將返回在使用文件/打開或在新圖像上使用文件/保存時出現的目錄(字符串變量)。但是,它不是用于“文件/保存工作區”或其他保存的內容。
從有關該命令的 F1 幫助文檔中:
請注意,“當前”目錄的概念在 Win10 上與應用程序(包括 GMS)有些沖突。特別是“SetApplicationDirectory”命令并不總是按預期工作......
如果您想找出當前顯示的特定文檔的文件夾(圖像或文本),您可以使用以下 DM 腳本。這里的假設是,窗口是最前面的窗口。
documentwindow win = GetDocumentWindow(0)
if ( win.WindowIsvalid() )
if ( win.WindowIsLinkedToFile() )
Result("\n" + win.WindowGetCurrentFile())

TA貢獻1853條經驗 獲得超6個贊
對于也需要這個(并且只想復制一些代碼)的每個人,我根據@BmyGuests 的回答創建了以下代碼。這會獲取腳本窗口綁定到的文件并將其保存為持久標記。然后從 python 文件中讀取此標記(并刪除標記)。
重要說明:這僅在您按下“執行腳本”按鈕的 python 腳本窗口中有效,并且僅當此文件已保存時!但是從那里開始,您可能正在導入應該提供該module.__file__屬性的腳本。這意味著這不適用于插件/庫。
import DigitalMicrograph as DM
# the name of the tag is used, this is deleted so it shouldn't matter anyway
file_tag_name = "__python__file__"
# the dm-script to execute, double curly brackets are used because of the
# python format function
script = ("\n".join((
"DocumentWindow win = GetDocumentWindow(0);",
"if(win.WindowIsvalid()){{",
"if(win.WindowIsLinkedToFile()){{",
"TagGroup tg = GetPersistentTagGroup();",
"if(!tg.TagGroupDoesTagExist(\"{tag_name}\")){{",
"number index = tg.TagGroupCreateNewLabeledTag(\"{tag_name}\");",
"tg.TagGroupSetIndexedTagAsString(index, win.WindowGetCurrentFile());",
"}}",
"else{{",
"tg.TagGroupSetTagAsString(\"{tag_name}\", win.WindowGetCurrentFile());",
"}}",
"}}",
"}}"
))).format(tag_name=file_tag_name)
# execute the dm script
DM.ExecuteScriptString(script)
# read from the global tags to get the value to the python script
global_tags = DM.GetPersistentTagGroup()
if global_tags.IsValid():
s, __file__ = global_tags.GetTagAsString(file_tag_name);
if s:
# delete the created tag again
DM.ExecuteScriptString(
"GetPersistentTagGroup()." +
"TagGroupDeleteTagWithLabel(\"{}\");".format(file_tag_name)
)
else:
del __file__
try:
__file__
except NameError:
# set a default if the __file__ could not be received
__file__ = ""
print(__file__);
添加回答
舉報