PDA

View Full Version : Set size works in Script Editor but not Studio?




memphis131
Mar 31, 2012, 04:52 PM
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. :(


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



Red Menace
Mar 31, 2012, 06:33 PM
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?

memphis131
Mar 31, 2012, 07:22 PM
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:


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

Red Menace
Mar 31, 2012, 08:18 PM
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.

memphis131
Mar 31, 2012, 08:40 PM
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.

Red Menace
Mar 31, 2012, 08:47 PM
You might try using an index such as "window 1", or get the window of the button in the clicked handler and use that.

memphis131
Mar 31, 2012, 09:46 PM
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. . .

Red Menace
Mar 31, 2012, 09:54 PM
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)

memphis131
Apr 1, 2012, 02:29 PM
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:


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

Red Menace
Apr 1, 2012, 03:40 PM
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).

memphis131
Apr 1, 2012, 04:10 PM
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:

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.

Red Menace
Apr 1, 2012, 04:49 PM
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.

memphis131
Apr 1, 2012, 05:02 PM
Ahh my apologies. Thank you for all your help.

Red Menace
Apr 1, 2012, 05:15 PM
No problem; if either of us had posted a complete button action, the lack of on clicked theObject would have been a dead giveaway.