Scripting OS X GUI login window - macos

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.

Related

What app needs to be whitelisted in OSX's Security&Privacy Accessibility list, so that my bash script can send keystrokes?

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.

AppleScript, why does first "display alert" work but then cause AppleScript icon to bounce, waiting for me to click it until I get another dialog?

NOTE: I do not have Admin privileges, and cannot use things that require assistance or accessibility or whatever it is!
I'm basically trying to write an execution of a script that sets up my macOS Desktop by opening apps, positioning them etc
Part of that is allowing me to click continue on a dialogue after doing certain things I have to do manually as a result of the limitation (positioning a few apps for example).
I can successfully have the first alert pop up, and when I press OK, it executes the task, but then the 2nd alert is hidden behind AppleScript and will not pop up until I click AppleScript. Anyone know why?
Heres what I have, and please keep in mind I do not have Admin privileges, which means I can't use System Events:
tell application "Script Editor"
set miniaturized of window 1 to true
end tell
display alert "App is opening. Make sure you log-out of Apple Connect!"
delay 3
tell application "Safari"
activate
make new document
set the bounds of the front window to {1077, 23, 1581, 464}
tell window 1
set URL of tab 1 to "http://appwebsite.com"
end tell
end tell
display alert "Set up App, sign into Apple then click OK to open all other Safari windows"
delay 1
tell application "Test Task App" to activate
Place an activate command before the display alert command so Script Editor has focus and the display alert command will not be hidden.
Example:
activate
display alert "Set up App, sign into Apple then click OK to open all other Safari windows"
You do not need it before the first one as Script Editor has focus with it runs the first display alert command, but once you activate Safari, the display alert command is behind Safari. So if you use an activate command before the display alert command, it brings it forward in front of, in this case, Safari.

App behavior via script different when display is asleep

I have an AppleScript that runs every morning and evening. It tells Quicken (Mac) to update all accounts.
I have to do this because Fidelity has some private funds and the only way to get daily stock prices for them is to actually update the account.
My script runs perfectly via a shell script that is executed by launchd, provided that the display is not asleep. When the display is asleep, the script runs without error, but Quicken does not update.
Note: My Mac is set to prevent the computer from sleeping when the display is asleep.
AppleScript:
tell application "Quicken"
reopen
activate
end tell
tell application "System Events"
keystroke "U" using {command down, shift down}
delay 1
set visible of application process "Quicken" to false
end tell
Command in .sh script:
/usr/bin/osascript /xxx/bin/Stocks/scripts/QuickenUpdate.scpt 2>&1
Anyone know why it won't update when the display is asleep ?
Possible other causes for the behavior ?
Thanks.

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.

How to make the Firefox application quit when the user hits its close button in Mac

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'

Resources