Set size works in Script Editor but not Studio?

Discussion in 'Mac Programming' started by memphis131, Mar 31, 2012.

  1. macrumors newbie

    Joined:
    Mar 31, 2012
    #1
    Hello all. I've been fooling around with applescript studio for some time but and I've solved many problems, but I have something that I believe should be simple, yet it is not. :(

    Basically what I have is an AppleScript Studio gui with a min and max size. I would like to expand the box to the max size, if it is the min size by clicking a button after launch.

    In general what I do is make small code snippets in Apple editor then if it works, I move it over to applescript studio and make the appropriate changes. The problem I have is the below snippet works just fine in AppleScript Editor, however when I link the code to a button on my GUI in AppleScript Studio, the application just lags out and does nothing.

    Any help would be appreciated. :(

    Code:
    tell application "MyApplication" to activate
    tell application "System Events" to tell application process "MyApplication"
    	try
    		get properties of window 1
    		set size of window 1 to {439, 300}
    	on error errmess
    		log errmess
    		-- no window open
    	end try
    	
    end tell
    
     
  2. macrumors 6502

    Joined:
    May 29, 2011
    Location:
    Littleton, Colorado, USA
    #2
    Probably because the script is waiting for System Events, which is waiting for your application to respond to it. An application should know about its own windows - is there any particular reason your application just doesn't set the window size itself?
     
  3. thread starter macrumors newbie

    Joined:
    Mar 31, 2012
    #3
    Hey Red Menace, thanks for the reply.

    "is there any particular reason your application just doesn't set the window size itself?"

    My apologies if I'm wrong, but I assume you mean telling my application to specifically reference the window in question and set it's size? If this is the case, I have tried this before and it doesn't process correctly. Perhaps my code is wrong, but whenever I specifically reference the window (and I have verified the app name/window name are exact), it simply states that you can't set the size of window X. Here is an example:

    Code:
            tell application "MyApp"
                activate
            try
                set size of window "MyWindow" to {439, 300}
                on error errmess
                log errmess
                -- no window open
            end try
     
  4. macrumors 6502

    Joined:
    May 29, 2011
    Location:
    Littleton, Colorado, USA
    #4
    The bounds and size properties are both read/write - does the window actually exist when you are trying to set the property?

    You also don't need to tell your application to activate - if you are trying to show the window, you would send it the show command.
     
  5. thread starter macrumors newbie

    Joined:
    Mar 31, 2012
    #5
    Yeah the activate is definitely a bit redundant I know. :p Part of me uses it though as an extra step in debugging in the sense that if the window didn't exist, I would think I should get a message about not being able to activate the window app.

    As far as the window itself, it defintely does exist as the button I'm clicking is located on the window itself. I've tried using various scripts to pump out the various open window names and my app window name shows up, but the set size or bounds always fail saying "can't" do this or "can't" do that.
     
  6. macrumors 6502

    Joined:
    May 29, 2011
    Location:
    Littleton, Colorado, USA
    #6
    You might try using an index such as "window 1", or get the window of the button in the clicked handler and use that.
     
  7. thread starter macrumors newbie

    Joined:
    Mar 31, 2012
    #7
    As an update, I've tried "window 1" and even "front window". . . both fail with the message "can't get window".

    It's crazy because if I run a script from the applescript editor to pull all window names active I can clearly see "MyWindowName" as an active window. . .
     
  8. macrumors 6502

    Joined:
    May 29, 2011
    Location:
    Littleton, Colorado, USA
    #8
    In your button handler, try using something like
    set size of (window of theObject) to {439, 300}

    Or check what it thinks the name is with something like
    get name of (window of theObject)
     
  9. thread starter macrumors newbie

    Joined:
    Mar 31, 2012
    #9
    Hey Menace, thanks for the help.

    I still am not sure why but the above suggestions did not help. I wound up doing some digging and found a different work around.

    What I did was. . .

    1 - create a property called MyWin

    2 - Linked the property to the app window MyWin

    3 - Used the the nswindow class to use the method "setFrame_display_animate". Not only did this allow me to effectively resize the window as I needed (including set the x/y position) but it do so in an aesthetically pleasing way too. Here is the code:

    Code:
     tell application "System Events"
                try
                    MyWin's setFrame_display_animate_({{(x of origin of (MyWin's frame())), (y of origin of MyWin's frame())}, {439, 300}}, true, true)
                    on error errMsg
                    log errMsg
                    display dialog errMsg
                end try
            end tell
    
    
     
  10. macrumors 6502

    Joined:
    May 29, 2011
    Location:
    Littleton, Colorado, USA
    #10
    That looks like AppleScriptObjC - is this an AppleScriptObjC project (Snow Leopard and later) or AppleScript Studio (Leopard and earlier) as mentioned in your original post? The handler/method declarations and the way objects are specified is quite a bit different between the two (if it is an ASObjC project, you still don't need to use System Events).
     
  11. thread starter macrumors newbie

    Joined:
    Mar 31, 2012
    #11
    Hey Menace,

    It is indeed an AppleScript Studio project created within Xcode, however, as I have found out, it appears you do have the ability to call upon ObjC methods and such with a little tweaking.

    As far as the system events part, I removed that a while ago and tweaked the code to the following, if anyone needs it for future use:

    Code:
    try
                    
                set {{x:oldX, y:oldY}, {width:oldWidth, height:oldHeight}} to (MailSend's frame()) as list            
                MailSend's setFrame_display_animate_({{oldX,oldY}, {439, 300}}, true, true)
                on error errMsg
                    log errorMsg
                    display dialog errMsg
            end try
    
    This will allow someone to reference the previous x/y/height/width values, should they need them (using oldX, oldY, oldWidth, oldHeight).

    ----------

    I just saw the earlier/later part.

    I am using Lion. When I created the project, I created it as "Cocoa-AppleScript Application" which according to the description builds an AppleScript project.
     
  12. macrumors 6502

    Joined:
    May 29, 2011
    Location:
    Littleton, Colorado, USA
    #12
    For future reference, AppleScript Studio refers to the earlier (deprecated in Snow Leopard) framework for creating applications in Xcode using AppleScript. AppleScript Studio was more like AppleScript in that the way objects and methods were referenced was more or less the same as any other AppleScript.

    The current framework (AppleScriptObjC) uses the Cocoa frameworks in much the same way as Objective-C, so is quite a bit different than its predecessor.
     
  13. thread starter macrumors newbie

    Joined:
    Mar 31, 2012
    #13
    Ahh my apologies. Thank you for all your help.
     
  14. macrumors 6502

    Joined:
    May 29, 2011
    Location:
    Littleton, Colorado, USA
    #14
    No problem; if either of us had posted a complete button action, the lack of on clicked theObject would have been a dead giveaway.
     

Share This Page