OSX Photos video play/pause Applescript shortcut - macos

There is currently no keyboard shortcut to play videos in the new Mac Photos application.https://discussions.apple.com/thread/6996731?start=0&tstart=0
Rather than be frustrated in manually clicking the "play" button hundreds of times, I would like to run an Applescript to play/pause with a keyboard shortcut. I tried with no success:
tell application "photos"
"play" video
end tell
Any help advice on this would be appreciated.
Thanks

Just to update Paul's response, in Photos 2.0 (3150.4.120), you need to hit TAB twice, then spacebar will start playback. Similarly you need to hit TAB twice once you are done playing it, and then (and only then) spacebar will take you back to the grid view of your thumbnails.

If you do not mind a slightly time-consuming workaround, you can enslave Automator to create a Service that will subsequently respond to a keyboard shortcut. You can find the instructions here.

Since your goal is to use the keyboard, there's a better option for you. After selecting a video with cursor keys or mouse, press space to maximize the video, and then press TAB. Then space will start the video playing, left and right arrows will move frame-by-frame. Hitting TAB again will allow the space bar to return the video to its normal size.
Apparently this changes the focus to the pane with the movie so that space can start it, or perhaps it changes focus to the movie controls? IMHO, whatever it does, it's not very intuitive and badly needs to documented and preferably fixed.
More info at:
https://discussions.apple.com/thread/6996731?start=0&

2020 / Photos 6.0:
You can now toggle playback with Option + Space
or do it manually via "Image" -> "Start Playback".
Autoplay Script
repeat while application "Photos" is running
delay 1
try
tell application "System Events"
tell process "Photos"
click menu item "Start Playback" of menu "Image" of menu bar 1
end tell
end tell
end try
end repeat
Save it as autoplay.scpt and run it with osascript autoplay.scpt.
The script exits when you close the Photos application.

Related

Allow JavaScript from Apple Events in Safari through Terminal Mac

I'm writing a program that executes do javascript in Safari. The only problem is that I'm trying to make the app give its self permission to do it. I'm trying to locate the file that handles the Safari developer preferences so that I can do this. Does anyone have any idea where this might be or how to change these settings?
It's in Safari's preferences plist at ~/Library/Preferences/com.apple.Safari.plist. The key you want is AllowJavaScriptFromAppleEvents. You can set it using defaults:
#to turn it on
defaults write -app Safari AllowJavaScriptFromAppleEvents 1
#to turn it off
defaults write -app Safari AllowJavaScriptFromAppleEvents 0
The virtual keyboard thing did not work for me. As StarPlayr at apple's develepoer forum has found out the problem is in something else.
For me problem occurred when i tried to do that on remote mac.
For some people plugging in a keyboard and mouse to the Server allowed to turn on JavaScript Apple Events in Safari and set the password.
However, for me that wasn't an option, so the next best thing is use an accessbility scripting feature and have the machine think a user is doing the clicks, allowing you to set the password:
-- The delays can be shorter, coordinates may vary
-- Best way to get the coordinates is with Apple screen capture (command-shift-3) from upper right to lower left (the coordinates will be shown)
-- if one spends the time, the click events can be converted to Accessibility AppleScript objects by capturing them as variables, or checking the events and using the events instead of the click coordinates
tell application "System Events"
tell application "Safari"
activate
end tell
delay 1
-- click develop menu (make sure its on first)
click at {430, 12}
delay 1
-- click Allow Javascript menu from Apple Events
click at {615, 615}
delay 1
-- Click the Allow Button
click at {1010, 386}
end tell

OSX: Lock the screen programmatically

I'm looking for a way to lock the user screen programmatically without putting the Mac asleep.
Right now, i'm able to trigger the lock screen with the kAESleep event but it's more a hack and it put the computer asleep.
Is it possible ?
Thanks
Configure the screensaver to require a password immediately after it starts, then start the screensaver programmatically. I have it programmed to a keyboard shortcut to help my Windows folks transition to using real computers ;).
The following AppleScript will do it for you. Note that because of security limitations of OSX, AppleScript pauses for five seconds before it executes an UI function, so it takes a small while to function. I'm using Quicksilver to bind it to a hotkey.
(As a bonus, this script will also pause a couple of your music players. Feel free to remove those lines.)
#
# Tell our noisy programs to shut up
#
tell application "Spotify"
pause
end tell
tell application "iTunes"
pause
end tell
#
# Lock up the screen without going to sleep. Needs that Keychain Access
# is set up properly.
#
tell application "System Events" to tell process "SystemUIServer" to click (first menu item of menu 1 of ((click (first menu bar item whose description is "Keychain menu extra")) of menu bar 1) whose title is "Lock Screen")
You will need to set up Keychain Access so that it has the lock icon on screen though.
I was successfully able to lock the screen on macOS in python with the following
import ctypes, ctypes.util
login = ctypes.CDLL( '/System/Library/PrivateFrameworks/login.framework/login' )
login.SACLockScreenImmediate()
I discovered this by scarce information on the Internet and trial-and-error. As far as I know, Apple doesn't document the SACLockScreenImmediate() function at all.
If anyone can find the official reference documentation for the "Login Framework" library, please drop it in the comments :)
Source
The is used in the BusKill app, which locks the screen when a magnetic breakaway connection in a USB Dead Man Switch is severed:
https://github.com/BusKill/buskill-app/tree/master/src/packages/buskill

