Forked from danielrotaermel/toggle-pip.applescript
Created
February 13, 2023 16:47
-
-
Save RedcoatAsher/95a491d25d61fb33cd82b14337be3506 to your computer and use it in GitHub Desktop.
Applescript to toggle picture-in-picture globally - I assign this to a keyboard shortcut with better touch tool (cmd+ctrl+p). Make sure "Developer > Allow JavaScript from Apple Events" is enabled in Safari. Download BTT preset -> https://share.folivora.ai/sharedPreset/caa1198d-36dd-4aeb-89de-142bf39cc336
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(* | |
Description: Applescript to open/close picture-in-picture in Safari | |
Supported Applications: Safari, IINA | |
Author: Daniel Rotärmel | |
Source: https://gist.github.com/danielrotaermel/201f549d5755ea886eb78bb660133722 | |
Instructions: | |
Assign this to a global keyboard shortcut with better touch tool / hammerspoon etc. - suggested: cmd+ctrl+p. | |
Make sure "Developer > Allow JavaScript from Apple Events" is enabled in Safari. | |
Download the BTT preset here -> https://share.folivora.ai/sharedPreset/caa1198d-36dd-4aeb-89de-142bf39cc336 | |
*) | |
set jsStartPip to "document.querySelector('video').webkitSetPresentationMode('picture-in-picture')" | |
set jsVideoExistsAndInline to "if (document.querySelector('video')!== null) document.querySelector('video').webkitPresentationMode === 'inline' ? true : false; else false;" | |
on closePip() | |
tell application "System Events" | |
if exists (window 1 of process "PIPAgent") then | |
-- display dialog "Found window 1" | |
-- get every UI element of (window 1 of process "PIPAgent") | |
click button 3 of (window 1 of process "PIPAgent") | |
-- click button 1 of (window 1 of process "PIPAgent") | |
end if | |
end tell | |
end closePip | |
-- tell application "Safari" to activate | |
-- tell application "IINA" to activate | |
tell application "System Events" to set frontApp to name of first process whose frontmost is true | |
if frontApp = "Safari" then | |
tell application "Safari" | |
set isVideoInline to do JavaScript jsVideoExistsAndInline in front document | |
if isVideoInline then | |
do JavaScript jsStartPip in front document | |
else | |
tell application "System Events" | |
if exists (window 1 of process "PIPAgent") then | |
-- display dialog "Found window 1" | |
-- get every UI element of (window 1 of process "PIPAgent") | |
click button 3 of (window 1 of process "PIPAgent") | |
-- click button 1 of (window 1 of process "PIPAgent") | |
end if | |
end tell | |
end if | |
end tell | |
else if (frontApp = "IINA") then | |
tell application "System Events" | |
tell process "IINA" | |
try | |
click menu item "Enter Picture-in-Picture" of menu "Video" of menu bar 1 | |
on error | |
click menu item "Exit Picture-in-Picture" of menu "Video" of menu bar 1 | |
end try | |
end tell | |
end tell | |
else | |
closePip() | |
end if |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment