Skip to content

Instantly share code, notes, and snippets.

@ybbond
Last active September 11, 2022 02:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ybbond/47d6fce6ce500748a0cfb7841cd0c64b to your computer and use it in GitHub Desktop.
Save ybbond/47d6fce6ce500748a0cfb7841cd0c64b to your computer and use it in GitHub Desktop.
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