Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
This will trigger Safari.app Picture in Picture (PiP) without the need to "Allow JavaScript from Apple Events"
--------------------------------------------------------------------------------
# Auth: Yohanes Bandung Bondowoso
# dCre: 2021-07-11 20:18
# dMod: 2021-07-13 20:01
# Appl: System Events, PIPAgent, Safari
# Task: Toggle Picture in Picture (PiP) on and off
# Libs: None
# Osax: None
# Aojc: True
# Tags: @Applescript, @Script, @System_Events, @PictureInPicture, @PiP, @Safari
# Vers: 1.04
--------------------------------------------------------------------------------
try
if application "PIPAgent" is running then
tell application "System Events"
click button 3 of window "PIP" of application process "PIPAgent"
end tell
else
error "Can’t get application process"
end if
on error pipErrMsg
if pipErrMsg contains "Can’t get application process" or pipErrMsg contains "Can’t get window" then
try
tell application "Safari"
set docItem to first item of documents
set tabName to name of docItem
end tell
tell application "System Events" to tell application process "Safari"
tell window tabName
try
tell button 3 of group 4 of toolbar 1
perform action "AXShowMenu"
end tell
click menu item "Enter Picture in Picture" of menu 1 of group 4 of toolbar 1
on error toolbarErrMsg
return "Error Level::Toggle PiP:" & space & toolbarErrMsg
end try
end tell
end tell
do shell script "killall System\\ Events"
on error safariErrMsg
return "Error Level::Safari:" & space & safariErrMsg
end try
else
return "Error Level::PIP Agent:" & space & pipErrMsg
end if
end try
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment