[Solved] Save Window Size and Position per file

Discuss setup / installation issues - Add a spell checker, Language pack?
Post Reply
lonadar
Posts: 1
Joined: Fri Oct 05, 2012 2:11 am

[Solved] Save Window Size and Position per file

Post by lonadar »

I have a number of different spreadsheets of varied orientations, shapes and sizes. Unfortunately, when I open one the window defaults to the size of the last spreadsheet I had open. I want to be able to save the unique window size and position for each spreadsheet individually so when I open them I don't have to go resizing each one for most efficient use.

I've seen a few posts that talk about saving it while in "design" or "edit" mode, but I don't see anything named that and, since the file isn't "Read Only", I don't know what would be different than "edit" mode and whatever I normally work in.

Any help appreciated...I'm slowly converting all of my little AppleWorks spreadsheets I've built over the years into something more Mountain Lion compatible since Numbers was a complete letdown. (Please stop, it's not nice to laugh.)
Last edited by Hagar Delest on Fri Sep 30, 2016 9:22 pm, edited 1 time in total.
Reason: tagged [Solved].
OpenOffice 3.4.1 for Mac OS X 10.6
User avatar
Charlie Young
Volunteer
Posts: 1559
Joined: Fri May 14, 2010 1:07 am

Re: Save Window Size and Position per file

Post by Charlie Young »

lonadar wrote:I have a number of different spreadsheets of varied orientations, shapes and sizes. Unfortunately, when I open one the window defaults to the size of the last spreadsheet I had open. I want to be able to save the unique window size and position for each spreadsheet individually so when I open them I don't have to go resizing each one for most efficient use.

I've seen a few posts that talk about saving it while in "design" or "edit" mode, but I don't see anything named that and, since the file isn't "Read Only", I don't know what would be different than "edit" mode and whatever I normally work in.

Any help appreciated...I'm slowly converting all of my little AppleWorks spreadsheets I've built over the years into something more Mountain Lion compatible since Numbers was a complete letdown. (Please stop, it's not nice to laugh.)
See Initial opening of a spreadsheet in Calc

I think though, that I added some routines to the Python macros, and never got around to posting them, so I have attached the latest version (zipped). It includes now a routine called setEvents and one called clearEvents. Run setEvents on a file for which you wish to save the Size and Position, a clearEvents to remove them.

Here is the full code:

Code: Select all

import uno
import os
import unohelper
from com.sun.star.beans import PropertyValue
from com.sun.star.beans.PropertyState import DIRECT_VALUE
from com.sun.star.awt import Rectangle

context = XSCRIPTCONTEXT

def StoreWindowState(*dummy):
    oDoc = context.getDocument()
    oView = oDoc.getCurrentController()
    oWindow = oView.getFrame().getContainerWindow()
    CurrentPosSize = oWindow.getPosSize()
    CurrentMaxState = oWindow.IsMaximized
    uProps = oDoc.getDocumentProperties().getUserDefinedProperties()
    upropvalues = uProps.getPropertyValues()

    uPropnames = getpropnames( upropvalues )

    myproperties = (("CurrentPosSizeWidth",CurrentPosSize.Width, 65535),
                    ("CurrentPosSizeHeight",CurrentPosSize.Height, 65535),
                    ("CurrentPosSizeX",CurrentPosSize.X, 65535),
                    ("CurrentPosSizeY",CurrentPosSize.Y, 65535),
                    ("CurrentMaxState",CurrentMaxState, True ))

    for prop in myproperties:
        if prop[0] not in uPropnames:
            uProps.addProperty( prop[0], 128, prop[-1])
        uProps.setPropertyValue( *prop[:2] )
            
def SetWindowState(*dummy):
    oDoc = context.getDocument()
    oView = oDoc.getCurrentController()
    oWindow = oView.getFrame().getContainerWindow()
    uProps = oDoc.getDocumentProperties().getUserDefinedProperties()
   
    upropvalues = uProps.getPropertyValues()   
    uPropnames = getpropnames( upropvalues )

    oPosSize = Rectangle()
   
    mypropnames = ("CurrentPosSizeX",
                   "CurrentPosSizeY",
                   "CurrentPosSizeWidth",
                   "CurrentPosSizeHeight",
                   "CurrentMaxState")

    if not has_stored( mypropnames, uPropnames ):
        StoreWindowState( )

    x, y, width, height = [ uProps.getPropertyValue(pname) for pname in mypropnames[:-1] ]

    oWindow.setPosSize( x, y, width, height, 15)
    oWindow.IsMaximized = uProps.getPropertyValue("CurrentMaxState")
            
def getpropnames( properties ):
    return [ prop.Name for prop in properties ]

def has_stored( mypropnames, properties ):
    for propname in mypropnames:
        if propname not in properties:
            return False
    return True

def setEvents(*dummy):
    oDoc = context.getDocument()
    oEvents = oDoc.getEvents()
    p0 = PropertyValue()
    p = PropertyValue()
    p0.Name = "EventType"
    p0.Handle = 0
    p0.Value ="Script"
    p0.State = DIRECT_VALUE
    p.Name = "Script"
    p.Handle = 0
    p.State = DIRECT_VALUE
    thisfile = "vnd.sun.star.script:" + os.path.basename(__file__)
    docEvents = [["OnLoad",thisfile + "$SetWindowState?language=Python&location=user"],
                 ["OnSave",thisfile + "$StoreWindowState?language=Python&location=user"],
                 ["OnSaveAs",thisfile + "$StoreWindowState?language=Python&location=user"]]
    for docEvent in docEvents:
        p.Value = docEvent[1]
        l = []
        l.append(p0)
        l.append(p)
        a = uno.Any("[]com.sun.star.beans.PropertyValue", tuple(l) )
        uno.invoke(oEvents,"replaceByName",(docEvent[0],a))

def clearEvents(*dummy):
    oDoc = context.getDocument()
    oEvents = oDoc.getEvents()
    p0 = PropertyValue()
    p0.Name = "EventType"
    p0.Handle = 0
    p0.Value ="Script"
    p0.State = DIRECT_VALUE
    docEvents = ["OnLoad","OnSave","OnSaveAs"]
    for docEvent in docEvents:
        oEvents.replaceByName(docEvent,p0)

    uProps = oDoc.getDocumentProperties().getUserDefinedProperties()
    upropvalues = uProps.getPropertyValues()

    uPropnames = getpropnames( upropvalues )

    myproperties = ("CurrentPosSizeWidth","CurrentPosSizeHeight","CurrentPosSizeX","CurrentPosSizeY","CurrentMaxState")

    for prop in myproperties:
        if prop in uPropnames:
            uProps.removeProperty(prop)
    
g_exportedScripts = StoreWindowState,SetWindowState,test_Mri, setEvents, clearEvents,
Attachments
WindowStates.zip
Window Position and Size
(1.13 KiB) Downloaded 351 times
Last edited by Charlie Young on Mon Jul 29, 2013 2:46 pm, edited 1 time in total.
Apache OpenOffice 4.1.1
Windows XP
jimthegeezer
Posts: 10
Joined: Fri Jul 26, 2013 8:41 pm

Re: Save Window Size and Position per file

Post by jimthegeezer »

OK, I have downloaded Python code and put it in the right place, now please tell me how to install/activate it:
I open my spreadsheet, go to Tools -> Customise -> Events. There are 25 Events; likely ones seem to be "Open Document" and "Save Document".
So I click the Macro button -> My Macros -> Window States. This gives your macro options, which I need not describe; but it seems appropriate to assign SetWindowState to Open Document, and StoreWindowState to Save Document. So I do that, and the events are shown linked to the macros. Close the Customise window, and save the spreadsheet.
The following error message appears:

A Scripting Framework error occurred while running the Python script
vnd.sun.star.script:WindowStates.pySStoreWindowState?
language= Python8£location= user.
Message: <type 'exceptions.NameError'>: name 'test_Mri' is not defined
C:\Users\.Iames‘\AppData\Roaming\OpenOffice.org\3\user\Scripts\python\WindowS
tates.py:113 in function <module>Q [g_exportedScripts =
StoreWindowState,SetWindowState,test_Mri, setEvents, clearEvents,]
C:\Program Files (:66]‘\OpenOffice.org 3\Basis\program\pythonscript.py:418 in
function getModuleByUrl0 [exec code in entry.module._dict_]
C:\Program Files (x86]\OpenOffice.org 3\Basis\program\pythonscript.py:919 in
function getScriptO [mod = self.provCtx.getModuleByUrl(fileUri )1
I OK |

Please advise!
Open Office Org 3.4.1
OS Name Microsoft Windows 7 Home Premium
Version 6.1.7601 Service Pack 1 Build 7601
jimthegeezer
Posts: 10
Joined: Fri Jul 26, 2013 8:41 pm

Re: Save Window Size and Position per file

Post by jimthegeezer »

Further to the above, I have got it working OK now. For the benefit of others who would like to know, here are the details:
1. Edit the last line of the script WindowsStates.py to delete "test_Mri,"
2. Open the spreadsheet file (or whatever) and go to Tools -> Customise -> Events
3. Click on "Save Document" under Event
4. Click on Macro -> My Macros -> Window State -> setEvents -> OK -> OK
5. Save/close the file.
6. Open it again, and click on Tools -> Customise.
You will see that the "Set" and "Store" macros have been assigned to the appropriate actions.
Click OK; it will work from now on.

The correct size and position are assigned afterthe data are loaded; until then the window will be positioned where the last document of that type was saved.
Open Office Org 3.4.1
OS Name Microsoft Windows 7 Home Premium
Version 6.1.7601 Service Pack 1 Build 7601
User avatar
Charlie Young
Volunteer
Posts: 1559
Joined: Fri May 14, 2010 1:07 am

Re: Save Window Size and Position per file

Post by Charlie Young »

jimthegeezer wrote:Further to the above, I have got it working OK now. For the benefit of others who would like to know, here are the details:
1. Edit the last line of the script WindowsStates.py to delete "test_Mri,"
It was careless of me to leave test_Mri in that line. That was for debugging, and the routine itself is no longer in the file. I just now fixed the posted version.
jimthegeezer wrote: 2. Open the spreadsheet file (or whatever) and go to Tools -> Customise -> Events
3. Click on "Save Document" under Event
4. Click on Macro -> My Macros -> Window State -> setEvents -> OK -> OK
5. Save/close the file.
6. Open it again, and click on Tools -> Customise.
You will see that the "Set" and "Store" macros have been assigned to the appropriate actions.
Click OK; it will work from now on.

The correct size and position are assigned afterthe data are loaded; until then the window will be positioned where the last document of that type was saved.
It shouldn't be necessary be in Tools -> Customize events prior to running "setEvents," but it doesn't hurt to go there to look and see what the macro does.

In a document to which this has been applied, you can go to File > Properties > Custom Properties and see (and modify) the settings.
Apache OpenOffice 4.1.1
Windows XP
jimthegeezer
Posts: 10
Joined: Fri Jul 26, 2013 8:41 pm

Re: Save Window Size and Position per file

Post by jimthegeezer »

Thanks Charlie
Cheers
Open Office Org 3.4.1
OS Name Microsoft Windows 7 Home Premium
Version 6.1.7601 Service Pack 1 Build 7601
jimthegeezer
Posts: 10
Joined: Fri Jul 26, 2013 8:41 pm

Re: Save Window Size and Position per file

Post by jimthegeezer »

Well folks - those of you who have been following this fascinating series - no sooner had I got this problem put to bed, than up pops a window saying, "It's time to upgrade to Open Office 4.0.0!". Happy days! So I do that, in the confidence that the wise men will have fixed this old problem..... have they hell!

Upgrade goes along fine. So I call up favourite spreadsheet, and following a brief introduction on how wonderful 4.0.0 is, or something, up comes my spreadsheet with two undesirable items,
- an error message saying (translated into plain language) it can't find the bloody macro
- an imitation of the egregious Microsoft 'ribbon' - only stuck on the right side.

Anyway it doesn't take too long to get rid of the latter and reclaim some valuable screen space, but the macro took a bit more figuring. To make a long story short, do this:

1. navigate to where the macro is:
C:\Users\<user name>\AppData\Roaming\Open Office.org\3\User\Scripts\python

P.S. My footnote should now read Open Office Org 4.0.0
2. open another window at
.....................................Roaming\Open Office\4\User\Scripts ( note changed "Open Office" folder name)

3. Make the directory "python" here, and open it.

4. Move the script WindowStates.py from the old folder to the new one.
-- and all will be well.
Open Office Org 3.4.1
OS Name Microsoft Windows 7 Home Premium
Version 6.1.7601 Service Pack 1 Build 7601
jimthegeezer
Posts: 10
Joined: Fri Jul 26, 2013 8:41 pm

Re: Save Window Size and Position per file

Post by jimthegeezer »

A MISLEADING ERROR MESSAGE:
Well folks, after using this macro for three years or so, it suddenly began throwing a weird error "A Scripting Framework error occurred....." referring to this script, and following the recovery of one of my documents. After checking everything was in place like it used to be, I found the solution was to delete and reset the Events, for afflicted document:
Tools -> Customise -> Events, and remove the assigned actions at "Open Document" "Save... " "Save as..." , then click OK, and save the document (don't just close it without saving), Then open the same document again, and put the assigned actions right back again, and save once more. Problem solved
Open Office Org 3.4.1
OS Name Microsoft Windows 7 Home Premium
Version 6.1.7601 Service Pack 1 Build 7601
jimthegeezer
Posts: 10
Joined: Fri Jul 26, 2013 8:41 pm

Re: Save Window Size and Position per file

Post by jimthegeezer »

Further to the above, I read it again and thought it wasn't very clear about how to install this.
The script “WindowsStates.py” should be in ..\{username}\AppData\Roaming\Open Office\4\User\Scripts\python.

To install on a particular application:
From the traditional toolbar on the application, click [Tools][Macros][Run Macro]
In Macro Selector window, look in the Library window, and click on the "+" in front of "My Macros" then click on "WindowStates".
In Macro Name window, click "setEvents", then [Run]. Make sure your application is where you want it normally on the screen, then save it!
That's all folks!

- but if you want to see what is set, click [Tools][Customise]
If all else fails, you might try as before, but execute "clearEvents" and [Run] before "setEvents".
Open Office Org 3.4.1
OS Name Microsoft Windows 7 Home Premium
Version 6.1.7601 Service Pack 1 Build 7601
macuser33
Posts: 8
Joined: Wed Dec 28, 2016 1:13 am

Re: [Solved] Save Window Size and Position per file

Post by macuser33 »

This issue is NOT "solved". Having every user install a work-around is not a solution.

Furthermore, it appears that the "solution" provided does not apply to OpenOffice running on Mac OS (which the original poster was doing).

I strongly urge the powers that be to change the status of this issue to "Open" (or equivalent) ... and truly fix the issue.
Chuck (macuser33)
OpenOffice 4.1.14 on macOS 10.13.6 (High Sierra)
User avatar
Hagar Delest
Moderator
Posts: 32664
Joined: Sun Oct 07, 2007 9:07 pm
Location: France

Re: [Solved] Save Window Size and Position per file

Post by Hagar Delest »

We are just users like you. If you want to have it fixed, file a bug report: [Tutorial] Reporting bugs or suggestions.
LibreOffice 7.6.2.1 on Xubuntu 23.10 and 7.6.4.1 portable on Windows 10
User avatar
lxvi
Posts: 8
Joined: Sun Dec 22, 2013 9:32 pm

Re: [Solved] Save Window Size and Position per file

Post by lxvi »

I just put WindowStates.py into Scripts/python and then I ran setEvents and then when I click the Save icon I get the error message in the attached screenshot (because OO doesn't let you select & copy error text?!?!).

Please help
Attachments
OpenOffice Error dialog when trying to save
OpenOffice Error dialog when trying to save
OpenOffice 4.0.1 on 64-bit Vista Home Premium SP2
AOO401m5(Build:9714) - Rev. 1524958
2013-09-20 11:40:29 (Fr, 20 Sep 2013)
Post Reply