Applescript to click Done when dictating (Mac)

I have a script that opens "Dictation & Speech", activates it (turns Dictation: On) and runs a shortcut to start dictating (System Events keystrokes Command D). All this works fine, but what I want to do is for it to click done (or the return key) after speech is not heard. I tried someone's answer from StackOverflow.
Here is the link: Applescript to automatically close dialog box after input?
But when it's done editing and the return key is pressed, there is no text entered. Maybe the return key is not pressed, and it just quits the dictation?
EDIT: After some research, I found out that the Dictation Window (or tiny Microphone Popup) will close if clicked anywhere except Done. So I am guessing when the Keystroke Return happens, it is on a different window, so I was wondering how would I get the current window/pop-up then click the Done key in it?
The Done button is button 1 of process "DictationIM":
tell application "System Events" to tell process "DictationIM"
click button 1
end tell
I don't understand why you are running a script or why you are not happy with it.
I have Dictation and Speech turned on in System Preferences with the shortcut "Press Left Command Key Twice". (You can set your own custom shortcut.) When I press the command key twice then the dictation service activates. I can terminate the dictation service by pressing the return key and the text of whatever I was dictating is inserted wherever the cursor is. This sounds like exactly what you want, so why do you need the script?
Try to "click" on the button with applescript like this:
tell application "System Events"
set theProcess to first process whose frontmost is true
tell theProcess
click button "Done" of window 1
end tell
end tell
You only have to repeat the command-D keystroke.
thats all. you are able to start dictation with command-D so you are able to exit dictation with command-D

How can I automate application installations that have dialog boxes that need input?

I am trying to install the downloaded application programatically, hence I am using apple script for the same since am working on Mac.
Here I've downloaded the Adobe AIR Installer app from adobe site which is in downloads folder and am trying to install the same with the below applescript code. Here when the Adobe AIR setup dialog box opens, I need to click on "I Agree" button to install the app.
With the below code am not able to click on the "I Agree" button, even i dont have Xcode Accessibility Inspecter do inspect the element. Pls guide me the script to handle the "I agree" button.
try
tell application "Finder"
activate
open application file "Adobe AIR Installer.app" of folder "Applications" of startup disk
set this_image to open this_file
delay 5
tell application "System Events"
click button "I Agree" of window "Adobe AIR Setup"
end tell
end tell
end try
You could try GUI scripting the commands with delays in-between clicks, but it's a little bit of work. Since the Adobe installer doesn't support clicking from applescript, you will need to download ExtraSuite and follow along with this previous MacScripter post.
To get pixel coordinates, start up the installer without AppleScript and type '⌘ + shift + 4' to get the crosshairs with pixel location (usually used for taking screenshots). Write down the coordinates for the first button and then hit 'esc' and move to the next button.
This is a pretty painful way to get the job done but there aren't many other alternatives that I know of.
It looks like the Adobe installers don't use Cocoa and aren't scriptable. You might have better luck with Sikuli. It's based on matching bitmaps and doesn't depend on any underlying framework.

How do you programmatically press a toolbar button with AppleScript?

I hope this isn't too obvious, but I'd like to press one of the toolbar buttons within an application by means of AppleScript.
Background: The button doesn't have any menu item or keyboard shortcut. Thus, I can't activate it by any of those methods; I need to find an AppleScript way of actually 'pressing' the button.
I am going to expand a little on what #hced said.
I agree that click button 3 of tool bar 1 of window 1 works in many cases. However from OS to OS thic can be a moving target. My example is iTunes. In iTunes 10.5.8 the airplay button is 17 where in 10.7.5 it is 25. However, in both instances it is called "AirPlay"!
So I suggest using the developer kit, and getting this code from Apple:
http://developer.apple.com/library/mac/#samplecode/UIElementInspector/Introduction/Intro.html
Run the application and whatever you mouse over will get you all the info you need to make this work in your AppleScripts:
click (every button whose description is "AirPlay") of window "iTunes" of application process "iTunes"
In this case there should only be one button with this description, but you can use the tool to discover whether this is the case. If so, limit the scope!
Hope this helps!!
Bo
Ah, figured out that one myself, too. Seems I'm on a self-commenting spree here.
This example worked for me, referencing a specific toolbar button by means of its description attribute:
tell application "System Events"
tell process "OmniFocus"
click (every button whose description is "Contexts") of tool bar 1 of window 1
end tell
end tell
I'll leave it up for any commenters with an opinion about referencing every button whose descripton is "Contexts" (as per my example), to express those down below. But I presume there can't be multiple buttons with the same description, right?
Ok, that was simpler than I thought, once I found out how. (Googling it didn't leave much help at all, but an application called Prefab UI Browser did the trick finding out how this is done).
Example:
tell application "System Events"
tell process "OmniFocus"
click button 3 of tool bar 1 of window 1
end tell
end tell
I've got a follow-up question, though:
How do you reference a toolbar button by name/description? I know for a fact that the button has a description attribute with the value: "Contexts". But again, how do I reference it in the click statement?

Resources