Getting AppleScript error -1728 when trying to get bounds of desktop - applescript

I'm trying to get the bounds of the desktop using the following code:
tell application "Finder"
get bounds of window of desktop
end tell
I keep getting the following error:
error "Finder got an error: Can’t get bounds of window of desktop." number -1728 from bounds of window of desktop
I found that the same question has been asked --> get bounds of desktop with applescript but the solution over there (something about turning on this preference: defaults write com.apple.finder CreateDesktop -bool false) didn't work for me when I tried it. I'm using High Sierra, while that solution was given back in 2012.
Any ideas what else I could try to get the bounds of the actual screen, not my Desktop directory?
Thanks

Related

xcode ios simulator: how to set focus on specific window

I have n instances of xcode ios simulator and I want to switch a focus on instance with specified id.
I tried next things:
Switching focus using "open": open -a Simulator --args -CurrentDeviceUDID <id>. But it opens the last focused simulator instance instead of the instance with passed id.
Switching focus using simctl tool. I tried to open URL in the specific instance of the simulator xcrun simctl openurl <id> "https://www.google.com", but it doesn't change the focus.
using applescript - it still focusing on the last focused simulator instance:
tell application "System Events" to tell process "Simulator"
perform action "AXRaise" of window 0
end tell
Do you have any idea, how to do this?
The following example AppleScript code is one method that works for me:
Example AppleScript code:
tell application "Simulator"
activate
set winName to the name of ¬
the first window ¬
whose visible is true ¬
and index is not 1
end tell
tell application "System Events" to ¬
perform action "AXRaise" of ¬
window winName of ¬
process "Simulator"
Notes:
The example AppleScript code assumes you have two Simulator windows visible, and it will toggle raising between the two. Obviously this is just an example and you'll need to modify and incorporate the methodology into you own code.
In your example AppleScript code, window n refers to its index as in:
index (integer) : The index of the window, ordered front to back.
    The quoted line above is from the AppleScript dictionary for Simulator in Script Editor.
The z-order of index starts at: 1
Using System Events and perform action "AXRaise" of window ... can be done by it's name property or it's index property.
The example AppleScript code, shown above, was tested in Script Editor under macOS Catalina with Language & Region settings in System Preferences set to English (US) — Primary and worked for me without issue1.
1 Assumes necessary and appropriate settings in System Preferences > Security & Privacy > Privacy have been set/addressed as needed.
Note: The example AppleScript code is just that and sans any included error handling does not contain any additional error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors. Additionally, the use of the delay command may be necessary between events where appropriate, e.g. delay 0.5, with the value of the delay set appropriately.

How to change the window title text on Mac with Applescript?

I am trying to change the window title text on my Mac with Applescript.
I followed this post Rename window title in OS X with AppleScript or terminal, but it did not change the text of the Telegram app window title and gives this error: Syntax Error: A property can’t go after this identifier.
I used this script:
tell application "Telegram"
set custom title of window 1 to "I want to change this"
end tell
Can someone find a solution to my problem?
Telegram does not include even the most rudimentary AppleScript support, sorry to say.

Cannot get or set the window size of some apps using AppleScript?

