Set a camera in quicktime using applescript - macos

I know there is probably a very simple answer to this. I'm trying to setup an Applescript application that when activated will launch quicktime, open a new movie recording, set the camera and audio, start the recording and then end the recording and save it after 30 seconds.
I currently have a script that is doing everything except setting the camera and audio source. Any tips on how to select a certain camera and audio source using applescript?
Thanks!
Here is the code as it stands now..
--Set some Variables
--Max length of recording
set maxrec to 20 --in seconds
--Ending Message
set EndMsg to "Thank you for participating in this project. Your video has been recorded."
--Begin Loop
repeat
--Get the person's name
repeat
display dialog "What's your name?" default answer ""
set theName to (text returned of result)
if theName ≠ "" then exit repeat
end repeat
--Set the date
set theDate to current date
set y to text -4 thru -1 of ("0000" & (year of theDate))
set m to text -2 thru -1 of ("00" & ((month of theDate) as integer))
set d to text -2 thru -1 of ("00" & (day of theDate))
set h to text -2 thru -1 of ("00" & (hours of theDate))
set mm to text -2 thru -1 of ("00" & (minutes of theDate))
set dateStamp to (y & "-" & m & "-" & d & " " & h & mm as string)
--Create a folder for the person
tell application "Finder"
set p to path to movies folder from user domain
try
make new folder at p with properties {name:"Video Clips"}
end try
set p to (path to movies folder from user domain as text) & "Video Clips:"
make new folder at p with properties {name:dateStamp & " " & theName}
set thePath to result
--Establish the final name of the movie
set fileName to ((thePath as text) & dateStamp & ".mov" as string)
end tell
--Open New Recording, start and stop it
tell application "QuickTime Player"
set newMovieRecording to new movie recording
set windowID to id of first window whose name = "Movie Recording"
delay 2
tell newMovieRecording
--Set Camera to Blackmagic
set current camera of newMovieRecording to Blackmagic of video recording devices
start
end tell
set theSeconds to maxrec
repeat theSeconds times
display dialog theSeconds buttons {} giving up after 1 with title "REMAINING TIME"
set theSeconds to (theSeconds - 1)
end repeat
tell newMovieRecording
stop
end tell
--save and close the recording
set newMovieRecordingDoc to first document whose name = (get name of first window whose id = windowID)
tell newMovieRecordingDoc to save in fileName
set SavedRecordingDoc to first document whose name = (get name of first window whose id = windowID)
tell SavedRecordingDoc to close
quit
end tell
--Display "Thank You" Message
tell application "System Events" to set frontmost of process "Video Booth" to true
display dialog EndMsg giving up after 20 buttons {"Continue…"} ¬
default button 1
end repeat
Here is the error I get right now when I run it.
error "QuickTime Player got an error: Can’t make Blackmagic of every video recording device of document \"Movie Recording\" into type specifier." number -1700 from Blackmagic of every video recording device of document "Movie Recording" to specifier

use the current camera and current microphone properties of your movie recording object. Just replace item 1 with the appropriate device:
set current camera of recording to item 1 of video recording devices
set current microphone of recording to item 1 of audio recording devices

I had accomplished this by doing it manually
tell application "System Events" to tell process "QuickTime Player"
#To open dialog to show available cameras
click button 3 of window 1
#To select our device
click menu item "your_camera_name" of menu 1 of button 3 of window 1
end tell
With this the dialog will open and you will select your camera from the list...
Taken from another answer I made:
https://stackoverflow.com/a/45454785/3685973

Related

How to get Apple music artwork using AppleScript

I have an AppleScript which retrieves several properties of the current track playing on apple music but I can't retrieve the artwork
if application "Music" is running then
tell application "Music"
if player state is playing or player state is paused then
set currentTrack to current track
return {get player state} & {get artist of currentTrack} & {get name of currentTrack} & {get album of currentTrack} & {get kind of currentTrack} & {get duration of currentTrack} & {player position} & {get genre of current track} & {get id of current track}
else
return "stopped"
end if
end tell
else
return "stopped"
end if
This is very simple i just couldn't find it, so Doug made a very simple script that you can find here and that i reworked a bit it save the current track artwork to the same location as the script and name it tmp.jpg or png
tell application "Music"
try
if player state is not stopped then
set alb to (get album of current track)
tell artwork 1 of current track
if format is JPEG picture then
set imgFormat to ".jpg"
else
set imgFormat to ".png"
end if
end tell
set rawData to (get raw data of artwork 1 of current track)
else
return
end if
on error
display dialog "Problem getting track info." buttons {"OK"}
return
end try
end tell
--get current path
tell application "Finder"
set current_path to container of (path to me) as alias
end tell
--create path to save image as jpg or png
set newPath to ((current_path as text) & "tmp" & imgFormat) as text
try
--create file
tell me to set fileRef to (open for access newPath with write permission)
--overwrite existing file
write rawData to fileRef starting at 0
tell me to close access fileRef
on error m number n
log n
log m
try
tell me to close access fileRef
end try
end try

