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
Related
I want to get the foreground window title in macOS.
I have tried using AppleScript for this, it works but is very slow.
Here is the AppleScript code:
tell application "System Events"
set frontApp to name of first application process whose frontmost is true
end tell
tell application frontApp
if the (count of windows) is not 0 then
set window_name to name of front window
end if
end tell
It takes a lot of time when we run this using Java.
Is there any other efficient solution to this?
I haven't used Java in ages, so I don't remember how it accesses frameworks (assuming I ever knew), but the framework you want is Quartz Window Services. These methods give you access to the window server, which manages all of the onscreen windows.
This AppleScript code works for me using the latest version of macOS Mojave.
For me, this AppleScript code is lightning fast.
try
tell application "System Events" to tell (process 1 whose it is frontmost) ¬
to tell (window 1 whose value of attribute "AXMain" is true) ¬
to set windowTitle to value of attribute "AXTitle"
end try
I have been searching endlessly with no avail on how to move a window between spaces on macos in applescript. The seemingly basic thing I want to do is move all open application windows from any space to the first space. If anyone out there can help, please reach out. There seems to be no straight forward way in doing this in the latest version of macos (Mojave 10.14.4). I am also open to doing this in any another language that will interact with macos if it is possible/simpler.
tell application "System Events"
repeat with proc in application processes where background only is false
tell proc
log "found app: " & name
set processName to name
log count of windows
repeat with win in windows
-- move window to first "space"
end repeat
end tell
end repeat
end tell
I think the only way to do this is just simulating moving spaces using
tell application "System Events" to key code 19 using {control down}
but then your desktop will bounce for each app like crazy.
There's a window manager for macOS called Phoenix that's scriptable using JavaScript.
I wrote a Phoenix script, phoenix-move-windows that moves windows based on the current monitor and screen layout. It is configurable so that it will move windows to a specified position, and you can have different arrangements based on the current number of screens, and number of spaces on each screen.
It does support moving all windows to a specified screen and space, using the "defaultBinding" in an arrangement.
I have an applescript made to close all apps:
tell application "System Events" to set quitapps to name of every application process whose visible is true and name is not "Finder"
repeat with closeall in quitapps
quit application closeall
end repeat
It works just fine. It quits all open applications.
My problem is that I want to modify this script to close only the apps that are hidden. For some reason, Apple hides all Apple-made apps with no active windows, and eventually it starts eating up my RAM.
What I thought was that if I just change the line whose visible is true to whose visible is false I would get that result.
Not quite:
I don't even know what this window is supposed to be, so I just hit cancel.
Well, it pops up again. Turns out I have to hit cancel exactly four times before the script bombs out.
Is there any way to quit all hidden applications, while leaving open the visible ones?
(Bonus points if you can explain the pop-up above.)
If it makes a difference, I'm running the latest version of OSX.
Setting visible to false affects all processes – even processes / applications without a GUI. If a process is not an application (.app) the application chooser appears.
Add a check for background only which affects only applications with a GUI.
tell application "System Events" to set quitapps to name of every application process whose visible is true and background only is false and name is not "Finder" ...
If I understand correctly, you are talking about how when you press the red close button, it only closes the application's windows, but not the application itself, so it's just left there still open.
In which case, you can use this script I made, it seems it works flawlessly:
-- Get open apps
tell application "System Events" to set openApps to name of every application process whose background only is false and name is not "Finder"
-- Do this for every open app
repeat with openApp in openApps
tell application openApp
-- Tell application to quit if it doesn't have any open windows
if (count of windows) is less than or equal to 0 then quit
end tell
end repeat
OSX doesn't "hide" apps. They just aren't active, or don't have any documents open. Hidden apps is a very specific process usually done with Command-H. Apps don't hide themselves in this fashion.
Rather than trying to close apps with no windows, use the document count to determine whether to close an app.
tell application "SomeApp" if count of documents = 0 then quit
This is a quirky issue. For reasons unknown to me, you have to separate stuff in order to get it to work. This quits every non-background app that's not in the igApp list. I added 'without saving' to deal with document apps like TextEdit but obviously, it's optional. While this could likely be streamlined somewhat, it seems to work reliably.
tell application "System Events"
set igApp to {"Finder", "Script Editor", "firefox"}
set qList to {}
set fApp to name of every application process whose background only is false
repeat with xx in fApp
if xx is not in igApp then
copy contents of xx to end of qList
end if
end repeat
end tell
repeat with yy in qList
quit application yy without saving
end repeat
-- qlist
Note that if you want to quit some (or all) of your background applications you should probably reconsider. Those are doing things in the background and unless something goes awry, they generally don't abuse a computer's resources. Some may require force-quit to stop. If your computer is starved for RAM when it's idle, then either some app is broken/buggy or you need more RAM.
With that said, if you swap 'background process' to true, it should work on any of the background applications. As I said, I recommend against this but you could comment out the 'quit application' line and then uncomment 'qlist' (the last line), and then you'll get a list of those applications. You could look those apps up in Activity Monitor's Memory panel to see how modest these apps' demands are. On my mac, if you exclude obvious apps like 'Dock', combined they use < 200 MB of RAM.
I am pretty new to programming, especially with AppleScript. I wrote a simple script for Valentine's Day to play a song from iTunes and then open a flash animation file in Safari. When I run the script in ScriptEditor, everything works as desired, but when I export as a standalone application, it fails at the command to enable full-screen mode. I am assuming it is an issue with System Events. To be clear, the application functions to the end, but at the keystroke command I hear an alert sound and the window remains as-is.
I am running Yosemite, and am fully updated.
Ideally, I would like to open the file in Google Chrome to utilize Presentation Mode, but I can't even get Chrome to open the file.
Thanks for any advice! Here is the code:
tell application "Finder"
set visible of every process whose visible is true and name is not "Finder" to false
close every window
end tell
set volume output volume 75
tell application "iTunes"
set currentVolume to sound volume
if player state is playing then
stop
back track
end if
play track "The Promise"
set player position to 6
end tell
delay 4
tell application "Safari"
activate
if (count of windows) is 0 then -- Remove "if" statement if you don't want to make a new window if there is none
make new window at front
end if
open (POSIX path of (path to home folder)) & "/Desktop/beMine/beMine.swf"
tell application "System Events"
tell process "Safari" to keystroke "f" using {command down, control down}
end tell
end tell
I agree with Jerry Stratton's comment that it could be an accessibility issue. However it also could be that you are issuing the keystroke command before Safari is ready to accept it. If it's opening a file then it could be busy and miss the keystroke command.
Also, I would move the system events code outside the Safari code and also just tell system events, rather than the Safari process, to perform the keystroke command. Try this as the Safari and System Events parts.
NOTE: I can't get Chrome to open a file either.
tell application "Safari"
activate
if (count of windows) is 0 then -- Remove "if" statement if you don't want to make a new window if there is none
make new window at front
end if
open (POSIX path of (path to home folder)) & "/Desktop/beMine/beMine.swf"
end tell
tell application "Safari" to activate
delay 1
tell application "System Events"
keystroke "f" using {command down, control down}
end tell
Most likely you’ll need to allow your standalone application to use System Events. At some point you needed to do that for Script Editor; you’ll need to do the same for your standalone app.
You’ll find the option in System Preferences under Security & Privacy, then Privacy, and then Accessibility. There’ll be a list of apps, and your app is probably listed there without a check for “Allow the apps below to control your computer.”
You may need to use the “+” button to add your app to the list.
I have verified that I can use this simple script to make Safari full-screen; it will work if the app is given permission under Accessibility, and it will silently fail if not.
tell application "Safari"
activate
end tell
tell application "System Events"
tell process "Safari" to keystroke "f" using {command down, control down}
end tell
This is Yosemite, Mac OS X 10.10; it may be different in other versions of Mac OS X.
I am loading a powerpoint presentation using applescript so I can automatically save it in another format.
Here is the code:
tell application "Microsoft PowerPoint"
open "/Users/Me/Desktop/test.pptx"
end tell
set theOutputPath to "/Users/Me/Desktop/Export.pdf"
tell application "Microsoft PowerPoint"
save active presentation in theOutputPath as save as PDF
end tell
tell application "Microsoft PowerPoint"
close active presentation
end tell
This works, however when opening a certain document made with PowerPoint for Windows instead of opening, exporting, closing PowerPoint gives me a popup:
"This presentation contains ActiveX controls, which will not work in PowerPoint. If you save this presentation, the ActiveX controls will be removed"
Unless I press "ok" nothing happens. So I cannot fully script load/export/close as after load i have to click on Ok with the mouse.
What actually happens is: AppleScript executes the open, and immediately tries the save. However the application is blocked on the above popup so AppleScript gives the following error:
Microsoft PowerPoint got an error: active presentation doesn’t
understand the save message.
So the script execution fails.
I searched in the dictionary and I don't find anything to solve this, nor did I find a solution in the PowerPoint preferences.
Does anyone know how to solve this?
If the ok button is the default button, try:
activate application "Microsoft PowerPoint"
tell application "System Events"
tell process "Microsoft PowerPoint"
keystroke return
end tell
end tell
Note : You must Enable Access for Assistive Devices