Need an Applescript to close all open mail messages - applescript

My mom is 150 years old and when using mail on her mac she clicks a message so it opens in it's own window, then instead of closing it she minimizes it to the dock. Eventually she'll have 40 open mail messages and her mac slows way down.
Does anyone have an applescript that will close all open mail messages, set to run automatically once a day?
Sorry I don't know a thing about applescript.

Love it, can really empathise with the elderly relatives doing the most unexpected things with technology.
Give this script a try. It works for me on 10.7, may not on a later OS.
if application "Mail" is not running then
return
end if
tell application "Mail"
set message_viewer_title to the name of message viewer 1 -- we don't want to close the main window
close (every window whose name is not message_viewer_title)
end tell
Note: If a new message (draft) is open that has never been saved, Mail will ask what to do.
I'll let you have a look around for a scheduling solution.

Related

AppleScript tell application "System Events" extremely slow only on 1 account of my Mac

I'm baffled by this. Any AppleScript which uses tell application "System Events" runs slow only on 1 account of my Mac but fine on the other account.
Just as a simple example, tell application “System Events” to display dialog “Hello World” displays the dialog almost instantly on account 1, but will take up to 6 or 7 seconds to display the dialog on account 2.
EDIT: Just to clarify, As user3439894 mentioned in the comment below. I understand that display dialog doesn't need to be called by System Events. This issue has nothing to do with display dialog. I'm using it as a simple example to demonstrate the issue. As I mention below ANY code I call inside system events is slow (eg. sent frontmost to true, click menu items, etc.).
Its only system event calls though. A simple display dialog “Hello World” or any other code that does not involve system events will run fast on both accounts.
A few more notes:
It's not just display dialogs in tell application "System Events", it's ANY code I attempt to run in tell system events (e.g. click menu items, set frontmost to true, ect.). I'm just using the display dialog as a simple example.
The delay is actually inconsistent. For example, with the display dialog command, most of the time it takes about 6 or 7 seconds for the dialog to appear, but occasionally the dialog will appear instantly. Other times, the dialog will appear instantly but then clicking the "OK" button will cause a 6 or 7 second delay (with the beachball).
Both accounts on same Mac are admin accounts.
The delay occurs in script editor, osascript, or apple script applications.
I'm lost. Any ideas?
EDIT 2 - More clarifications based on comments:
I've been trouble shooting this for several days and at this point I'm fairly certain of the following:
This has NOTHING to do with my code or how I am calling the script. It also is not AppleScript in general, or the account being slow.
Scripts NOT involving tell application "System Events" run fine on both accounts. Both accounts are generally running fast.
It's specifically tell application "System Events"on ONE account.tell application "System Events"runs fine on one of my user accounts. The SAME exact script, called the EXACT same way is taking 6 to 7 even 10 seconds to run on the second user account on the same computer. Both accounts have admin access.
#TedWrigley provided several recommendations in the comments of my main post that allowed me to identify the issue. So full credit goes to him! Thank you #TedWrigley! I'm posting here and marking this as the correct answer incase others encounter an issue similar to this.
EDIT: It was a specific Elgato Stream Deck Plugin; the Zoom Plugin!
Booting in safe-mode allowed me to initially identify that Elgato Stream Deck Software (VERSION 4.9.2) I have installed appeared to be causing the issues. I don't understand why that was interfering with System Events because SE did not appear to have any issues in Activity Monitor and I have the Stream Deck Software on both accounts, but it was only causing issues with System Events on one account.
After un-installing and re-installing that software, AppleScripts calling tell application "System Events" was running as expected on both accounts...
UNTIL I re-installed my plugins. I discovered that is was not actually the Stream Deck application itself that was interfering with System Events but a specific plugin, the Zoom Plugin V2.1 11/16/20
https://lostdomain.org/stream-deck-plugin-for-zoom/. I only use this plugin on one account which is why I was only seeing the System Event issue on that account.

Redirected to applescript editor on clicking apple notification

I am using
osascript -e 'display notification "Lorem ipsum dolor sit amet" with title "Title"'
to display notifications in Mac. However, on clicking the notification, I am getting redirected to the applescript editor. Is it possible for me to redirect the user to a url or open up a directory on clicking the notification which is generated?
The run handler will only get called if the script is saved as an app, preferably a stay-open app. In any case, the app has to be still running when someone clicks the notification. You won't get this behavior from a simple osascript string.
You could get osascript to run a compiled script file (which can store properties persistently), but you will still need to distinguish between the run event that happens when you run the script, and the run event that gets called when someone clicks the notification.
I can suggest a couple of solutions here.
Use a python library to fire off notifications and forget about
appleScript/OSA. You can find some information, and various solutions at this
stackoverflow link:
Python post osx notification
Set up a stay-open appleScript app as a kind of 'notification server' and send a message to that (possibly with OSAscript, unless you can send a raw apple event to the 'server' from python) when you want to set up some notification intercourse. This is tricky, and seems overcomplex, compared to my first suggestion. In particular, you may still need to mess about with the privacy settings (especially if on Mavericks or later) to allow OSAscript access to system events.
Here are a couple of links which may guide you with the latter approach, but I really thing the first suggestion will get you further, with fewer tears:
http://jacobsalmela.com/bash-script-enable-access-assistive-devices-programmatically-os-x-mavericks-10-9-x-simulate-keystrokes/
http://support.apple.com/kb/HT6026?viewlocale=en_US&locale=en_US
so yes there is a way to do what you would like
here is a tutorial here
this is a simplified version that does what you like, however you must save it as an application and drag a file on it.
on open theItems
display notification "Open stackoverflow ?" with title "Stackoverflow"
delay 2
end open
on run
tell application "Safari"
tell window 1
set current tab to (make new tab with properties {URL:"http://www.stackoverflow.com"})
end tell
end tell
end run

