Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

macstatic

macrumors 68020
Original poster
Oct 21, 2005
2,056
175
Norway
I use an app (Hatari) which always opens its window in the middle of the screen, meaning I have to reposition it every time (I drag it to the top left corner). The app won't remember its screen position, nor is there an option for this in its preferences.
So I was thinking AppleScript or Automator would be of help here, but which one and how?
I'm on MacOS 10.14 (Mojave)
I was thinking of the script to be in the OSX Dock, and when clicked does the following:

1) open the specific app in question
2) position the window in the top left-hand corner

Update: I played around with the AppleScript Script editor app and have so far solved the first step (opening the app):
Code:
tell application "Hatari"
    activate
end tell

The next step is a little harder. I've tried various solutions found online, but none of them appear to work (and the error messages in Script Editor aren't very helpful or detailed):

Code:
set the position to {12, 22}
    set position of application "Hatari" to {724, 11, 967, 647}
    set bounds of front window to {300, 30, 1200, 900}
    set position of front window to {1, 1}
    set position to 120, 220

I suspect one of them will work, but that my syntax and/or values are wrong.
 
Last edited:
If the application doesn't provide any of those terms, System Events will need to be used to access the various UI objects, for example:

AppleScript:
tell application "Hatari" to activate
tell application "System Events" to tell application process "Hatari"
   set position of front window to {12, 22}
end tell

The script would need to be an application to be in the dock, a better place might be the Script Menu.
 
If the application doesn't provide any of those terms, System Events will need to be used to access the various UI objects, for example:

So some apps are more "Applescript friendly" while others are not and need to be handled differently?
How can I determine if an app I want to script is one or the other?


AppleScript:
tell application "Hatari" to activate
tell application "System Events" to tell application process "Hatari"
   set position of front window to {12, 22}
end tell

Awesome! That worked perfectly.
I would also like to add a specific screen-size, and if I'm not mistaken the "bounds" command is used for this (and also includes the screen position), so why won't the following work?

AppleScript:
tell application "Hatari" to activate
tell application "System Events" to tell application process "Hatari"
    set bounds of front window to {0, 0, 1280, 800}
end tell

What I've intended is to move the window to the very upper left corner of the screen (X=0, Y=0), then resize the same window to 1280 pixels wide and 800 pixels high.
The script opens up the Hatari app, but does nothing to the window size/position but give me this error message:
"Can't set bounds of UI element to any"


The script would need to be an application to be in the dock, a better place might be the Script Menu.
Excellent idea! I've just enabled the Script menu now, but for this specific script I think it might be more practical to put it in the Dock (or both Dock and Script menu.
I suppose for the Dock I'd want to (within Script Editor) to "Save as..." and select "File format: Application".
 
To see the terminology an application provides (if any), you can open its scripting dictionary in the Script Editor.

Depending on what the application provides, not all classes and properties may be available from System Events. For example, in the Finder there isn't a bounds property for a window, but it provides its own bounds property for a Finder window. In that case you would target the application directly.

You might try setting the individual size and position properties.
 
To see the terminology an application provides (if any), you can open its scripting dictionary in the Script Editor.
Are you referring to the window that pops up when I go to "Window"-"Library", where I then double-click "Script Editor"?
Screenshot 2025-05-01 at 21.26.19.png
Screenshot 2025-05-01 at 21.27.35.png


Depending on what the application provides, not all classes and properties may be available from System Events. For example, in the Finder there isn't a bounds property for a window, but it provides its own bounds property for a Finder window. In that case you would target the application directly.
I'm a little confused.
So for this particular script I'm working on, to control the window of the Hatari app, I would ideally want to look up Hatari's scripting capabilities, but if I try to add it to the "Library" window (by pressing "+", then locating and selecting "Hatari"), but this only gives me the following error message:
Screenshot 2025-05-01 at 21.31.02.png


... which I now understand you were probably referring to in your previous posting where you said:
If the application doesn't provide any of those terms, System Events will need to be used to access the various UI objects,

so if I'm not mistaken I need to look up which properties are available in System Events.
And indeed... it is available there:
Screenshot 2025-05-01 at 21.36.46.png


Which leaves me asking, why doesn't it work?
Perhaps my syntax or context is wrong. Where's a good place to look this up?

You might try setting the individual size and position properties.
I'll give it a go, looking up the available properties as you suggested.
 
Hey, I got it working!

AppleScript:
tell application "Hatari" to activate
tell application "System Events" to tell application process "Hatari"
    set position of front window to {0, 0}
    set size of front window to {1280, 822}
end tell
 
Script Editor's File > Open Dictionary... will open the selected scripting dictionaries. The dictionary library initially contains some common apps, but it doesn't necessarily contain all scriptable apps, since you can add to and edit the list. As your error message indicates, the application is not scriptable, so it doesn't have a dictionary to add.

why doesn't it [bounds] work?
Who knows, the app may be using something different that System Events doesn't look at, such as a window subclass, so some properties may not be available. That is why I used the Finder example, as it has similar behavior - there isn't a bounds property for a window when using System Events, but Finder has a bounds property for its Finder Window.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.