macOS Photos and AppleScript: Quicker organization method

The Goal:
In macOS Photos App, highlight/select a bulk of photos, iterate each one.
AppleScript will tell Photos to quickview the photo, ask what 'Album(s)' to add the photo to, then it will ask for a title and caption.
I'm doing this because the 'Info' panel that provides this function doesn't allow for rapid entry, and it's pretty small to work with.
So I suppose I need a couple things:
Show the picture (tell Photos to quick look it)
Identify the 'My Album' folders (eliminate the smart folders from the mix)
Prompt and Update the Title and the Caption (called name and description in the def)
Add the photo to the selected Albums.
I'm not sure if it's just me, but the AppleScript scripting on Photos is just not something I can wrap my head around very well.
Here's what I have so far, but I was hoping you can help.
tell application "Photos"
--Find All Albums
set thefullList to the name of every album of every folder
-- Need:Figure out how to strip Smart Albums out of this query
-- Set comma as the delim to separate the folders/albums
set oldDelimiters to AppleScript's text item delimiters
set AppleScript's text item delimiters to ","
set thefullList to every item of thefullList
set AppleScript's text item delimiters to oldDelimiters
-- Test the output (fails to delim, and won't display the string)
display dialog thefullList
-- Clean list of Albums
set albumNames to {thefullList}
-- Get the selected photos from Photos
set theSelection to (get selection)
-- Iterate through each photo
repeat with i in theSelection
-- Tell Photos to 'quick look'
-- Need:(no idea how to make that work yet)
-- Ask the user to choose the Album(s) this photo should go into
set theAlbumChoice to choose from list albumNames with prompt "Where should this photo go?"
-- Tell the Album that this photo is now added
set theAlbum to theAlbumChoice
add i to theAlbum
-- Ask for Title and Caption
display dialog "What's the Title of this Photo?"
set theTitle to text returned of result
display dialog "What's the Caption of this Photo?"
set theCaption to text returned of result
-- Get the Photo ID for adding MetaData
set selectionID to id of item i of theSelection
-- Set the Title and caption
set name of media item id selectionID to theTitle
set description of media item id selectionID to theCaption
end repeat
end tell
property albumNames : {}
property albumIDs : {}
tell application "Photos"
activate
my getAlbumNames(it) -- get all album names and IDs (recursively)
repeat with aPhoto in (get selection) -- process each selected photo
try -- Get the Photo ID
set selectionID to id of aPhoto
on error errorMessage
set ATID to AppleScript's text item delimiters
set AppleScript's text item delimiters to "\""
set selectionID to text item 2 of errorMessage
set AppleScript's text item delimiters to ATID
end try
spotlight media item id selectionID -- Tell Photos to 'quick look'
-- Ask the user to choose the Album(s) this photo should go into
set theAlbumChoice to choose from list albumNames with prompt "Where should this photo go?"
if theAlbumChoice is false then return
-- Find destination folder's ID and add photo to it
repeat with k from 1 to count albumNames
if (item 1 of theAlbumChoice) is (item k of albumNames) then
set theAlbumID to item k of albumIDs
exit repeat
end if
end repeat
add {media item id selectionID} to album id theAlbumID
-- Ask for Title and Caption, and set them
display dialog "What's the Title of this Photo?" default answer ""
set theTitle to text returned of result
display dialog "What's the Caption of this Photo?" default answer ""
set theCaption to text returned of result
set name of media item id selectionID to theTitle
set description of media item id selectionID to theCaption
end repeat
end tell
on getAlbumNames(aFolder) -- recursive handler
tell application "Photos"
set albumNames to albumNames & name of albums of aFolder
set albumIDs to albumIDs & id of albums of aFolder
repeat with subFolder in (get folders of aFolder)
my getAlbumNames(subFolder)
end repeat
end tell
end getAlbumNames

Why does this Applescript cause "Export Library.scpt: execution error: System Events got an error: Can’t get process "i". (-1728)"?

ƒIt's caused when run through osascript from terminal using the command line "osascript Export\ Library.scpt /Users/bryandunphy/Development/iTunesLibraryConsolidator testing.xml".
same script as my last (solved) question but posting it's entirety this time.
-- `menu_click`, by Jacob Rus, September 2006
--
-- Accepts a list of form: `{"Finder", "View", "Arrange By", "Date"}`
-- Execute the specified menu item. In this case, assuming the Finder
-- is the active application, arranging the frontmost folder by date.
on menuClick(mList)
local appName, topMenu, r
-- Validate our input
if mList's length < 3 then error "Menu list is not long enough"
-- Set these variables for clarity and brevity later on
set {appName, topMenu} to (items 1 through 2 of mList)
set r to (items 3 through (mList's length) of mList)
-- This overly-long line calls the menu_recurse function with
-- two arguments: r, and a reference to the top-level menu
tell application "System Events" to my menuClickRecurse(r, ((process appName)'s ¬
(menu bar 1)'s (menu bar item topMenu)'s (menu topMenu)))
end menuClick
on menuClickRecurse(mList, parentObject)
local f, r
-- `f` = first item, `r` = rest of items
set f to item 1 of mList
if mList's length > 1 then set r to (items 2 through (mList's length) of mList)
-- either actually click the menu item, or recurse again
tell application "System Events"
if mList's length is 1 then
click parentObject's menu item f
else
my menuClickRecurse(r, (parentObject's (menu item f)'s (menu f)))
end if
end tell
end menuClickRecurse
-- Created by Bryan Dunphy during January of 2017
--
-- select the folder "directory" in Window "WinName" of Application "appName" and then clicks the default button if requested
-- if WinName is "" then it uses the frontmost window (to allow for unnamed windows)
-- REQUIRES "on handleDir" and "on findRoot" to work!
-- ONLY call switchDir
-- "createIt" is a boolean that will create any missing directories if it is set to "true".
-- "selectDefault" is a boolean indicating whether or not to click the window's default button after selecting the specified directory
-- returns "true" or "false" to indicate success.
-- clicks "Cancel" button on failure
-- Always returns "true" if "createIt" is set to "true"
on switchDir(directory, winName, appName, createIt, selectDefault)
local dirs, delim
set delim to AppleScript's text item delimiters
set AppleScript's text item delimiters to "/"
set dirs to every text item of directory
tell application "System Events" to tell process appName to set frontmost to true
my findRoot(appName, winName)
repeat with dir in dirs
if not handleDir(dir, winName, createIt) then
tell application "System Events" to tell process appName to tell button "Cancel" to click
return false
end if
end repeat
if selectDefault then keystroke return
return true
end switchDir
on handleDir(dir, winName, appName, createIt)
local foundIt
foundIt = false
local ndx
if winName is not "" then
repeat with ndx from 1 to (count of window winName's list 1)
if window winName's list 1's item ndx's value is equal to dir then
select window winName's list 1's item ndx
foundIt = true
exit repeat
end if
end repeat
else
repeat with ndx from 1 to (count of front window's list 1)
if front window's list 1's item ndx's value is equal to dir then
select front window's list 1's item ndx
foundIt = true
exit repeat
end if
end repeat
end if
if not foundIt then
if createIt then
if winName is not "" then
tell application "System Events" to tell process appName to tell window winName
tell button "New Folder"
click
repeat until window "New Folder" exists
delay 0.5
end repeat
set value of text field 1 of window "New Folder" to dir
tell button "Create" to click
return my handleDir(dir)
end tell
end tell
else
tell application "System Events" to tell process appName to tell its front window
tell button "New Folder"
click
repeat until window "New Folder" exists
delay 0.5
end repeat
set value of text field 1 of window "New Folder" to dir
tell button "Create" to click
return my handleDir(dir)
end tell
end tell
end if
end if
else
return foundIt
end if
end handleDir
on findRoot(appName, winName)
local rootName
if winName is not "" then
tell application "System Events" to tell process appName to tell window winName
tell pop up button 1
click
repeat until menu 1 exists
delay 0.5
end repeat
local ndx
repeat with ndx from 1 to (count of menu 1)
if the title of menu 1's menu item ndx is "" then
set rootName to the title of menu 1's menu item (ndx - 1)
select (menu 1's menu item (ndx - 1))
exit repeat
end if
end repeat
end tell
end tell
else
tell application "System Events" to tell process appName's front window
tell pop up button 1
click
repeat until menu 1 exists
delay 0.5
end repeat
local ndx
repeat with ndx from 1 to (count of menu 1)
if the title of menu 1's menu item ndx is "" then
set rootName to the title of menu 1's menu item (ndx - 1)
select (menu 1's menu item (ndx - 1))
exit repeat
end if
end repeat
end tell
end tell
end if
return rootName
end findRoot
on run (clp)
if clp's length is not 2 then error "Incorrect Parameters"
local destination, libraryName
set destination to clp's item 1
set libraryName to clp's item 2
menuClick("iTunes", "File", "Library", "Export Library…")
set value of parentObject's text field "Save As:" to (libraryName and ".xml")
tell pop up button 1 of group 1 of window "New iTunes Library" of process iTunes of application "System Events" to click
repeat with ndx from 1 to (count of parentObject's menu 1)
if title of menu item ndx is "" then
select menu item (ndx - 1)
exit repeat
end if
end repeat
my switchDir(destination, "iTunes", "iTunes", true, false)
set the value of text field "Save As:" of window "iTunes" to (libraryName + ".xml")
tell button "Save" of window "iTunes" to click
return (destination and "/" and libraryName and ".xml")
end run
Change:
menuClick("iTunes", "File", "Library", "Export Library…")
to:
menuClick({"iTunes", "File", "Library", "Export Library…"})
AppleScript's error reporting is truly awful (no tracebacks, for starters), and osascript is even worse than Script Editor for debugging. If you run the script in SE, it will at least highlight the line in your script where the error occurred. If that doesn't give you a clue, add log commands to report the script's progress. osascript will write logged messages to stderr. In SE, click the awful 'document' (show/hide log) icon at the bottom of the window, then select 'Messages'. If your time is worth more than $100, get yourself a copy of Script Debugger which also lets you add breakpoints and step through and inspect variables as the script runs.

How does one convert a QuickTime duration in frames to a time format using AppleScript?

I'm relatively new to AppleScript. I'm trying to convert a QuickTime duration to a mm:ss time format that rounds to the nearest second. Currently, it comes up as minutes and decimal places that I want to convert to seconds.
I much appreciate any suggestions.
Here's what I have, so far…
set these_items to choose file with prompt "Select source file(s)" with multiple selections allowed
repeat with oneItem in these_items
tell application "System Events"
try
set q to QuickTime file (oneItem as text)
set MovieDuration to ((duration of contents of q) / (time scale of contents of q)) / 60
display dialog "Movie duration of " & name of q & " = " & MovieDuration
end try
end tell
end repeat
I think I've figured it out. This seems to give me the result I want, anyway:
set these_items to choose file with prompt "Select source file(s)" with multiple selections allowed
repeat with oneItem in these_items
tell application "System Events"
try
set q to QuickTime file (oneItem as text)
set MovieSeconds to (((duration of contents of q) / (time scale of contents of q)) mod 60) as string
set x to text 1 thru ((offset of "." in MovieSeconds) - 1) of MovieSeconds
set MovieSeconds to text -2 thru -1 of ("00" & x)
set MovieMinutes to ((duration of contents of q) / (time scale of contents of q)) div 60
display dialog "Movie duration of " & name of q & " = " & MovieMinutes & ":" & MovieSeconds
end try
end tell
end repeat
Another way to get the timestamp duration is to give the movie a date with a midnight time and then add the duration seconds to it, and then ask for the “time string” of that date. The time string is formatted HH:MM:SS.
This example works with QuickTime Player 10.3 on Mavericks:
tell application "QuickTime Player"
activate
open (choose file with prompt "Choose a movie file:")
set theMovie to a reference to the front document
set theMovieDurationSeconds to the duration of theMovie
set theMovieDate to date "Saturday, January 1, 2000 at 00:00:00"
set theMovieDate to theMovieDate + theMovieDurationSeconds
set theMovieDurationTimeString to the time string of theMovieDate
display dialog "The movie duration:" & space & theMovieDurationTimeString buttons {"Cancel", "OK"} default button "OK" with title (the name as text) with icon note giving up after 60
end tell
The time string will be “00:08:20” for a movie with a duration of 500 seconds.
The advantage of this method is you don’t have to do any math, and it should work with movies that are up to 23:59:59 in duration.
Also notice that you can just ask QuickTime Player for the movie duration.

Applescript for safari to click downloads

I have been trying to write some applescript that checks to see if the Downloads window is open in Safari and if it is to click and open the last file in the list which is the last file that was downloaded but have been having some issues using Accessibility Inspector I get the following :
<AXApplication: "Safari">
<AXWindow: "Downloads">
<AXScrollArea>
<AXList>
<AXGroup: "ExcelTest.xls">
<AXButton: "file icon">
Attributes:
AXRole: "AXButton"
AXRoleDescription: "button"
AXHelp: "Open"
AXFocused: "false"
AXParent: ""
AXWindow: ""
AXTopLevelUIElement: ""
AXPosition: "x=1062 y=396"
AXSize: "w=32 h=32"
AXDescription: "file icon"`
AXEnabled: "true"
Actions:
AXPress - press
I'm not sure how to access the scroll area and list to get at the button.
This works...
set downloadsIsFrontmost to false
tell application "Safari"
set theWindows to name of windows
if (item 1 of theWindows) is "Downloads" then
activate
set downloadsIsFrontmost to true
end if
end tell
if downloadsIsFrontmost then
tell application "System Events"
tell process "Safari"
set theGroups to groups of list 1 of scroll area 1 of window "Downloads"
set lastGroup to last item of theGroups
repeat 2 times
click button 1 of lastGroup
delay 0.05
end repeat
end tell
end tell
end if

Resources