For some apps, such as Notes, or VLC, the following can be used:
tell application "Notes" to get the bounds of the window 1
if I put the above line into a file and use sudo osascript thatfilename to invoke it, it works.
But for some apps such as the Angry Birds Space, the following will not work?
tell application "Angry Birds Space" to get the bounds of the window 1
giving an error:
execution error: Angry Birds Space got an error: Can’t get bounds of
window 1. (-1728)
Is there something special of that app that prevents it? (because it is a game or does it not have window 1?)
I am looking into getting and setting the bounds or just the width of some window (just the width as some window seem to have to have a specific aspect ratio, so I think setting it to a specific width and height may not work if the aspect ratio isn't correct).
If the application is not scriptable, you won't be able to get this kind of information from it. If you have the System Preferences > Accessibility preference for "Enable access for assistive devices" set, you may be able to use the System Events processes suite to get/set the window size, for example:
tell application "System Events" to tell application process "Angry Birds Space"
get size of window 1
end tell
Note: Enabling accessibility varies gravely between macOS versions. How to Enable Accessibility on Mac OS X
is a good guide. As of 10.14, you have to go to Security & Privacy > Accessibility option > Privacy tab and add the application in the "Allow the apps below to control your computer".
Is Angry Birds Space scriptable? If not, telling it do anything should not work. A similar example would be:
tell application "Preview" to get the bounds of the window 1
As Red Menace points out, you can use the application process like this:
tell application "System Events" to tell application process "Preview"
tell window 1
set {size, position} to {{1280, 800}, {50, 50}}
end tell
end tell

Positioning windows with applescript on dual monitors

I have 2 air applications that I wrote. They auto fullscreen after 10 seconds. Before then, they need to be sent to their proper displays. "app_1" needs to run on display 1, "app_2" needs to run on display 2.
Essentially, I have this code:
do shell script "cd /Applications/app_1.app/Contents/MacOS/ ; open app_1;"
which works for me flawlessly. Both apps are launched that way, and there is some code for ensuring that the apps weren't already open, and closing them if they were.
I tried to add in a script to position the app after it is launched:
do shell script "cd /Applications/app_1.app/Contents/MacOS/ ; open app_1;"
tell first window of application "app_1" to set bounds to {0,0,1920,1080}
This gives me an error:
app_1 got an error: Can't set bounds of window 1 to {0,0,1920,1080}
I tried adding a delay of a couple seconds before the set bounds, in case the application hadn't yet launched when the set bounds fired off, however this didn't change anything.
I also tried setting the bounds to something like {100,100,200,200} just to see if I had the screen coordinates wrong or something, but still the exact same error, only with the {100,100,200,200} instead of the original 1920x1080 coordinates.
Anyone have any insight on this? I've been trying to find the solution on google for a couple of hours now.
It sounds like your app isn't exposing the standard "window" class. I don't know if AIR apps are supposed to automatically take care of this and it's not working—if so, you'll want to debug that.
But another alternative is to use UI Scripting to control its windows externally. Instead of this:
tell first window of application "app_1" to set bounds to {0,0,1920,1080}
Do this:
tell application "System Events"
set position of first window of application process "app_1" to {0, 0}
set size of first window of application process "app_1" to {1920,1080}
end tell
However, this will only work if you've gone to the Universal Access pane of System Preferences and checked "Enable access for assistive devices" (or done the same via API, "sudo touch /var/db/.AccessibilityAPIEnabled", etc.).

Applescript: How to get window count of a process in all spaces?

Updating Microsoft Office to the newest version caused a pop over error to occur every few hours. "Mail could not be received at this time." Only restarting the app makes the error go away. Many people seem to have this problem with no resolution so I (a windows programmer) decided to write my first AppleScript.
First I tried to get the count of windows for Office:
tell application "Microsoft Outlook" to display dialog (count of windows)
returns 1 which is no good. Does this mean the pop over dialog isn't considered a window by OSX? Hmm. Seems unlikely.
Second I tried to get the window count from the process:
tell application "System Events" to tell process "Microsoft Outlook" to display dialog (count of windows)
returns 2. Great. Window 1 is the one I need and I write the script, run it, and works perfectly ... until I change from the space Outlook is in to a different space. From the new space count of windows returns 0. On further research it seems the spaces module is not included in Lion for AppleScript.
Anyone know how I can get a count of the process windows in all spaces? Is there another way to detect the pop over?
You may need to activate the application:
activate application "Microsoft Outlook"
tell application "System Events"
tell process "Microsoft Outlook"
if accessibility description of window 1 is "alert" then
beep
-- enter rest of your code
end if
end tell
end tell

Resources