Running video with applescript doesn't work - applescript

The following applescript is meant to open a video file using quicktime player from a file path but I'm not sure why it's not working as it comes up with vague yet menacing errors that are listed as "unknown." Please help fix it. Thanks.
tell application "QuickTime Player"
set theMovie to open file ":Users:User:Desktop:Script:Video.mp4"
tell theMovie
set the presenting to true
set the looping to true
play
end tell
end tell

An HFS path must start with the name of a volume.
set theMovie to open file "Macintosh HD:Users:User:Desktop:Script:Video.mp4"
But there is a relative way
set moviePath to (path to desktop as text) & "Script:Video.mp4"
tell application "QuickTime Player"
set theMovie to open file moviePath
...

Related

AppleScript - What's wrong with my quotes?

Hello and thanks for your help,
This is driving me crazy! I keep having syntax errors for simple stuff like just opening a file.
I get > Syntax Error - Expected end of line, etc. but found “"”.
What am I doing wrong? It's the same quotes as the ones for Finder...
tell application "Finder"
set theFile to selection
open theFile with application "QuickTime Player"
end tell
Here is the corrected script[1]:
tell application "Finder"
set theFile to selection
open theFile using application file "QuickTime Player" of folder "Applications" of startup disk
end tell
This can be simplified by specifying the application by bundle ID instead of location, in which case Finder will find it for you:
open theFile using application file id "com.apple.QuickTimePlayerX"
Or, if you want to do stuff with the file(s) once they’re opened:
tell application "Finder"
set theFiles to selection as alias list
end tell
if theFiles is {} then error "No files selected."
tell application "QuickTime Player"
activate -- (ensures any "can't open file" error dialogs are visible)
open theFiles
-- do other stuff here
end tell
--
[1] The syntax error is due to the special behavior of with/without parameter labels, which expect to be followed by a single keyword (or comma-separated keywords). AppleScript reads the command as open theFile using application—and since a command can’t be immediately followed by an expression, in this case a literal string, it reports a (frustratingly unhelpful) syntax error.

Tell QuickTime to open a random video file in a folder with AppleScript

I would like to play a random video file using QuickTime. My default application for opening video files is VLC, so I specifically want to tell QuickTime Player to open the file, rather than telling Finder to open the file with the default application.
I ran into issues when using 'some file' to select a random file as part of the "tell application "QuickTime Player"" section (I assume it only works with the application "Finder"), so I ended up trying a workaround that involves duplicating a random file to a specific location, then opening that specific location.
My current solution:
tell application "Finder"
set folderPath to "Macintosh HD:Users:username:Desktop:No Backup:Music:TimeOut:"
delete (every item of folder folderPath whose name is "video.mp4")
set sourceFile to some file of folder folderPath
set duplicateFile to duplicate file sourceFile of folderPath
set the name of duplicateFile to "video.mp4"
end tell
tell application "QuickTime Player"
set theMovie to open "Macintosh HD:Users:username:Desktop:No Backup:Music:TimeOut:video.mp4"
tell theMovie
set the looping of theMovie to true
set the present of theMovie to true
play
end tell
end tell
This leads to the following error, highlighting the duplicate file sourceFile of folderPath bit:
Can’t make «class docf» "One of the random videos.mp4" of «class cfol» "TimeOut" of «class cfol» "Music" of «class cfol» "No Backup" of «class cfol» "Desktop" of «class cfol» "username" of «class cfol» "Users" of «class sdsk» of application "Finder" into type integer.
The QuickTime Player part of the script seems to be working correctly; I'm trying to work out the first half of the script.
A bit of help would be very much appreciated!
The following example AppleScript code works for me when the value of the folderPath variable is a proper HFS path:
set folderPath to ¬
"Macintosh HD:Users:username:Desktop:No Backup:Music:TimeOut:"
tell application "Finder" to ¬
set the fileToPlayInQuickTime to ¬
some file of container folderPath as alias
tell application "QuickTime Player"
set theMovie to open fileToPlayInQuickTime
tell theMovie
set the looping of theMovie to true
set the present of theMovie to true
play
end tell
end tell
If you want to target a specific type of file, assuming there are various types in the target folder, then use the following example AppleScript code while setting the value of the name extension property to one that is compatible with the target application:
tell application "Finder" to ¬
set the fileToPlayInQuickTime to ¬
(some file of container folderPath ¬
whose name extension is "mp4") as alias

