I have an AppleScript that nicely collates information and creates an email message with attachments.
I cannot find a way for the script to set the message format to "Plain Text" which is required by the receiving inbox (rather than the default "Rich Text").
Is there an AppleScript way (or trick) to setting the message format to "Plain Text"?
I found this question while trying to solve exactly this problem. Eventually I came up with the following solution:
tell application "Mail"
set newMessage to make new outgoing message
with properties {visible:true,
subject:"message title",
sender:"sender#from.address",
content:mailBody}
tell newMessage
make new to recipient with properties {address:"mail#recipient.com"}
end tell
activate
end tell
tell application "System Events"
click menu item "Make Plain Text" of ((process "Mail")'s (menu bar 1)'s (menu bar item "Format")'s (menu 1))
end tell
Hopefully someone will find this useful, I know I would have several hours ago!
This is tested on Mail versions 7.3 and 12.4. Newer, or older, versions might need different menu titles.
Before the section in your script that creates a message, add this line.
tell application "Mail" to set default message format to plain text
At the very end of the script add this to reset the value
tell application "Mail" to set default message format to rich text
You can use keyboard shortcut ⌘shiftt also to change the message format to plain text:
tell application "System Events" to keystroke "t" using {command down, shift down}
Tested in Mail version 12.4
Sad to say that Apple’s implementation is so broken that there is currently no way around it. You will have to suffer through it or find a way to send your mails through a commandline.
It got worse in macOS 13. Trying to have System Events send a keyboard shortcut will result in a permission error, even though all permissions are granted in System Settings.
Related
I tried below code
activate application "Adobe Reader"
tell application "System Events"
tell process "Adobe Reader"
set currentFile to active Document
end tell
end tell
But I couldn't get active document name, I tried this code by already with opened document. I don't even find any dictionary for adobe reader in script Editor. Any suggestions will be much appreciated
The Adobe Reader app does not have an AppleScript dictionary file within it's application bundle and as such is not AppleScript scriptable beyond a very limited number of Standard Suite commands and UI Scripting its menu bar with System Events.
If you just want to get the name of the document that has focus, then the following example AppleScript code can do that:
if running of application id "com.adobe.Reader" then
try
tell application "System Events" to ¬
set docName to ¬
(get name of every menu item of menu 1 of menu bar item ¬
"Window" of menu bar 1 of application process ¬
"Acrobat Reader" whose value of ¬
attribute "AXMenuItemMarkChar" = "✓") as string
return docName
end try
end if
The use of return docName was for testing purposes and can be removed as appropriate. Additionally, it was not necessary for Adobe Reader to have focus and its window could even be minimized, the script still retrieved the name as shown on its Window menu.
Note: This was tested on macOS High Sierra using US English and Adobe Acrobat Reader DC (Continuous Release | Version 2019.021.20058.) and works as is. Adjustments may be needed for other languages and or different versions of Adobe Reader.
Note: The example AppleScript code is just that and 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.
Here are a few AppleScript options that may work for you. I don’t have Adobe Reader so I am not able to test the code.
tell application "Adobe Reader" to set currentFile to name of document 1
If that doesn’t work for you, you can try this…
tell application "Adobe Reader" to activate
repeat until application ""Adobe Reader" is frontmost
delay 0.1
end repeat
tell application (path to frontmost application as text) to set currentFile to name of document 1
I have a collection of Ableton Live files (extension ".als") that I need to cycle through while playing a show. I'd like to dedicate a keyboard shortcut to launch each one, and had intended to use AppleScript for this.
The issue is that each file gets changed as I go through the process of playing the associated song, so that when I press the keyboard shortcut to launch the .als associated with the next song in my set, Ableton opens the "Save changes before closing?" dialog box (at which point what I want to do is select "Don't Save").
Simply pressing command + D at this point will do the trick, but I'd really like to automate this keypress. I can't seem to figure out how to get applescript to do this. I'm an applescript noob, and clicking the "Open Dictionary" option in AS seems to show that Ableton is not officially a scriptable app.
Any thoughts on this? Here's an example of an AppleScript I've been trying. This starts the process of opening the next .als in my set list, but won't click the "Don't Save" button.
tell application "Finder"
activate
open document file "Song 1.als" of folder "Desktop" of folder "User" of folder "Users" of startup disk
end tell
tell application "System Events"
keystroke "d" using command down
end tell
Interesting!
Finally came across tips that made it work:
Add both the Script Editor and Ableton Live to the Accessibility API:
System Preferences > Security & Privacy > Privacy...
Ignore application responses to continue the script during dialog.
LiveLoader.scpt:
-- open file
ignoring application responses -- don't wait for user input
tell application "Ableton Live 9 Suite" to open "Users:username:Desktop:LiveSet Project:LiveSet.als"
end ignoring
-- use delay if needed
-- delay 0.5
-- skip saving file
tell application "System Events"
set frontmost of process "Live" to true
key code 123 -- left
key code 123 -- left
keystroke return -- enter
end tell
Note:
Consider possible security impact.
Perhaps simply disable apps in Privacy List after use. (Could be scripted ;)
Can now also send mouse clicks, for more creativeness. :)
I know this is old. but in the interest of helping others who might find themselves here... heres what i have done.
use a program call Qlab. the free version will be fine.
make an applescript Cue. go to the 'trigger' tab. select midi trigger. hit the midi key you would like to assign the command too. this cue will now launch when it receives this midi note - even when running in the background.
go to the 'script' tab. copy and paste the script below.
you can make the relevant adjustments for each song. Basically each key will close all current ableton files without saving - as requested. and then launch a specific live set. which ever one you have assigned. in this case, the song 'Less Than Nothing'
the code...
tell application "System Events"
set frontmost of process "Live" to true
keystroke "q" using command down
tell application "System Events" to keystroke (ASCII character 28) --left arrow
tell application "System Events" to keystroke (ASCII character 28) --left arrow
keystroke return
end tell
delay 2.0
do shell script "open '/Users/CamMac/Desktop/Less Than Nothing 2 .als' "
I'm trying to configure my Mac so that I can use my voice to control page turning (e.g. while exercising).
Based on a combination of http://alvinalexander.com/apple/mac-voice-speech-recognition-software-commands-custom and Typing with Applescript I created a file called Next Page.scpt in the directory of speakable items with the contents:
tell application "System Events" to tell process "Kindle" to keystroke " "
The software is recognizing my "Next Page" voice prompt, as it echo's back that command name, but I'm not getting the effect of typing a space. Nothing is apparently happening and I have no idea how to debug. Prior to issuing this command, I've switched to Kindle itself.
As an aside, the rest of the files in the speakable events directory are XML files and not simple applescript files. I have not tried figuring out and adopting the XML format.
You could try
tell application "Kindle"
activate
tell application "System Events" to keystroke " "
end tell
or sometimes keystroke can be effectively replaced with key code, in your case SPACE would be: key code 49
List of other key codes here
You can use the Speakable Items capability to define the keyboard commands, as follows (extracted from http://support.apple.com/kb/PH14380):
This same document also discusses how to create commands for other functions, including running scripts.
This works for me, using down arrow:
tell application "System Events" to tell process "Kindle" to key code 125
(key code 124, which is right arrow, should also work; back (back a page) could be up arrow (key code 126) or left arrow (123)
I've been playing around with various UNIX commands and came across this one to display a dialog:
osascript -e 'tell app "System Events" to display dialog "Hello World"'
I'd like to change the position of the dialog. For most commands, I can just use man command to figure out how to do something for that command. However, man osascript doesn't tell me how I can change the position of the dialog box. How can I modify the command to put the dialog in a different place?
First, to get help with applescript just open the AppleScript Editor application and look under the help menu. The applescript language guide is in there and other tools. Also under the file menu is "Open Dictionary" which will show you the specific commands for applications.
To see the information about display dialog, open the dictionary of StandardAdditions and use the search field to find the command.
To answer your question, no, you cannot specify the location of a "display dialog" window. There are no parameters in that command for positioning the window, as you'll see in the dictionary. In addition, no other commands will help you either because you can't issue other commands while the dialog window is open because the code pauses while the window is open waiting for a response from the dialog window (like when you press the OK button).
If you need to set the position of a window to display information to a user then you'll need to use some other dialog tool other than "display dialog". You could create your own window in cocoa or google for some alternatives like cocoaDialog, SKProgressBar, and Notification Center.
There is a round-about way to go about this, which may be useful in some scenarios. Try the following:
on displayMessage(msg)
tell application "Finder"
activate
set c to (count windows)
ignoring application responses
display dialog msg with icon note
end ignoring
end tell
tell application "System Events"
tell application process "Finder"
repeat until ((count windows) > c)
delay 0.2
end repeat
set position of window 1 to {0, 22}
end tell
end tell
end displayMessage
displayMessage("I'm over here!")
Credit for this little script goes to a post here.
In implimenting this myself, I found it was limited to whether or not the application that is being called (Finder, in the example) supports the count window command (or whether it has API support at all).
I realise I've dragged up a question from 2013. I am posting this answer here in case it is of use to the OP or, more likely, someone else with a similar question.
I have an AppleScript that nicely collates information and creates an email message with attachments.
I cannot find a way for the script to set the message format to "Plain Text" which is required by the receiving inbox (rather than the default "Rich Text").
Is there an AppleScript way (or trick) to setting the message format to "Plain Text"?
I found this question while trying to solve exactly this problem. Eventually I came up with the following solution:
tell application "Mail"
set newMessage to make new outgoing message
with properties {visible:true,
subject:"message title",
sender:"sender#from.address",
content:mailBody}
tell newMessage
make new to recipient with properties {address:"mail#recipient.com"}
end tell
activate
end tell
tell application "System Events"
click menu item "Make Plain Text" of ((process "Mail")'s (menu bar 1)'s (menu bar item "Format")'s (menu 1))
end tell
Hopefully someone will find this useful, I know I would have several hours ago!
This is tested on Mail versions 7.3 and 12.4. Newer, or older, versions might need different menu titles.
Before the section in your script that creates a message, add this line.
tell application "Mail" to set default message format to plain text
At the very end of the script add this to reset the value
tell application "Mail" to set default message format to rich text
You can use keyboard shortcut ⌘shiftt also to change the message format to plain text:
tell application "System Events" to keystroke "t" using {command down, shift down}
Tested in Mail version 12.4
Sad to say that Apple’s implementation is so broken that there is currently no way around it. You will have to suffer through it or find a way to send your mails through a commandline.
It got worse in macOS 13. Trying to have System Events send a keyboard shortcut will result in a permission error, even though all permissions are granted in System Settings.