Play Quicktime Player video using Applescript [duplicate] - applescript

This question already has answers here:
Simple Applescript - Quicktime Issue Play Song
(2 answers)
Closed 1 year ago.
So I wrote a little script that opens a video I want to play. As soon as it opens, it should play. I tried working with play, and even though Quicktime recognizes the word play, it doesn't actually play. What should I do?
tell application "Quicktime player"
open file "/directory/to/the/file/"
play --doesn't work
end tell

If you want to use a fully qualified POSIX pathname, you can use the following, to open and play the file:
tell application "QuickTime Player"
activate
set thisFile to open POSIX file "/path/to/filename.mp4"
play thisFile
end tell

Related

Automation failing when running Apple script

Hello I am using this Apple script in Automator to auto launch and play in full screens video using quick time.
tell application "QuickTime Player"
set theMovie to open file "Users:ronaldwise:Movies:Jarvis Startup.mp4"
tell theMovie
set the presenting to true
set the looping to false
play
end tell
end tell
The Script runs just fine when I press play in automator however, when I try and start the automation application outside of automator I receive this Message.
"The action "Run AppleScript" encountered an error "Not authorized to send Apple Events to QuickTime Player"
My Automation workflow looks like this
Workflow screenshot
I have allowed access to Security privacy > Accessability to the following apps: Terminal, Consul, AEServer, Automator, Consul, and Quicktime player.
I have allowed access to Security Privacy>Full Disk Access to the same above listed programs.
I have allowed access to Security Privacy > Automation to the following: Automator>QuickTime Player and Terminal>Finder
I am at a loss any help please.
The error has nothing to do with Accessibility and Full Disk Access
You have to add the key NSAppleEventsUsageDescription in Info.plist of the script application.
Open the application package in Finder with Right-click > Show Package Contents, then open the Info.plist file in Contents with a text editor and insert
<key>NSAppleEventsUsageDescription</key>
<string>Jarvis needs to control QuickTime Player</string>
The position of the key value pair is irrelevant but it must not follow directly a key line.

Applescript to cause iTunes to play an album by Apple Music URL?

I'm attempting to script iTunes to play an arbitrary playlist or an arbitrary album from a shell script.
I've solved the playlist problem with the following:
osascript -e 'tell application "iTunes" to open location "'$url'?cmd=AddStation"'
Where url is an itmss:// type URL to a playlist. Without the "cmd=AddStation" parameter, it won't automatically play.
This doesn't work for an album URL, however. If I execute:
osascript -e 'tell application "iTunes" to open location "'$url'"'
where url is defined as an itmss:// album URL, then the album does come up on iTunes, but it won't start playing. If I tell iTunes to play via Applescript, it instead starts playing the first album in the entire iTunes library, not the album that is displayed.
There was a similar question about a year back here Applescript play music from iTunes URL ; however, the solution provided in that response requires you to know the title of the first track you want to play. Does anyone have an idea for a more generic solution that will bring an album from the command-line and then play the first track?

How to prevent application from crashing when display goes to sleep in Apple Script

How can I prevent an Apple Script application from crashing whenever the computer logs out or goes to sleep?
I'm currently working on an application that sets the desktop background to the album art of the current song playing in iTunes, however if I put my display to sleep, iTunes will continue to play music but my application will crash, meaning that the desktop background is no longer updating when I return to the computer.
This is the code I'm using to set the wallpaper:
tell desktop 2
set picture to fileName
end tell
where fileName is name of the iTunes artwork of the current song, which has been extracted and saved to the desktop.
It says desktop 2 as I have two different applications for each screen, and conduct testing on my 2nd screen.
This is the error I get when running in Automator and putting the display to sleep:
The action “Run AppleScript” encountered an error.
System Events got an error: Can’t get desktop 2. Invalid index.
I am getting the same results on my primary display also.
How can I stop this script from encountering an error when the display is asleep?
Any suggestions would be greatly appreciated, as I'd love anyone who wants it to have this application.
The safest way is to check if the item exists
if exists desktop 2 then
tell desktop 2
set picture to fileName
end tell
end if
or wrap the code in a try block which ignores any error.
try
tell desktop 2
set picture to fileName
end tell
end try

How to get iTunes to play an iPhone playlist automatically when connected to the iPhone

I have a habit of working/studying while listening to music. And since the Mac I am connected to at work doesn't have all the songs I want, I normally listen to the songs via iTunes from the iPhone Playlists option.
For this, everytime I connect my iPhone to the computer, iTunes detects it, then I have to manually goto the 'iPhone' button in the application, and then "On my iPhone", and then select the playlist, and play a song.
Is there anyway of automating this process ? What I'm looking for, is as soon as I connect my iPhone, iTunes should open the playlist inside my iPhone automatically, and start playing a song at random.
I've tried automating it with Automator. But that doesn't seem to be working. There's something wrong I'm doing in AppleScript ::
tell application "iTunes"
play playlist named "Sasha" of source of type iPhone
end tell
Ha !! I figured it out !! And then I created an applet that would do this for me using AppleScript.
And then, I published the steps/code for the same on my blog below. Here's the link ::
Explanation of Code :: How to start playing songs from your iOS device automatically on your Mac
And the code for the same ::
tell application "iTunes"
set theCurrentPlaylist to view of front browser window
set myiPhone to some source whose kind is iPod
set mainPhonePlaylist to playlist "Sasha" of myiPhone
set the view of the front browser window to mainPhonePlaylist
set EQ enabled to true
play playlist named "Sasha" of myiPhone
set myRandomSong to random number from 1 to count of tracks of (get view of front window)
play track myRandomSong of mainPhonePlaylist
end tell

Get the next ten songs in iTunes DJ using AppleScript

I am creating an application using AppleScript and I want to display a list of the next ten (including the current) songs iTunes will play from iTunes DJ. I can't find it anywhere on the internet, Google, whatever so I asked it here. Can anyone help me? Thanks
The following AppleScript will return the names of the songs in the iTunes DJ playlist:
tell application "iTunes"
get name of every track of playlist "iTunes DJ"
end tell
The song names are returned in play order and will also include recently played songs (this can be configured in the iTunes DJ settings).

Resources