Skip to content

Instantly share code, notes, and snippets.

@adriannier
Last active October 6, 2015 18:08
Show Gist options
  • Save adriannier/482537c6f7f505f1e8f4 to your computer and use it in GitHub Desktop.
Save adriannier/482537c6f7f505f1e8f4 to your computer and use it in GitHub Desktop.
on run
(*
iOS QuickTime Viewer
Version 0.2
AppleScript to assist a user to view an iOS devices’ screen
on a Mac using QuickTime Player
# Compatiblity
- OS X 10.9 and older: Feature not available in QuickTime Player
- OS X 10.10.5: Successfully tested
- OS X 10.11.0: Cannot get connected devices. To System Events the device
selection menu button in QuickTime Player is just an
ordinary button. When clicked execution is blocked.
No way to get at the device menu. Not even on
second thread.
# Version history
- 2015-10-06 v0.2: Now waiting for Movie Recording window to appear
- 2015-10-06 v0.1: Initial release
*)
try
-- Find all camera devices other than FaceTime cameras
set cameraDevices to findCameraDevices()
if (count of cameraDevices) > 1 then
-- If more than one device is found, allow the user to select which device to view
set cameraDevice to letUserChooseCameraDevice(cameraDevices)
else
-- Default to the one and only device that was found except the FaceTime camera
set cameraDevice to item 1 of cameraDevices
end if
-- Set the camera device
setQuickTimePlayerCameraDevice(cameraDevice)
on error eMsg number eNum
if eNum = -128 then return -- User canceled
activate
display alert "Something went wrong" message eMsg
end try
end run
on findCameraDevices()
repeat
-- Make sure we start with a fresh QuickTime Player state
if quickTimePlayerRunning() then tell application "QuickTime Player" to quit
-- Ask the user to connect the device
displayConnectionMessage()
-- Show the movie recording window
showMovieRecorder()
-- Poll for camera devices (ignoring FaceTime camera)
repeat 5 times
set cameraDevices to quickTimePlayerCameraDevices() -- Search for devices
if cameraDevices is not {} then return cameraDevices -- Devices found, return them
delay 1
end repeat
end repeat
end findCameraDevices
on displayConnectionMessage()
activate
set userChoice to button returned of (display alert "Connect device" message "Please connect your iPhone or iPad to your Mac." buttons {"Cancel", "Continue"} cancel button 1 default button 2)
end displayConnectionMessage
on showMovieRecorder()
tell application "QuickTime Player" to activate
tell application "System Events"
tell process "QuickTime Player"
set frontmost to true
tell menu bar 1
click menu bar item 3 -- Open File menu
tell menu bar item 3 to tell menu 1 to click menu item 1 -- Select the first command from the File menu
end tell
repeat 100 times
if exists window "Movie Recording" then return
delay 0.1
end repeat
if exists window "Movie Recording" is false then error "Could not open Movie Recording window"
end tell
end tell
end showMovieRecorder
on quickTimePlayerRunning()
tell application "System Events" to return exists process "QuickTime Player"
end quickTimePlayerRunning
on quickTimePlayerCameraDevices()
try
tell application "System Events"
tell process "QuickTime Player"
set frontmost to true
tell window 1
click menu button 1
tell menu button 1
tell menu 1
-- The menu will hopefully start with the title *Camera* followed by the first available device
set availableDevices to {}
repeat with i from 2 to count of UI elements
set deviceName to name of item i of UI elements
if deviceName is missing value then exit repeat -- This is the blank line; stop looking for any more devices
if deviceName does not contain "FaceTime" then -- This is not a FaceTime device
set end of availableDevices to deviceName
end if
end repeat
end tell
end tell
key code 53 -- Close the popup menu first
return availableDevices
end tell
end tell
end tell
on error eMsg number eNum
error "quickTimePlayerCameraDevices(): " & eMsg number eNum
end try
end quickTimePlayerCameraDevices
on setQuickTimePlayerCameraDevice(aDevice)
try
tell application "System Events"
tell process "QuickTime Player"
set frontmost to true
tell window 1
click menu button 1
delay 0.3
tell menu button 1
tell menu 1 to click menu item aDevice
end tell
end tell
end tell
end tell
on error eMsg number eNum
error "setQuickTimePlayerCameraDevice(): " & eMsg number eNum
end try
end setQuickTimePlayerCameraDevice
on letUserChooseCameraDevice(cameraDevices)
activate
set chosenDevice to choose from list cameraDevices with prompt "Please select a device:" default items {item 1 of cameraDevices}
if chosenDevice is false then error "User canceled." number -128
return item 1 of chosenDevice
end letUserChooseCameraDevice
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment