-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a way to save the state in a file #4
Comments
You can write such function in MRI macro like the following. __all__ = [
"store_state",
]
import traceback
import uno
from mytools_Mri.unovalues import TypeClass, TypeClassGroups
def select_files(mri):
def create(name):
return mri.create_service(name, nocode=True)
fp = create("com.sun.star.ui.dialogs.OfficeFilePicker")
fp.initialize((1,))
fp.appendFilter('All Files (*.*)', '*.*')
fp.appendFilter('Text Files (*.txt)', '*.txt')
fp.setCurrentFilter('All Files (*.*)')
ret = None
if fp.execute() == 1:
ret = fp.getFiles()[0]
fp.dispose()
return ret
def store_state(mri):
""" Store current
Store property values of the current object into file. """
file_url = select_files(mri)
if file_url is None:
return
file_path = uno.fileUrlToSystemPath(file_url)
NUMERIC = TypeClassGroups.NUMERIC
STRING = TypeClassGroups.STR
BOOLEAN = TypeClass.BOOLEAN
entry = mri.current
target = entry.target
with open(file_path, "w") as f:
imple_name = mri.engine.get_imple_name(entry)
f.write("IMPLE_NAME: {}\n".format(imple_name))
props = mri.engine.get_properties_info(entry)
for prop in sorted(props):
name = prop[0]
value = prop[2]
type = prop[1]
type_class = prop[6]
if type_class in NUMERIC or type_class == BOOLEAN:
f.write("{}\t{}\t{}\n".format(name, type, value))
elif type_class == STRING:
try:
_value = getattr(target, name)
except:
_value = value
if _value is None:
_value = "None"
else:
_value = _value.replace("\t", "\\t").replace("\n", "\\n").replace("\r", "\\r")
f.write("{}\t{}\t{}\n".format(name, type, _value.encode("utf-8")))
else:
f.write("{}\t{}\t{}\n".format(name, type, " ")) |
Okay, sorry for the long delay. I just tried to run it, and I'm getting an error:
Sorry, I don't know if there's some trick that should be used to execute the code — I'm just trying it from the «Tools → Macros → Run Macro…» |
It seems I've forgot to mention about how to execute the macro. It have to be executed through MRI. |
Ah, sorry, I was wondering btw why doesn't I see the option you mentioned in the first comment ☺ So, it meant to be in MRI. Okay, I just tried it. It was a bit tricky — MRI for some reason doesn't save the directory path, and when I tried to create the directory that is by default written, MRI just ignores it. But I found that I could save the macro to the
And now everything works :з However, it seems that the macro saves a small part of the state. I made a test document, saved the state, next applied some transformations — changed a style of the first paragraph, changed the first page style, made a direct formatting to the first paragraph… But the only moment when the state changed was when I tried to add more text — that was just a count of characters and words. |
A feature request: it would be really nice to see a way to save the state to a file.
UNO API most often very hard to find something, also e.g PyUNO suffer of an awful architecture. So it would be a really great debugging facility if there was a way to save a state in a text file — this would allow to compare properties before and after some change in Office using a simple diff utility, and thus figure out for how to do something.
The text was updated successfully, but these errors were encountered: