How can I open *.eml-files in compose mode for Mac OS X Mail? For Outlook "X-Unsent: 1" seems to do the trick, but does not work for Mac OS X Mail still opens in Read-Only.
This is a super old question, but obviously has received a fair number of views. My curiosity piqued, I figured I'd take a look. After some fiddling around, I found you can add the following header to make a message editable:
X-Uniform-Type-Identifier: com.apple.mail-draft
Add that to your otherwise RFC-2822 compliant message, give it a .eml or .emlx extension, and when you double-click it'll open as an editable message in Mail.app.
Have fun.
In macOS Catalina 10.15.7, running the following commands from a Python script worked to open a new message and then switch to compose mode, but there is a race condition between opening the message and putting it in Compose mode if the osascript command runs too quickly. The sleep command is there to prevent the race, but you may need to adjust the length of sleep time to compensate for this. (I also used time.sleep(1) in Python instead of the shell command.)
/usr/bin/open -a Mail path/to/tmp/message.eml
/bin/sleep 1
/usr/bin/osascript -e 'tell application "System Events" to tell application process "Mail"' -e 'keystroke "d" using {command down, shift down}' -e 'end tell'
Note that you must grant access to Terminal to control other apps, or the osascript command will fail with an error. To do that, open System Preferences, click on the Security & Privacy item, click Accessibility from the left menu, click the lock icon to unlock the menu, then check the Terminal item (or add it if not listed) in the "Allow apps to control your computer." list.
Also note that the Cmd-Shift-D keyboard shortcut sends a message that is already in compose mode, so if someone is using Mail at the time the script runs, and they have a separate compose window in the foreground, then the script could send that message instead of putting the new message in compose mode.
Related
I setup an Applescript quick action in Automator to leave a zoom meeting. No matter what application is in the forefront, I would like to check if zoom is running and if so, leave the meeting.
set appName to "Zoom.us"
if application appName is running then
tell application id (id of application appName)
activate
end tell
tell application "System Events"
keystroke "w" using command down
keystroke tab
keystroke return
end tell
end if
It works! The problem is, if I'm in a zoom meeting and another app is in the forefront, I have to get permission from system preferences for that app to access system preferences. Like if I'm in Chrome, I have to allow Chrome to send keystrokes. Then, Chrome will always work.
I have to do this for every single possible app. Is there a way to get keystrokes in there without going thru this security stuff in Big Sur? I don't mind bringing up zoom to the forefront.
There are issues at play here with using global keyboard shortcuts with Automator workflows saved as a Service/Quick Action.
The keyboard shortcut assigned to the Service/Quick Action needs to not conflict with a default keyboard shortcut for whichever application is frontmost at the time it's pressed, otherwise there may be unwanted behavior.
Every application that is frontmost when the keyboard shortcut is pressed on a Service/Quick Action using UI Scripting in the Run AppleScript action will need to have accessibility privileges granted for it (as you've already found this out and hence the question).
To workaround the accessibility privileges issue, here are three methods to achieve the goal that come to mind.
The first, my preferred method for running AppleScript scripts with a keyboard shortcut, is to use a third-party application named FastScripts, as it would not need to have every application that's frontmost, that hasn't yet been granted privileges, to be granted accessibility privileges to run the AppleScript code shown in your question. I'd imagine other similar type third-party applications that allowed assigning keyboard shortcuts and running a script would bypass the issue too, but have only tested the aforementioned.
The second method, can be done with Automator as a Service/Quick Action using a Run Shell Script action, then assigned a keyboard shortcut and will work without having to give accessibility privileges to the application that is frontmost when the keyboard shortcut is pressed.
The third method, can be done with Automator as a Service/Quick Action using a Run AppleScript action if changing a zoom.us default preference under zoom.us > Preferences… > General by unchecking [] Ask me to confirm when I leave a meeting, then when assigned a keyboard shortcut and will work without having to give accessibility privileges to the application that is frontmost when the keyboard shortcut is pressed.
All testing was done under macOS Big Sur using zoom.us, (Version: 5.4.7 (59780.1220)) with my Language & Region setting in System Preferences set to US English using the various methods presented.
Method 1
The first method using the following example AppleScript code, show further below, and FastScripts with the keyboard shortcut ⌃⌥⌘W assigned and works for me as coded.
In System Preferences > Security & Privacy > Privacy > Accessibility I had the following added and checked:
FastScripts
System Events
Then with zoom.us running and several other applications which were frontmost when the keyboard shortcut was pressed I did not have to grant accessibility privileges to those other applications, zoom.us was brought to the front and closed.
Example AppleScript code:
if application "zoom.us" is running then
tell application "zoom.us" to activate
delay 0.5
tell application "System Events" to ¬
tell application process "zoom.us"
keystroke "w" using command down
delay 0.5
key code 36
end tell
end if
Note for testing purposes, after testing Method 1, I quit FastScripts as it would have been triggered by the same keyboard shortcut which was assigned when testing the next two methods.
FastScripts can be run as a free app, up to 10 keyboard shortcuts, or upgraded for $24.95 USD to unlock unlimited keyboard shortcuts. I have no affiliation with Red Sweater Software, LLC, other then as a user of FastScripts.
Method 2
The second method was tested using Automator and a Service/Quick Action with setting Workflow receives [no input] in [any application] using a Run Shell Script action with its default settings, and the following example shell script code is all that was used:
[[ -z $(pgrep -x 'zoom.us') ]] || pkill −x 'zoom.us'
In System Preferences > Keyboard > Shortcuts > Services I assigned it the keyboard shortcut: ⌃⌥⌘W
Then with zoom.us running and several other applications which were frontmost when the keyboard shortcut was pressed I did not have to grant accessibility privileges to those other applications, zoom.us was closed.
Method 3
The third method was tested using Automator and a Service/Quick Action with setting Workflow receives [no input] in [any application] using a Run AppleScript action replacing its default code with just the following example AppleScript code:
tell application "zoom.us" to quit
In System Preferences > Keyboard > Shortcuts > Services I assigned it the keyboard shortcut ⌃⌥⌘W after removing it from the Service/Quick Action created in Method 2.
Then with zoom.us running and several other applications which were frontmost when the keyboard shortcut was pressed I did not have to grant give accessibility privileges to those other applications, zoom.us was closed.
This of course works because the [] Ask me to confirm when I leave a meeting preference was unchecked in the preferences for zoom.us.
To recap, if you do not mind changing the mentioned default preference in zoom.us, then Method 3 is probably the easiest and best way to resolve your issue as it allows for a graceful quit over Method 2, does not require any third-party application, and does not require any accessibility privileges be granted or zoom.us being frontmost, it just works.
I mentioned the other methods first as method one addresses the UI Scripting issue with Automator and a Service/Quick Action, and method two works with the default settings in zoom.us.
I am writing a simple (haha) bash script that will send keystrokes to an app. It uses the osacript command line to do this, like so:
osascript <<EOSCRIPT
activate application "Mini vMac"
tell application "System Events" to keystroke "a"
EOSCRIPT
When this script is run, it does bring the app to the front, but then I get the following error:
77:90: execution error: System Events got an error: osascript is not allowed to send keystrokes. (1002)
I have tried whitelisting /usr/bin/osascript but other search results suggest that it doesn't want this. The bash script itself is unselectable (which doesn't necessarily mean that it doesn't want it, just that I can't check).
How do I force it to let me run this? 10.14.6 Mojave if it matters.
I don't have Mini vMac; however, on macOS Mojave 10.14.6, the following example AppleScript code should work to send a keystroke, assuming a Mini vMac window is open, and Terminal is given permission in: System Preferences > Security & Privacy > Privacy > Accessibility
osascript -e 'activate app **Mini vMac**' -e 'delay 0.25' -e 'tell app "System Events" to keystroke "a"'
Note: The example AppleScript code is just that and does not contain any 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.
I'm trying to write a plugin for emacs that displays a notification using OS X's native notification display.
I've run into terminal-notifier which works, but it's a dependency that doesn't work on every mac. Plus the user should be made aware that they need to install the package.
What I want to do is call a process osascript -e and make it display the notification. The problem is, the only way to change its icon is from an external bundle. Is there any way to make osascript -e display what I want.
starting sudo osascript seems to do that, but it seems to be bad design and I also need to find a way to pass the root password every single time.
You cannot. This is simply not a macOS feature exposed to AppleScript.
If you need a custom icon, consider using a pop-up "dialog" rather than a Notification Center pop-up. With timeouts and buttons you can recreate much of the functionality, though not the integration nor aesthetics.
In `display dialog', if you wish to use the standard icons: 0, 1, or 2 (stop, note, or caution), perhaps don't have osascript be the program that displays the icon. Finder, for example:
osascript -e 'tell application "Finder"' -e 'activate' -e 'display dialog \
"this is the note icon." with icon note' -e 'end tell'
or without the tell application… you may use an icon of your choice by referencing it directly, e.g. the Terminal app's icon:
osascript -e 'display dialog "Terminal icon" with icon alias \
"Macintosh HD:Applications:Utilities:Terminal.app:Contents:Resources:Terminal.icns"'
I'm not sure what you mean by, "the only way to change its icon is from an external bundle. Is there any way to make osascript -e display what I want." What, precisely, do you want? What have you tried?
Here's the display dialog section from Apple's documentation.
Actually, this is possible.
Just save your script as an application and then switch the applet.icns file within the application's Contents/Resources folder for the icon you want.
Any notifications sent from your script will use that icon.
Unfortunately, the "display notification" documentation shows that you can't:
display notification
Posts a notification using the Notification Center, containing a title, subtitle, and explanation, and optionally playing a sound.
Syntax
display notification – text, required
with title – text, optional
subtitle – text, optional
sound name – text, optional
(Even using the tell application "..." trick from https://stackoverflow.com/a/49079025/3528 leaves you with the default notification icon.)
The reason why terminal-notifier can is because it's using the Notification Center APIs directly which, as far as I can tell, osascript doesn't present an interface to.
Best solution for was to:
Create an AppleScript app and replace the applet.icns file, inside the /Contents/Resources folder, with your icon file
create a handler:
on notify(vTxt, vTtl)
display notification vTxt with title vTtl
end notify
Don't forget to create an 'on idle' handler as well:
on idle
return 10
end idle
Save the app as "Stay open"
Run the app
now you can call the handler from another app or process and the notification will display with the icon of your app. Call it from the command line :
osascript -e 'tell application "MyApp" to notify("Message", "TITLE")'
If I open /System/Library/CoreServices/loginwindow.app/Contents/MacOS/loginwindow I'm able to interact with the login window by running this script:
osascript -e <<EOF 'tell application "System Events"
tell process "SecurityAgent"
set value of text field 1 of window "Login" to "password"
click button 5 of window 1
end tell
end tell'
EOF
I'm also able to use my script above on the real login window (/System/Library/CoreServices/Menu\ Extras/User.menu/Contents/Resources/CGSession -suspend) when SSH'd in from another computer.
Frustratingly, however, I cannot get it to work on the real login window when run from any locally-run script.
EDIT (some context): The end goal is to have a script that runs at startup which unlocks OS X after some criteria are met (changes to a log file, etc.). In this case, I don't care about security. The hangup is being able to enter the password and "return" key to the lock screen/login window from a script, i.e. programatically.
Am I missing something? Let me know if there's other info I should provide.
I open the Firefox application using a
open -n -a Firefox.app
When I close the window of this Firefox application, only the window closes but the application still keeps running. I know that I could use Command+Q, but I want the application to quit when I click the Close button.
I have looked up for various command line options but haven't been able to figure out a way to start the Firefox application so that when I close the last window, the application itself quits.
I know this has more to do with Mac treating Applications and Windows differently. So how do I approach this problem?
Are there any other command line arguments that I can pass to make this work?
You can use osascript to send a quit AppleEvent to the application using AppleScript
$ osascript -e 'tell application "Firefox" to quit'