Closing QuickTime by Applescript - applescript

I am trying to play a movie fullscreen one time, then close the player programmably.
I have tried using QTMovieView, command line and AppleScript and found the Applescript is the most simple way.
BUT, as I really don't know Applescript, I can not make the QuickTime auto close after movie playing.
Everything works fine but the "done" was unrecognized in the repeat line.
Here is the script with this error:
error "QuickTime Player got an error: Can't make done of document 1 into type specifier." number -1700 from done of document 1 to specifier
tell application "QuickTime Player"
activate
open "/Users/...real path of the movie.mov"
present document 1
play document 1
repeat until (get done of document 1)
end repeat
delay 2
close document 1
end tell
Finally, I changed to this, is this ok?
tell application "QuickTime Player"
quit
end tell
tell application "QuickTime Player"
activate
open "/Users/.../...mov"
tell document 1
present
play
repeat until playing is false
end repeat
delay 2
close
end tell
quit
end tell
New problem: app hang before video finish.

This works for me, however it doesn't seem very robust. Is it guaranteed that the current time will always end up being equal to the duration, given that they're both reals? You may want to put some "within epsilon" logic into the repeat condition.
tell application "QuickTime Player"
play document 1
repeat until (current time of document 1 = duration of document 1)
end repeat
delay 2
close document 1
end tell

Try:
tell application "QuickTime Player"
tell document 1
present
play
repeat until playing is false
delay 1
end repeat
end tell
quit
end tell

Related

Open/search Spotify track in Apple Music (with Applescript)

