AppleScript keystrokes - applescript

I have a problem with the keystrokes when I use the system events in applescript application. Indeed, when I run the applescript program with keystrokes in system events, the message: "The name of the program is not allowed to send key entries" error 1002 appears. I have already authorized the AppleScript program in the security and privacy and privacy section but despite everything, the message appears. Note that this AppleScript program works well in the AppleScript format before becoming a program. I can't fix this problem.

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

Need an Applescript to close all open mail messages

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.

OS X Mail - Open *.eml-files in compose mode

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.

Can AppleScript listen for events?

I want to write a script that takes action when a document is opened on a certain application, or before an application quits, etc.
Is there a way to attach a script to an event in an application? Does AppleScript support any form of hooks at all?
If not, can I hack my way into getting what I want?
applescript only has certain "event listeners" the are folder action script that might be considered an event listener and indesign has real event listeners which I won't get into at the moment.
if you want a blanket listener for any application to quit you may find what your looking for in a Quickeys though I'm not certain of this as it has been a long time since I have messed around with quickeys.
but all and all the answer is for the most part no.
hth
Mike
EDIT more tools that may help brought by kch
FastScripts
QuickSilver
Keyboard Maestro
"Some apps, eg. iChat, have script hooks in the preferences. In iChat, the Alerts preference pane, you can set it to run a script when a certain event is triggered, like message received, file transfer request, etc." – kch

Resources