How to open, close and close the dialogs of another application using cocoa or applescript?

I want to open another separate application, open the projects/documents of that application in a iterative way and then close the application. I also want to close all the modal and non modals dialogs which popped up during the opening of the document. I want to close all the dialogs including the crash dialog in case the application fails/ crashes.
What will be the best way using cocoa or applescript to achieve this and from where i can get more detailed information?
If the app has a scripting interface, of course the best way is to do that.
You generally don't want to iterate in AppleScript, but rather to operate on all of the results of a query.
For example, for almost any application that implements the "standard suite", you can just:
tell app "TextEdit" to close windows
This is much simpler (and faster, and more likely to be implemented correctly in the target app) than:
tell app "TextEdit"
repeat with theWindow in windows
close theWindow
end repeat
end tell
Of course this may pop up save/abandon changes dialogs, and it may skip over or include dialogs and inspectors, and so on, depending on the application's user model.
More importantly, it won't work if the app doesn't support scripting (and the standard suite).
Also, it won't help at all with closing a crash report—that window is owned by CrashReporter, not the original application (which is a good thing, because you can't talk to the original application anymore, now that it's crashed…).
The alternative is the UI Scripting features in System Events. This will only work if assistive access is enabled. It can also be a bit fiddly to figure out which windows are the ones you want to deal with, and which controls are the ones you want.
For example:
tell app "System Events"
click button 1 of windows of application process "TextEdit"
end tell
This works by finding every window (no matter what kind) owned by the TextEdit process, and simulating a click on the first button in that window (the red close button).
If you google "AppleScript UI Scripting" you should find lots of different guides. The first hit I found was http://www.makeuseof.com/tag/applescripts-ui-scripting-mac/ and it looks like a decent place to start.

Is there a way to get a permanent identifier for any given window

I'm trying to write a script that can control different windows from different programs (set position and size). I've got things just about working but I'm having problem accurately identifying windows. Right now I've got:
tell application "System Events"
tell application "Mail"
set windowName to name of window 1
end tell
end tell
This is a pretty simplified version of what I have working now. I'm grabbing the window information for many different applications and storing them as properties which are being called upon by another script later:
tell application "System Events"
tell application "Mail"
set position of window windowName to valueX
end tell
end tell
This works as long as the name of the window doesn't change. In many other applications I have no problems because window titles don't change (iCal, iChat, etc). In Mail the window title changes depending on how many e-mails are in your e-mail box. If an e-mail comes in between the first part of the script and the last then the script fails.
I can't really refer to the window by it's index number because those change as the order of the windows change (front to back). I thought maybe the window ID would work, but I need my script to work even if an application has been quit and restarted and the ID number changes if the application is relaunched. Am I SOL, or is there something I hadn't thought of?
Not SOL, but you'll have to do a little more work. There's only a few types of windows. You have the main browser window and you have email messages, whether it be a draft they are composing or an email message they're reading. Maybe you're concerned with the preferences window too. So you have to store the type of window. If it's a browser then you also will have to store the currently selected message. If it's an email window then you store the message id too. For browsers you just open a new browser window and restore the selection. For email messages you just open the messages. You'll have to check Mail's dictionary for other types of windows, but the idea will be the same.

How to tell if a menu item is 'checked'?

I'm building an Applescript that will scan my network every X minutes, checking for my house's Xbox360 or PS3 and enabling my Transmission BitTorrent client Speed-Limit Mode when either console is online.
Currently I can only Pause all transfers or resume all transfers using applescript, as there are separate key-commands for start/stop transfer. I want it to go into speed-limit mode though, not stop completely.
My issue is that the Speed-Limit (Turtle) mode is the same key to turn it on/off. If anyone touches the speed-limit manually, it will be out of sync and will actually turn speed-limit off when the consoles come online. Also if one console comes online, the speed-limit will come on, but then if the other console comes on, the limit will be turned off.
The menu item becomes 'checked' when the speed-limit is active, but I do not know how to test for this. There was nothing in the applescript dictionary for the transmission app.
How can I determine whether a menu item is 'checked'(It even shows an actual check-mark) in Applescript?
[Edit:] I'm currently trying to figure out how to turn the Speed-Limit on via RPC, rather than trying to script it using the GUI or keycommands, since the developers don't provide any applescript access.
http://trac.transmissionbt.com/browser/trunk/doc/rpc-spec.txt
I had the exact same issue, and finally figured out how to check if Transmission's Speed Limit menu item is checked (and you could easily modify this to check for menu items in other applications). This has been dead for almost a year now, but hopefully this helps.
tell application "Transmission" to activate
tell application "System Events"
tell process "Transmission"
set speedLimitCurrentlyOn to (value of attribute "AXMenuItemMarkChar" of menu item "Speed Limit" of menu "Transfers" of menu bar 1 as string) ≠ ""
display dialog "Speed Limit On: " & speedLimitCurrentlyOn
end tell
end tell
PS:
I adapted this from the AppleScript here: http://mac.softpedia.com/progDownload/Transmission-Auto-Speed-Limit-Download-60275.html
What information you are able to divine from any given application via AppleScript is entirely up to said application's developer. If Transmission doesn't define any way for you to determine this state, then you're not going to be able to do so with any degree of reliability.
It would make far more sense to invest $40-$50 in a router with quality of service controls that would allow you to prioritize your network traffic by port or by device.

Resources