Quicktime Automator + Applescript for QuickTime reference files

I'm trying to create an Automator droplet that allows me to pass one or more video files to an Applescript that then saves out reference files with "_ref.mov" appended to the file name by using QuickTime Player 7.
Saving as a reference is the default save method, which is why I'm not using save reference file function (also, because I can't find any docs on the using descriptors attribute object.)
I found this script on another forum, and it works, but it doesn't accomplish exactly what I need because it requires that 1) a file is open and 2) the user manually specifies an output file name.
on run {input, parameters}
tell application "QuickTime Player 7"
activate
try
if not (exists document 1) then display dialog "please open a quicktime movie." buttons {"cancel"} default button 1 with icon 1
set theFile to (choose file name)
save document 1 in theFile
close document 1
end try
end tell
return input
end run
I've tried to make changes to accomplish what I need, but my changes break the script:
on run {input, parameters}
tell application "QuickTime Player 7"
activate
try
open input
set theFile to ((path of input) & "_ref.mov))
save document 1 in theFile
close document 1
end try
end tell
return input
end run
I've also tried:
set theFile to (((path of input) & "_ref.mov") as text) doesn't work
either does as alias or as string
Not sure where I'm going wrong. Please help if you can!

Applescript trouble with moving files in script

this is the script I'm trying to use:
on open thisItem
set this_folder to (the POSIX path of thisItem)
set export_folder to "Macintosh HD:Users:j****.*******:Desktop:AUTOMATOR:export"
set pdf_folder to "Macintosh HD:Users:j****.*****:Desktop:AUTOMATOR:PDF"
tell application "Adobe Illustrator" to open thisItem
tell application "Adobe Illustrator" to save current document in export_folder as pdf with options {class:PDF save options, PDF preset:"Low res proofing"}
tell application "Adobe Illustrator" to close current document
tell application "UltraLowPDF" to open
tell application "Finder" to move every file of folder "Macintosh HD:Users:j*****.*****:Desktop:AUTOMATOR:PDF" to container of this_folder
end open
This script should do a few things:
1. It creates a pdf file from an .ai file using adobe illustrator.
2. It then runs that pdf file through an automator workflow, which then spits out a pdf into a folder on my desktop.
3. It then moves that pdf file from the desktop, back to the folder of the original .ai file.
Every part of the script works fine except for step 3, it just seems to ignore it entirely. I've tried adding a delay, thinking the script was getting ahead of itself.
I've also tried isolating the move part of the script, using this droplet:
on open thisItem
set this_folder to (the POSIX path of thisItem)
tell application "Finder" to move every file of folder "Macintosh HD:Users:j****.******:Desktop:AUTOMATOR:PDF" to container of this_folder
end open
But this just throws up an error saying that it can't get the «class ctnr» of the file dropped on it (-1728).
Any help is greatly appreciated.
Thanks.
(note: I've starred out the user name part of the filepath for privacy)
EDIT: It seems that it's the Automator part of the script that is the problem, (the ''UltralowPDF"" app). After this runs, nothing after it in the script will run.
The problem is that thisItem is actually theseItems (aka a list). Either you need a repeat loop or – as in the following code – get the first item of the list.
The Finder does not accept (slash separated) POSIX paths. It works only with (colon separated) HFS paths.
The script uses the relative path path to desktop which point always to the desktop of the current user.
on open theseItems
set automatorFolder to (path to desktop as text) & "AUTOMATOR:"
set thisItem to item 1 of theseItems
set export_folder to automatorFolder & "export:"
set pdf_folder to automatorFolder & "PDF:"
tell application "Adobe Illustrator" to open thisItem
tell application "Adobe Illustrator" to save current document in export_folder as pdf with options {class:PDF save options, PDF preset:"Low res proofing"}
tell application "Adobe Illustrator" to close current document
tell application "UltraLowPDF" to open
tell application "Finder" to move every file of folder pdf_folder to container of thisItem
end open

Applescript to launch file from current folder?

How does one open a file in the same folder as the AppleScript code? Something along these lines?
tell application "QuickTime Player"
activate
open "file.avi"
end tell
(which doesn't work).
Thanks!
tell application "Finder"
open file "somefile.txt" of folder of (file (path to me))
end tell
(only works once you've saved the script - otherwise "path to me" goes to the script editor)

Resources