I'd like an easy way to switch from a Spotify release to the same release in Apple Music.
I already found a way to search for the currently playing Spotify track in the Apple Music web player with Applescript:
tell application "Spotify"
if player state is not stopped then
set currentArtist to artist of current track as string
set currentTrack to name of current track as string
open location "https://music.apple.com/search?term=" & currentArtist & " " & currentTrack
end if
end tell
I'd love to:
Open the search in the native Music.app, not the web player. Is this supported?
Ideally not do a search, but go straight to the same release. Maybe with ISRC codes?
Take any selected Spotify track, not just the currently playing one. Looking at the Spotify Applescript dictionary tells me this in not possible.
had a similar problem right now and quickly hacked it out. In my case I want to simply trigger a search on the Music app.
Opened Automator and created a new "Service".
Workflow receives current > "Text" > in "every application".
Here's the AppleScript:
on run {input, parameters}
tell application "Music"
activate
end tell
tell application "System Events"
tell process "Music"
set window_name to name of front window
set value of text field 1 of UI element 1 of row 1 of outline 1 of splitter group 1 of window window_name to input
keystroke ""
key code 36
end tell
end tell
return input
end run
I saved it as "Find on Music" in Automator and now I can select text, right click > Service > Find on Music and Music plops open and shows me the results for the selected text. I hope you can use some parts of it.
I just figured out how to pass text from wherever to the search field in Music, with help from daemon's answer, which no longer works. This should work for what you want to do in conjunction with what you have.
Replace your "open location" line with a variable name for your concatenated string. Add this code below yours and pass that variable in place of 'input' (in my case 'input' is text from any application, which I use to select text of an artist name in an email/webpage/message that I want to send to Music's search).
First it checks to see if the main Music window is open vs the MiniPlayer, and open it if not to enable search via cmd-O, the cmd-F to find, then passes the input and hits return:
tell application "Music"
activate
end tell
tell application "System Events"
if not (exists (window "Music" of process "Music")) then
tell process "Music"
keystroke "0" using command down
end tell
end if
tell process "Music"
keystroke "f" using command down
keystroke input
key code 36
end tell
end tell
So, something like this (I don't have Spotify to check that section, but this should work assuming your code there is correct):
tell application "Spotify"
if player state is not stopped then
set currentArtist to artist of current track as string
set currentTrack to name of current track as string
set spotTrack to currentArtist & " " & currentTrack
end if
end tell
tell application "Music"
activate
end tell
tell application "System Events"
if not (exists (window "Music" of process "Music")) then
tell process "Music"
keystroke "0" using command down
end tell
end if
tell process "Music"
keystroke "f" using command down
keystroke spotTrack
key code 36
end tell
end tell
The only thing I couldn't figure out is how to check if the search field is already in focus, because if it is, the cmd-F causes a system alert sound. Generally not an issue as typically you'll search and interact with something else before running this script again, so calling it good. :)

Determining when QuickTime stops recording using AppleScript

I'm using the following AppleScript to determine when the user clicks the stop recording button in the task bar:
tell application "QuickTime Player"
tell document 1
activate
new screen recording
delay 1
tell application "System Events" to key code 49
delay 2
repeat until (new screen recording) is false
end repeat
end tell
end tell
Though instead the script keeps relaunching QuickTime.
You can put the result of the new screen recording command into a variable.
Use the exists command to check this document
tell application "QuickTime Player"
activate
set tdoc to new screen recording --> document "Screen Recording"
delay 1
tell application "System Events" to key code 49
delay 2
repeat while exists tdoc
delay 1
end repeat
-- the recording is stopped
tell front document
-- do something with the front document ("Untitled")
end tell
end tell

Applescript run Safari in background

I have an applescript that is now doing what I want it to do: Open a specific URL, close the page and wait 2 seconds then do it again. The script works.
The problem is that when I run this on my machine, and I am trying to do something else at the same time, the Safari windows keeps popping up. I would really like to run this in the background so that I can continue to work on whatever I am doing.
The code I have is:
set theURL to "https://sites.google.com/site/whatever/"
repeat 100 times
tell application "Safari"
activate
try
tell window 1 to set current tab to make new tab --with properties {URL:theURL}
set URL of document 1 to theURL
on error
open location theURL
end try
delay 2
try
close window 1
end try
end tell
tell application "Safari"
quit
end tell
delay 1
end repeat
Anyone have a solution to this one?
Safari is coming to the front because you are activating it within the loop.
repeat 100 times
tell application "Safari"
if not (exists document 1) then reopen
set URL of document 1 to "https://sites.google.com/site/whatever/"
delay 2
close window 1
quit
end tell
delay 1
end repeat
EDIT
set myURLs to {"https://www.google.com/", "http://www.yahoo.com/"}
repeat with aUrl in myURLs
repeat 100 times
tell application "Safari"
if not (exists document 1) then reopen
set URL of document 1 to aUrl
delay 2
close window 1
quit
end tell
delay 1
end repeat
end repeat

Append a variable to a link in AppleScript?

Currently I have this working code:
on run
set info to ""
tell application "System Events"
set num to count (every process whose name is "iTunes")
end tell
if num > 0 then
tell application "iTunes"
if player state is playing then
set albumName to (get album of current track)
end if
end tell
end if
return text 1 thru 10 of albumName
end run
if running iTunes and live albums are formatted correctly (YYYY-MM-DD) - this will bring back the date of a live album. I'd like to now take the result of "return text 1 thru 10 of albumName" and add it to this code:
tell application "Firefox" to activate
tell application "Firefox"
open location "http://someband.com/showID=" & *[insert code for "text 1 thr 10 of albumName" variable]*
end tell
I know the "&" isn't correct but I thought that was the start. The way this should work is if I'm listening to "2013-03-12 Live on StackOverflow" I want the script to take "2013-03-12" and append it to my link so Firefox opens website address: "http://someband.com/showID=2013-03-12"
...make sense? Any help would be greatly appreciated. Thank you!
Try:
if application "iTunes" is not running then return
tell application "iTunes"
if player state is playing then
set albumName to (get album of current track)
end if
end tell
set myText to text 1 thru 10 of albumName
-- Assuming Firefox is your default browser…
open location "http://someband.com/showID=" & myText

AppleScript - AppleEvent handler failed

I've got a script than on login, ask a user if they want to watch a movie. For the most part it works great. However on occasion and for reasons unknown to me I get an AppleEvent handler failed error. I've read other post on this error but they all seem to be unique. So, if possible can someone please take a look at my script and tell me why this occasionally pops up and if there's anything i can do to prevent it?
One thing that might help to know, is the one thing in the script that fails when this error occurs is the movie doesn't play. It opens in quicktime but doesn't start.
Thanks in advance, here's the script.
tell application "Welcome" to activate
set question to display dialog "Would you like a welcome video?" buttons {"No, I've seen it", "Yes, please"} default button 2
set answer to button returned of question
if answer is equal to "Yes, please" then tell application "QuickTime Player"
set theMovie to "Macintosh HD:Library:Desktop Pictures:Mac ML Opening Chalkbaord Video.mov"
set openMovie to open theMovie
present openMovie
play openMovie
delay 30
quit
end tell
if answer is equal to "No, I've seen it" then tell application "Welcome"
quit
tell application "System Events"
delete login item "Welcome"
end tell
end tell
My guess is that you probably need a delay between opening and playing the movie. Sometimes the code runs faster than the computer can react. If that's the case then the movie may still be trying to open when the code tells the movie to play... thus the error. As such I added 2 repeat loops which checks for things to make sure they're available before proceeding to the next step in the code. You also need "open file" in the code instead of just "open".
Your approach in your if statements of telling an application to do something is unusual. I wouldn't do that. I would also combine your if statements into one if/else if statement. Anyway, here's how I would write your code (I'm assuming application "Welcome" is the code itself). I hope this helps!
set theMovie to "Macintosh HD:Library:Desktop Pictures:Mac ML Opening Chalkbaord Video.mov"
tell me to activate
set question to display dialog "Would you like a welcome video?" buttons {"No, I've seen it", "Yes, please"} default button 2
set answer to button returned of question
if answer is equal to "Yes, please" then
tell application "QuickTime Player"
activate
set openMovie to open file theMovie
-- delay until the movie opens
set startTime to current date
repeat until exists document 1
delay 0.2
if (current date) - startTime is greater than 10 then return -- a precaution so you don't get stuck in the repeat loop forever
end repeat
present openMovie
play openMovie
-- delay until the movie stops playing
repeat until document 1 is not playing
delay 1
end repeat
quit
end tell
else if answer is equal to "No, I've seen it" then
tell application "System Events" to delete login item "Welcome"
end if

Resources