I created an AppleScript application that resizes a TextEdit window to be just large enough to accommodate the document’s text without wrapping lines (ie, without enabling the scroll bar.) For the sake of this discussion, I named the application “ResizeWindow.app” and saved it on the desktop. I found a large discrepancy in execution speed as follows:
Normal execution speed:
Either… Open the app as a compiled script in Script Editor, then run the script directly in Script Editor
Or… Run the app from a separate AppleScript app using the following “run script file” command:
run script file ((path to home folder as string) & "Desktop:ResizeWindow.app")
Up to seven times slower execution speed:
Either… Open the app with Finder directly (ie, double-click its icon)
Or… Open the app from a separate AppleScript app using the Finder as follows:
tell application "Finder" to open file "ResizeWindow.app" of desktop
The same difference in execution speeds was observed whether the resizing script was saved as an application (.app) or as a compiled script (.scpt).
My questions are: 1) What is the cause of the discrepancy in execution speeds? and 2) Is there a way to get an app opened by Finder to execute as fast as when it is run by Script Editor or “run script file”? Thank you for any observations or suggestions.
The resizing app with an included sample document is as follows:
-- CREATE A SAMPLE TEXTEDIT DOCUMENT
tell application "TextEdit"
activate
make new document with properties {text:"Newton's Three Laws of Motion:
I. Every object in a state of uniform motion will remain in that state of motion ...
II. Force equals mass times acceleration.
III. For every action there is an equal and opposite reaction."}
end tell
-- RESIZE THE WINDOW
tell application "TextEdit"
tell window 1 to set zoomed to true -- start with a fully zoomed window
end tell
tell application "System Events" to tell process "TextEdit"
set frontmost to true
tell window 1
set {x_max, y_max} to size -- get the starting size of the fully zoomed window
repeat with xy_axis in {"y", "x"} -- resize the vertical then the horizontal dimension of the window
if contents of xy_axis is "x" then -- set the maximum and minimum amounts (delta) by which the window will be resized with each iteration, as well as switches to apply the resizing to the horizontal or vertical dimension selectively
set {delta_max, delta_min, x_delta_switch, y_delta_switch} to {-x_max / 2, 5, 1, 0}
else if contents of xy_axis is "y" then
set {delta_max, delta_min, x_delta_switch, y_delta_switch} to {-y_max / 2, 5, 0, 1}
end if
set delta_value to delta_max -- set the starting delta to 1/2 the dimension of the fully zoomed window
repeat
set abs_delta_value to delta_value * (1 - 2 * ((delta_value < 0) as integer)) -- absolute value of delta
tell scroll area 1 to tell scroll bar 1 to if enabled then -- if scroll bar is enabled, window size is too small
if delta_value = delta_max then exit repeat -- quit if fully zoomed window is too small
if delta_value < 0 then set delta_value to delta_value * -1 -- otherwise, be sure the next window resizing is in the optimal direction
else -- if scroll bar is not enabled, window size is either optimal or too big
if abs_delta_value < delta_min then exit repeat -- quit if window size is optimal
if delta_value > 0 then set delta_value to delta_value * -1 -- otherwise, be sure the next window resizing is in the optimal direction
end if
set {x, y} to size
set size to {x + delta_value * x_delta_switch, y + delta_value * y_delta_switch} -- resize the window
if abs_delta_value > delta_min then set delta_value to delta_value / 2 -- halve the amount by which the window will next be resized, but no smaller than the minimum amount
end repeat
end repeat
set {x, y} to size
set position to {round (x_max - x) / 2 rounding down, round (y_max - y) / 2 rounding down} -- center window on screen
end tell
end tell