Actually in the end I don’t mind which method would end up working for what I’m trying to do, but it’s been a bit frustrating when the method that finally works otherwise ends up not working because under the hood, things aren’t what they appear on the surface.
The following step of a longer script seems to cause the usual assistive access error that makes an .app fail to run until end. It works correctly when run from Script Editor.
tell application "System Events" to tell application process "TextEdit"
tell window 1
set {position, size} to {{500, 23}, {560, 1120}}
end tell
end tell
I have allowed assistive access for the .app from System Preferences. I’m also aware of the fact that if I modify the app, its access has to be revoked and enabled again. I also added TextEdit to the list just in case, but it wasn’t about that either.
Now I start to wonder, is it actually System Events or even application progress TextEdit that would need assistive access enabled, instead of just the app itself? Not that I’d ever seen that done, but I’m running out of ideas here…
BetterTouchTool allows assigning triggers to set position and size of a window and it works when I actually press the keys, but trying to trigger that shortcut by Applescript key codes doesn’t do anything. I have – and can – successfully use key codes to trigger other keys though. That’s what I tried to do first, and when it didn’t work, I tried tell TextEdit to set bounds of window 1, but it always gave an error that was among the lines of “can’t set bounds to any”.
Copy your script and paste it into Script Editor. Save the script to the desktop as an application with the name Test Script.
Add the Test Script application to Accessibility under Security & Privacy of System Preferences.
Open a TextEdit window and then double-click on the Test Script icon on the desktop. The TextEdit window resized as expected.
Perhaps you might provide some information as to whether the above works for you and, if not, what happens.
BTW, I’m running Catalina not Yosemite, and there might be an issue with Yosemite that prevents the script from running (although it’s hard to imagine anything more draconian than Catalina in this regard). Also, I do not have TextEdit or System Events in Accessibility.
For sure, Nigel’s script is cleaner but, if for some unknown reason you really need to rely upon GUI Scripting, as you are running an old system, it may help to add one instruction:
tell application "System Events" to tell process "TextEdit"
set frontmost to true # ADDED
tell window 1
set {position, size} to {{500, 23}, {560, 1120}}
end tell
end tell
Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mardi 24 décembre 2019 21:47:24
Thank you all again for the solutions! I started by testing Nigel’s version and it worked!
I’m a bit surprised that I didn’t figure it out because I already played around with a very similar script that I had found an example for online, and that kept giving me the ‘can’t set bounds to any’ error. I can’t remember what the difference was because I deleted the bit that didn’t work; Maybe instead of commanding TextEdit directly, it was in a tell application “System Events” to tell application “TextEdit” block.
What do the 4 numeric values in the bounds sequence mean, by the way? I stopped guessing the numbers when the window would get thrown off-screen, and ‘get bounds of window’ was a better way of first figuring out where it actually is. 23 is the only one that could logically be the vertical distance (menu bar’s vertical height, because no window can intersect the menu bar).
No not really, I only went for it after my other methods failed! I nowadays avoid GUI scripting if at all possible because I’ve come to understand the risks: everything has to go right for it work every time, and even then, there’s the annoyance of assistive access! So, really happy that I don’t have to go for that last resort in this case after all!
Riku. I’m glad you were able to get your script working. I did want to comment on the above quote–hopefully without sounding preachy, which is not my intent.
Setting the position and size of a window is a common task in AppleScripts and many macOS apps support this natively with the bounds property. Examples of these apps are Safari, Mail, TextEdit, Script Editor, and Preview.
There are, however, many apps that do not support the bounds property and on my computer these include FSNotes, Xee, Soulver, Script Geek, and Easy Find. For these, use of the Position and Size properties of the Processes Suite of System Events (GUI Scripting) are quite useful and in my experience very reliable.
Most users will utilize apps like Better Touch Tool to position and size windows. For avid AppleScripters, and for inveterate fiddlers like me, GUI scripting is a good option.
BTW, I agree wholeheartedly that setting permissions is a pain and, unfortunately, it gets much worse with Catalina.